简体   繁体   English

Python 将 Excel 文件 (.xlsx) 转换为 PDF (.pdf)

[英]Python Converting an Excel file (.xlsx) to a PDF (.pdf)

I'd like to transform an excel (.xlsx) file to a.pdf file.我想将 excel (.xlsx) 文件转换为 a.pdf 文件。 I found this code:我找到了这段代码:

from win32com import client
xlApp = client.Dispatch("Excel.Application")
books = xlApp.Workbooks.Open('C:\\Users\\gregd\\PycharmProjects\\excel\\data.xlsx')
ws = books.Worksheets[0]
ws.Visible = 1
ws.ExportAsFixedFormat(0, 'C:\\Users\\gregd\\PycharmProjects\\excel\\data.pdf')

But I'm getting this error:但我收到了这个错误:

Traceback (most recent call last):
File "C:/Users/gregd/PycharmProjects/excel/idea.py", line 3, in <module>
books = xlApp.Workbooks.Open('C:\\Users\\gregd\\PycharmProjects\\excel\\data.xlsx')
File "C:\Users\gregd\PycharmProjects\excel\venv\lib\site-packages\win32com\client\dynamic.py", line 
527, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Excel.Application.Workbooks

The code seems to work for others, but not for me.该代码似乎适用于其他人,但不适用于我。 Any ideas as to why this is?关于为什么会这样的任何想法?

I just consulted the answer from which you copied this code fragment and simply deduced that this method only opens documents with xls extensions (not xlsx).我刚刚查阅了您复制此代码片段的答案,并简单地推断出此方法仅打开具有 xls 扩展名(而不是 xlsx)的文档。

Here's a useful link that might help you to convert your file(s) to xls format.这是一个有用的链接,可以帮助您将文件转换为 xls 格式。

There is an API available which converts the.xlsx file to.pdf file.有一个可用的 API 将 .xlsx 文件转换为 .pdf 文件。

Reference: https://www.convertapi.com/xlsx-to-pdf参考: https://www.convertapi.com/xlsx-to-pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM