我已经花了一整天试图找出如何导出了一个MS Excel文件为PDF。我在别人聪明的迫切需要,比我:Python的Win32Com - ExportAsFixedFormat - MS Excel 2010的文件
这里是我迄今为止和错误,我得到:
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(Ty PE = “xlTypePDF”,OUTPUTFILE) 语法错误:关键字ARG后非关键字ARG
过程结束,退出代码1
请帮助。我在组里找不到任何东西。
谢谢你提前。
罗伯特
一般情况下,你不能把一个非关键字参数关键字参数之后。例如。 'func(arg = foo,bar)'是不允许的,它需要采用'func(arg = foo,arg2 = bar)'或'func(bar,arg = foo)'的格式。你可以重写你的函数调用来匹配吗? – 101 2015-01-27 00:37:56
无花果 - 谢谢你的回答。我相信我先用关键字尝试,然后没有,似乎也没有给我所需的结果。让我仔细检查,我会让你知道。谢谢您的帮助。 – user2320821 2015-01-27 08:59:00
图当我试图用关键字我得到如下:xlwb.ExportAsFixedFormat(TYPE =“xlTypePDF”,文件名= OUTPUTFILE)我得到以下错误:回溯(最近通话最后一个): 文件“C:/用户/ davisr /PycharmProjects/File_Names/Loop_Throug_Open.py”,第55行,在 xlwb.ExportAsFixedFormat(类型= “xlTypePDF”,文件名= OUTPUTFILE) 类型错误:ExportAsFixedFormat()得到了意想不到的关键字参数 '文件名' –
user2320821
2015-01-27 13:27:18