win32 ExportAsFixedFormat Error

孤街浪徒 提交于 2021-01-29 04:33:00

问题


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/GeneSiC-Guest/PycharmProjects/DScreator/DScreater.py", line 27, in ws.ExportAsFixedFormat(0, path_to_pdf[i],From = 1,To = 1,OpenAfterPublish=False) File "C:\Users\GeneSiC-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)


回答1:


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.:)



来源:https://stackoverflow.com/questions/48874767/win32-exportasfixedformat-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!