【发布时间】:2018-02-19 21:55:08
【问题描述】:
我试图更改我的 excel 文件的页脚,然后使用 Python3.6 中的 win32 包将其转换为 pdf。 它实际上适用于我的家用电脑和工作电脑,只有 pdf 导出部分给我错误。我想知道 MS Office 版本是否重要,因为我家里有最新版本,工作中使用的是 Excel 2007。 这是我的代码:
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()
我得到的错误是:
Traceback(最近一次调用最后一次): 文件“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, None, None, None, 0, -2147024809), None)
【问题讨论】: