I try to export one of my xlsx files as PDF with pywin32:
JavaScript
x
13
13
1
# Import Module
2
from win32com import client
3
4
# Open Microsoft Excel
5
excel = client.Dispatch("Excel.Application")
6
7
# Read Excel File
8
sheets = excel.Workbooks.Open('C:/Users/xxx/MyExcel.xlsx')
9
work_sheets = sheets.Worksheets[4]
10
11
# Convert into PDF File
12
work_sheets.ExportAsFixedFormat(0,'C:/Users/xxx/MyPDF.pdf')
13
Unfortunately I get this com_error:
JavaScript
1
2
1
com_error: (-2147352567, 'Ausnahmefehler aufgetreten.', (0, 'Microsoft Excel', 'Das Dokument wurde nicht gespeichert. Das Dokument ist möglicherweise geöffnet, oder beim Speichern ist ein Fehler aufgetreten.', 'xlmain11.chm', 0, -2146827284), None)
2
In the end I want to do use this conversion on 30 xlsx files at once. I know how to write the loop for this but can not fix the above mentioned error.
Advertisement
Answer
When using Python paths on Windows, you need to use \
not /
. So convert all your paths to use the double backslash instead of the forward slash:
JavaScript
1
4
1
sheets = excel.Workbooks.Open('C:\Users\xxx\MyExcel.xlsx')
2
3
work_sheets.ExportAsFixedFormat(0,'C:\Users\xxx\MyPDF.pdf')
4
Your ExportAsFixedFormat
function call looks fine so I don’t see any other problem.