Start New Discussion within our Software Development Community

A small script which converts Microsoft Office Documents (Word, Excel, Powerpoint) to Posscript. You can build your own PDF Converter with this script.

More Python/Pywin32 examples can be found at http://www.win32com.de and Python based PDF Converter (for server and client usage) can be found at http://www.goermezer.de .

import win32com.client, time, pythoncom
"""
MSOffice2PS - Microsoft Office to Postscript Converter 1.0
For more examples please have a look to http://www.win32com.de
Makes a Postscriptfile from Word-, Excel- or Powerpoint-Files.
usage:
* make_ps.word('wordfilename', 'psfilename', 'ps_printername')
* make_ps.excel('excelfilename', 'psfilename', 'ps_printername')
* make_ps.powerpoint('powerpointfilename', 'psfilename', 'ps_printername')
"""
def word(wordfile, psfile, printer):
    pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
    myWord = win32com.client.DispatchEx('Word.Application')
    myWord.Application.ActivePrinter = printer
    myDoc = myWord.Documents.Open(wordfile, False, False, False)
    myDoc.Saved=1
    myWord.Application.NormalTemplate.Saved = 1
    myWord.PrintOut(True, False, 0, psfile)
    while myWord.BackgroundPrintingStatus > 0:
        time.sleep(0.1)
    myDoc.Close()
    myWord.Quit()
    del myDoc
    del myWord
    pythoncom.CoUninitialize()
 
def excel(excelfile, psfile, printer):
    pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
    myExcel = win32com.client.DispatchEx('Excel.Application')
    myExcel.Application.AskToUpdateLinks = 0
    Excel = myExcel.Workbooks.Open(excelfile, 0, False, 2)
    Excel.Saved = 1
    Excel.PrintOut(1, 5000, 1, False, printer, True, False, psfile)
    Excel.Close()
    myExcel.Quit()
    del myExcel
    del Excel
    pythoncom.CoUninitialize()
 
def powerpoint(powerpointfile, psfile, printer):
    pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
    myPowerpoint = win32com.client.DispatchEx('Powerpoint.Application')
    myPpt = myPowerpoint.Presentations.Open(powerpointfile, False, False, False)
    myPpt.PrintOptions.PrintInBackground = 0
    myPpt.PrintOptions.ActivePrinter = printer
    myPpt.Saved = 1
    myPpt.PrintOut(1, 5000, psfile, 0, False)
    myPpt.Close()
    #myPowerpoint.Quit()
    del myPpt
    #del myPowerpoint
    pythoncom.CoUninitialize()