【问题标题】:win32 ExportAsFixedFormatwin32 ExportAsFixedFormat
【发布时间】: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)

【问题讨论】:

    标签: python excel


    【解决方案1】:

    我只是找出答案。对于我工作桌面上的 Excel 2007,我需要下载 ExportAsFixedFormat() 的加载项。链接在这里: https://www.microsoft.com/en-us/download/confirmation.aspx?id=7

    希望这对您也有帮助。:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 2014-03-04
      • 2015-05-22
      • 2016-06-21
      相关资源
      最近更新 更多