I was trying to change the footers of my excel file then convert it to pdf with win32 package in Python3.6. It actually work with my home pc and at work pc only the pdf exporting part is giving me error. I am wondering if the MS Office version matters, since I have the newest at home and Excel 2007 at work. Here is mycode :
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
file_path = r"C:\Mydir\DSexample\Myfile.xlsx"
wb = excel.Workbooks.Open(file_path)
ws = wb.ActiveSheet
ws.PageSetup.CenterFooter = '&"Arial,Regular"&8new address'
ws.PageSetup.LeftFooter = '&"Arial,Regular"&8new date'
path_to_pdf = list()
excel.Visible = True
if ws.Cells(24,7).Value[-2]=="R":
print(type(str(ws.Cells(24,7).Value[:-3])))
path_to_pdf.append("C:\\Mydir\\DSexample\\"+str(ws.Cells(24,7).Value[:-3]).strip()+".pdf")
path_to_pdf.append("C:\\Mydir\\DSexample\\" +
str(ws.Cells(24, 7).Value[:-3]).strip() + "R.pdf")
wb.SaveAs(r"C:\Mydir\DSexample\new.xlsx")
for i in range(0,len(path_to_pdf)):
ws.ExportAsFixedFormat(0, path_to_pdf[i])
wb.Close()
The Error I got is:
Traceback (most recent call last): File "C:/Users/Guest/PycharmProjects/DScreator/DScreater.py", line 27, in ws.ExportAsFixedFormat(0, path_to_pdf[i],From = 1,To = 1,OpenAfterPublish=False) File "C:\Users\Guest\AppData\Local\Programs\Python\Python36\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x6_Worksheet.py", line 115, in ExportAsFixedFormat , To, OpenAfterPublish, FixedFormatExtClassPtr) pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024809), None)
I just find out the answer. For the Excel 2007 on my work desktop, I need to download the add-in for ExportAsFixedFormat(). Here is the link: https://www.microsoft.com/en-us/download/confirmation.aspx?id=7
Hope this help you as well.:)