win32 ExportAsFixedFormat [英] win32 ExportAsFixedFormat
问题描述
我试图更改Excel文件的页脚,然后使用Python3.6中的win32软件包将其转换为pdf.它实际上可以在我的家用PC上工作,而在工作PC上,只有pdf导出部分给了我错误.我想知道MS Office版本是否重要,因为我在家中拥有最新版本并且在工作中使用Excel 2007.这是mycode:
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()
我得到的错误是:
回溯(最近通话最近):文件"C:/Users/Guest/PycharmProjects/DScreator/DScreater.py",第27行ws.ExportAsFixedFormat(0,path_to_pdf [i],From = 1,To = 1,OpenAfterPublish = False)文件" C:\ Users \ Guest \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ win32com \ gen_py \ 00020813-0000-0000-C000-000000000046x0x1x6_Worksheet.py'',115行,格式为ExportAsFixedFormat,收件人,OpenAfterPublish,FixedFormatExtClassPtr)pywintypes.com_error:(-2147352567,'发生了异常.',(0,无,无,无,0,-2147024809),无)
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)
推荐答案
我只是找到答案.对于工作桌面上的Excel 2007,我需要下载ExportAsFixedFormat()的加载项.链接在这里: https://www.microsoft.com/zh-cn/download/confirmation.aspx?id = 7
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.:)
这篇关于win32 ExportAsFixedFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!