Python Win32Com - ExportAsFixedFormat - MS Excel 2010文件

时间:2015-01-27 00:29:06

标签: python excel win32com

我花了一天时间试图弄清楚如何将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/username/PycharmProjects/File_Names/Loop_Throug_Open.py   文件" C:/Users/username/PycharmProjects/File_Names/Loop_Throug_Open.py" ;,第55行     xlwb.ExportAsFixedFormat(类型=" xlTypePDF",OUTPUTFILE) SyntaxError:关键字arg之后的非关键字arg

使用退出代码1完成处理

请帮忙。我在小组中找不到任何内容。

提前谢谢你。

罗伯特

1 个答案:

答案 0 :(得分:2)

问题在于ExportAsFixedFormat方法:

我改为:      xlwb.ExportAsFixedFormat(0,OutputFile)

我还必须使用双正常斜线放置路径。所以outputWkbkPath如下所示:

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

我希望这有助于其他人。以下帖子实际上让我在那里:

.xlsx and xls(Latest Versions) to pdf using python

它不是相同的包/模块,但该部分有效。