1

I have spent the day trying to figure out how to export out a MS Excel File as a PDF. I am in desperate need of someone smarter than I:

Here is what I have so far and the error I get:

 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)

I get the following error:

C:\Python27\python.exe C:/Users/username/PycharmProjects/File_Names/Loop_Throug_Open.py File "C:/Users/username/PycharmProjects/File_Names/Loop_Throug_Open.py", line 55 xlwb.ExportAsFixedFormat(Type="xlTypePDF",OutputFile) SyntaxError: non-keyword arg after keyword arg

Process finished with exit code 1

Please help. I can not find any on it in the groups.

Thank you ahead of time.

Robert

user2320821
  • 1,141
  • 3
  • 13
  • 19
  • 1
    In general, you can't put a non keyword argument after a keyword argument. E.g. `func(arg=foo, bar)` is not allowed, it needs to be in the form `func(arg=foo, arg2=bar)` or `func(bar, arg=foo)`. Can you rewrite your function call to match that? – 101 Jan 27 '15 at 00:37
  • figs - thank you for answering. I believed I tried first with keywords, then without and neither way seemed to give me the needed result. Let me double check and I will let you know. Thanks for the help. – user2320821 Jan 27 '15 at 08:59
  • Figs when I attempt with the keyword I get the following: xlwb.ExportAsFixedFormat(Type="xlTypePDF",FileName=OutputFile) I get the following error: Traceback (most recent call last): File "C:/Users/davisr/PycharmProjects/File_Names/Loop_Throug_Open.py", line 55, in xlwb.ExportAsFixedFormat(Type="xlTypePDF",FileName=OutputFile) TypeError: ExportAsFixedFormat() got an unexpected keyword argument 'FileName' – user2320821 Jan 27 '15 at 13:27
  • xlwb.ExportAsFixedFormat(Type=xlTypePDF,FileName=OutputFile) When I attempt the code without the quotes around xlTypePDF I get the following error: Traceback (most recent call last): File "C:/Users/davisr/PycharmProjects/File_Names/Loop_Throug_Open.py", line 55, in xlwb.ExportAsFixedFormat(Type=xlTypePDF,FileName=OutputFile) NameError: name 'xlTypePDF' is not defined – user2320821 Jan 27 '15 at 13:31

1 Answers1

3

The problem was in the ExportAsFixedFormat method:

I changed to the following: xlwb.ExportAsFixedFormat(0, OutputFile)

I also had to put the path with double regular slashes. So the outputWkbkPath looks like the following:

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

I hope this helps someone else. The following post actually got me there:

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

It is not the same package/module but that part works.

Community
  • 1
  • 1
user2320821
  • 1,141
  • 3
  • 13
  • 19
  • Consider someone else helped. Just lost an hour and a half of my life because '/' != '\\' when you're working with this. – mattvivier Oct 30 '15 at 19:23