简体   繁体   中英

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

I'd like to transform an excel (.xlsx) file to a.pdf file. 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).

Here's a useful link that might help you to convert your file(s) to xls format.

There is an API available which converts the.xlsx file to.pdf file.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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