使用 python 将 .xlsx 和 xls(最新版本)转换为 pdf

2024-01-04

借助这个使用 python 将 .doc 转换为 pdf https://stackoverflow.com/questions/6011115/doc-to-pdf-using-python链接我正在尝试使用 Excel(.xlsx 和 xls 格式)

以下是修改后的Excel代码:

import os
from win32com import client

folder = "C:\\Oprance\\Excel\\XlsxWriter-0.5.1"
file_type = 'xlsx'
out_folder = folder + "\\PDF_excel"

os.chdir(folder)

if not os.path.exists(out_folder):
    print 'Creating output folder...'
    os.makedirs(out_folder)
    print out_folder, 'created.'
else:
    print out_folder, 'already exists.\n'

for files in os.listdir("."):
    if files.endswith(".xlsx"):
        print files

print '\n\n'

word = client.DispatchEx("Excel.Application")
for files in os.listdir("."):
    if files.endswith(".xlsx") or files.endswith('xls'):
        out_name = files.replace(file_type, r"pdf")
        in_file = os.path.abspath(folder + "\\" + files)
        out_file = os.path.abspath(out_folder + "\\" + out_name)
        doc = word.Workbooks.Open(in_file)
        print 'Exporting', out_file
        doc.SaveAs(out_file, FileFormat=56)
        doc.Close()

它显示以下错误:

>>> execfile('excel_to_pdf.py')
Creating output folder...
C:\Excel\XlsxWriter-0.5.1\PDF_excel created.
apms_trial.xlsx
~$apms_trial.xlsx

Exporting C:\Excel\XlsxWriter-0.5.1\PDF_excel\apms_trial.pdf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "excel_to_pdf.py", line 30, in <module>
    doc = word.Workbooks.Open(in_file)
  File "<COMObject <unknown>>", line 8, in Open
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel
', u"Excel cannot open the file '~$apms_trial.xlsx' because the file format or f
ile extension is not valid. Verify that the file has not been corrupted and that
 the file extension matches the format of the file.", u'xlmain11.chm', 0, -21468
27284), None)
>>>

有问题在

doc.SaveAs(out_file, FileFormat=56)

应该是什么文件格式文件格式? 请帮忙


xlsxwriter的链接:

https://xlsxwriter.readthedocs.org/en/latest/contents.html https://xlsxwriter.readthedocs.org/en/latest/contents.html

借助此功能,您可以生成 excel 文件.xlsx and .xls

例如excel文件生成的名称是审判.xls

现在,如果您想生成该 Excel 文件的 pdf,请执行以下操作:

from win32com import client
xlApp = client.Dispatch("Excel.Application")
books = xlApp.Workbooks.Open('C:\\excel\\trial.xls')
ws = books.Worksheets[0]
ws.Visible = 1
ws.ExportAsFixedFormat(0, 'C:\\excel\\trial.pdf')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 python 将 .xlsx 和 xls(最新版本)转换为 pdf 的相关文章

随机推荐

  • C# 中的正则表达式负向前瞻

    我需要匹配 this 但不是 this 我有这个代码 Match match Regex Match result RegexOptions IgnoreCase while match Success MessageBox Show ma
  • 如何使用代码更改组合框的边框颜色?

    我有一个组合框 我想将边框的颜色更改为红色以将其标记为必填字段 我正在通过我的代码动态生成此组合框列 为了更改边框颜色 我应该设置哪些属性 DataGridTemplateColumn dataGridComboBoxTemplateCol
  • 无法通过 PowerShell 将角色添加到 Azure 应用程序注册

    我正在使用 Powershell 将角色添加到 Azure 中的现有应用程序注册 我正在使用这个命令 Set AzureADApplication ObjectId myApp ObjectId AppRoles newAppRoles n
  • Pico 容器异常:org.picocontainer.injectors.AbstractInjector$UnsatisfiableDependencyException:?

    我似乎遇到了 pico 容器异常 org picocontainer injectors AbstractInjector UnsatisfiableDependencyException 我的代码如下 代表网站特定页面的页面对象 publ
  • Linux 上的集群与lockf

    If lockf与 0 偏移量一起使用 两者之间有什么区别flock and lockf当在独占模式下使用时 如果有的话 我这么问是因为我正在阅读根据平台有条件地在这两个函数中进行编译的代码 并且我想了解可能的原因 之间的实际区别flock
  • 在 QTabWidget 中动态设置单个选项卡的样式

    如何单独 动态地访问单个选项卡 不是其内容或选项卡中的小部件 以实现样式目的 例如更改背景颜色或向其添加图形效果 应用程序可以通过让选项卡以另一种颜色闪烁来通知用户该选项卡需要他们的注意 就像在 Windows 任务栏中 如果窗口想要获得焦
  • Vue路由器不渲染/挂载根路径组件

    我正在制作一个页面vue vue router and laravel 问题 当我进入localhost myproject public html the Home组件未渲染在router view 如果我单击指向服务组件的路由器链接 它
  • 如何正确钳位贝克曼分布

    我正在尝试实现 Microfacet BRDF 着色模型 类似于 Cook Torrance 模型 但我在使用本文中定义的贝克曼分布时遇到了一些问题 https www cs cornell edu srm publications EGS
  • 如何运行快捷方式

    我想知道从另一个应用程序中运行应用程序快捷方式的 官方 方式是什么 像这样 string program application exe Process Start program 这不会工作 如果application exe是一条捷径
  • .Net 4 - 在程序集中包含自定义信息

    我正在构建一个可扩展的应用程序 它将在运行时加载其他程序集Assembly LoadFile 这些附加程序集将包含 WPF 资源字典 外观等 普通资源 Resx 和 或插件类等内容 程序集也可以不包含公共类 只包含资源或资源字典 我正在寻找
  • 如何在SQL Server中生成Guid?

    我需要在 SQL 中创建一个 Id 作为 Guid 无身份 我该怎么做 我将 Id 定义为 uniqueidentifier 但保存在 Db 中的是 00000000 0000 0000 0000 000000000000 Use NEWI
  • Visual Studio 调试器值“{ }”是什么意思?

    我在 Visual Studio 2008 NET C 项目中观察到一个属性 并且调试器显示打开和立即关闭的大括号 我相信它是未初始化的 I 列表 但为什么它不显示 null 或 uninitialized 这是什么意思 br 米兰 最可能
  • 如何在 Swift 中使用 UISplitViewController

    所以我添加一个UISplitViewController到一个嵌入了UITabBarController The UISplitViewController has a UINavigationController作为与其自己的根控制器的主
  • 使用 CIImage 添加纯色边框

    我正在寻找一种使用 Core Image 将纯色边框添加到现有图像的方法 我找到了过滤器列表参考 但没有人制作它 Help 我们需要有要在其中创建实线边框的 CIImage 范围或 CGRect 然后 我们可以在指定区域绘制一个形成实线的C
  • ViewDidLoad 期间 ViewModel 为 null

    我正在开始在 iOS 中使用 MvvmCross public class MainView MvxTabBarViewController public override void ViewDidLoad base ViewDidLoad
  • 如何限制tensorflow GPU内存使用?

    我在 Ubuntu 18 04 中使用了tensorflow gpu 1 13 1 并在 Nvidia GeForce RTX 2070 上使用了 CUDA 10 0 驱动程序版本 415 27 下面的代码用于管理张量流内存使用情况 我有大
  • 有没有办法减少操作栏上操作项图标之间的间距?

    我想减少添加到 ActionBar 的操作项图标之间的间距 我们有办法做到这一点吗 使用自定义 android actionButtonStyle
  • 如何使用javascript获取网页的长度/大小

    我们使用 w3c 导航 API 获取网页计时信息 我们使用资源计时 API 获取资源信息 现在 如何获取网页的大小 看来 如果我知道页面何时加载等 我应该知道最后一个字节何时下载 这应该足以给我页面的长度 大小 我如何获得这个长度 尺寸 找
  • 尝试使用 php 使用 aes-256-gcm 进行解密

    我想知道是否有人可以帮忙 我使用的是aes 256 gcm加密方法 可以加密 但无法解密 下面是我的代码 任何人都可以看到我哪里出错了 textToDecrypt POST message password 3sc3RLrpd17 meth
  • 使用 python 将 .xlsx 和 xls(最新版本)转换为 pdf

    借助这个使用 python 将 doc 转换为 pdf https stackoverflow com questions 6011115 doc to pdf using python链接我正在尝试使用 Excel xlsx 和 xls