簡體   English   中英

Python Win32Com-ExportAsFixedFormat-MS Excel 2010文件

[英]Python Win32Com - ExportAsFixedFormat - MS Excel 2010 file

我花了整整一天的時間來弄清楚如何將MS Excel文件導出為PDF。 我迫切需要一個比我聰明的人:

這是我到目前為止所遇到的錯誤:

 import os
 import win32com.client
 import win32com.client.dynamic
 import datetime
 import time
  #Path to Read from where you want all the files read from InputWkbkPath = "O:/MIS/Reporting/Field Reports/2014_Template_Files/w_code/" obj = win32com.client.Dispatch("Outlook.Application") xlApp = win32com.client.DispatchEx('Excel.Application') OutputWkbkPath ='O:/MIS/Reporting/Field Reports/2015_Template_Files/Directors /Templates/20150123_Archive/' for subdir, dirs, files in os.walk(InputWkbkPath): for file in files: #print os.path.join(subdir, file) ip= os.path.join(subdir, file) xlwb= xlApp.Workbooks.Open(ip) #print xlwb currentyear = datetime.date.today().strftime("%Y") currentmonth = datetime.date.today().strftime("%B") currentday = datetime.date.today().strftime("%d") currentdate = currentmonth+"-"+currentday+"-"+currentyear participant = xlwb.Worksheets(1).Range("C4").Value title = xlwb.Worksheets(1).Range("C5").Value StaffCode = xlwb.Worksheets(1).Range("C6").Value OfficeName = xlwb.Worksheets(1).Range("C7").Value LOCode = xlwb.Worksheets(1).Range("C8").Value Region = xlwb.Worksheets(1).Range("C9").Value ESN = str(xlwb.Worksheets(1).Range("C10").Value) ParticipantEmail= xlwb.Worksheets(1).Range("C11").Value MDEmail= xlwb.Worksheets(1).Range("C12").Value RVPEmail = xlwb.Worksheets(1).Range("C13").Value if title == "Director" or title == "DIRECTOR": FileName = LOCode+"_"+participant+"_"+ESN+"_Comp_Model_"+currentdate+".xlsx" #print FileName else: FileName = Region+"_"+LOCode+"_"+participant+"_"+ESN+"_Comp_Model_"+currentdate+".pdf" OutputFile=OutputWkbkPath+FileName xlwb.Worksheets(1).Activate #print OutputFile ws=xlwb.Worksheets(1) ws.Visible = 1 xlwb.ExportAsFixedFormat(Type="xlTypePDF",OutputFile) xlwb.Close(True) 

我收到以下錯誤:

C:\\ Python27 \\ python.exe C:/ Users /用戶名/PycharmProjects/File_Names/Loop_Throug_Open.py文件“ C:/ Users /用戶名/PycharmProjects/File_Names/Loop_Throug_Open.py”,第55行xlwb.ExportAsFixedFormat(Type =“ xlTypePDF “,OutputFile)語法錯誤:關鍵字arg之后為非關鍵字arg

流程以退出代碼1完成

請幫忙。 我在小組中找不到任何內容。

提前謝謝你。

羅伯特

問題出在ExportAsFixedFormat方法中:

我更改為以下內容:xlwb.ExportAsFixedFormat(0,OutputFile)

我還必須在路徑上加上兩次常規斜杠。 因此,outputWkbkPath如下所示:

OutputWkbkPath ='O:\\MIS/Reporting\\Field Bonus Plan Reports\\2015__Files\\ DirectorsTemplates\\20150123_Archive\\'

我希望這可以幫助其他人。 以下帖子實際上使我到達了那里:

.xlsx和xls(最新版本)到python的pdf

它不是相同的包/模塊,但是那部分起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM