A simple Excel to PDF Converter

This is a simple PDF Converter which watches in a directory for
incoming Excel documents and converts them to PDF. Ghostscript and a
Postscript printer has to be installed.

path_to_watch = "D:\\share\\"
before = dict ([(f, None) for f in os.listdir (path_to_watch)]) def convert(filename): tmpfile = path_to_watch + 'tmp\\'+filename+'.ps' if filename[-3:]=='xls': pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED) myExcel = win32com.client.DispatchEx('Excel.Application') Excel = myExcel.Workbooks.Open(path_to_watch+filename, 0, False, 2) Excel.PrintOut(1, 5000, 1, False, 'Rumborak PDF-Writer Plus 1.0 RC5', \ True, False, tmpfile) Excel.Saved = 1 Excel.Close() myExcel.Quit() del Excel del myExcel os.popen('c:\\gs\\gs8.15\\bin\\gswin32c.exe -sDEVICE=pdfwrite -r300 -dNOPAUSE \ -dBATCH -dSAFER -sPAPERSIZE=a4 \ -sOutputFile="'+path_to_watch+filename+'.pdf" "'+tmpfile+'"') #os.remove(path_to_watch+filename) os.remove(tmpfile) pythoncom.CoUninitialize() while 1: time.sleep (0.1) after = dict ([(f, None) for f in os.listdir (path_to_watch)]) added = [f for f in after if not f in before] if added and added[0][-3:]=='xls': starttime = time.clock() print "File ", ", ".join (added), "added to queue" convert(added[0]) stoptime = time.clock() print "OK. It took", stoptime-starttime, 'seconds.\n' before = after

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.