我试图使用win32com.client在python中从excel创建pdf

时间:2018-08-21 11:00:05

标签: excel python-2.7 win32com

我正在尝试使用python 2.7.15将Excel表从win32com.client保存为pdf。 虽然我正在设置“打印区域”,但整个工作表仍保存为pdf。我只想保存A1:I36之间的pdf。如果我尝试设置页数ws.PageSetup.Pages.Count = 1,则会出现以下错误

Traceback (most recent call last):
  File "C:\Users\MMC\Desktop\fmisc_codes\py_excel.py", line 202, in <module>
    ws.PageSetup.Pages.Count = 1
  File "C:\Python27\ArcGISx6410.3\lib\site-packages\win32com\client\dynamic.py", line 581, in __setattr__
    raise AttributeError("Property '%s.%s' can not be set." % (self._username_, attr))
AttributeError: Property '<unknown>.Count' can not be set.

到目前为止,我的尝试受到Print chosen worksheets in excel files to pdf in python

的启发
o = win32com.client.Dispatch("Excel.Application")

o.Visible = False

wb_path = r'D:\Shared\%s' %xln

wb = o.Workbooks.Open(wb_path)

path_to_pdf = r'D:\shared\sample.pdf'

print_area = 'A1:I36'

ws = wb.Worksheets[1]


ws.PageSetup.Zoom = False

ws.PageSetup.FitToPagesTall = 1

ws.PageSetup.FitToPagesWide = 1
ws.PageSetup.PaperSize = 9
ws.PageSetup.PrintArea = print_area
ws.PageSetup.Pages.Count = 1

wb.WorkSheets(1).Select()
wb.ActiveSheet.ExportAsFixedFormat(0, path_to_pdf)

在这方面的任何帮助,我将不胜感激。

0 个答案:

没有答案