Welcome, guest | Sign In | My Account | Store | Cart

This Python module converts Microsoft Office documents to Postscript via an installed Postscript printer driver. For example you can build your own Microsoft Office to PDF Converter with Ghostscript. Ready to run applications (webbased or batch converter) you can find at http://win32com.goermezer.de/content/view/156/192/ .

This script needs Pywin32 from Marc Hammond (http://sourceforge.net/projects/pywin32/) and an installed Postscript printer driver.

Simply import msoffice2ps and make a msoffice2ps.word('c:\testfile.doc', 'c:\testfile.ps', 'ps_printername') to convert a Wordfile to Postscript.

Python, 56 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import win32com.client, time, pythoncom

"""
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)

http://win32com.goermezer.de/content/view/156/192/
"""

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')
    # myPowerpoint.Visible = 0 # doesn`t work for PowerPoint
    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()

This script can run in multiple instances for Word and Excel. But not for Powerpoint, which only can be started once.

I didn`t find a solution for that so far. If someone know how to start multiple instances o Powerpoint 2000/XP with COM under Python and Pywin please let me know whow to solve that.

2 comments

Alain Pointdexter 17 years, 2 months ago  # | flag

ONLY THE FIRST PAGE ??? IT SEEMS TO ONLY PRODUCE A PS FILE FOR THE FIRST PAGE OF THE DOCUMENT. This has not to do with the script itself but with the method used in the script because I could obtain the same result by printing the word document to a file.

Jon Peltier 16 years, 11 months ago  # | flag

msoffice2ps - a Microsoft Office to Postscript converter. "If someone know how to start multiple instances o Powerpoint 2000/XP with COM under Python and Pywin please let me know whow to solve that."

Some MS Office applications, such as Word and Excel, are capable of opening multiple instances. Others, including PowerPoint and Outlook, can only open a single instance.

  • Jon

Jon Peltier, Microsoft Excel MVP

Peltier Technical Services

Tutorials and Custom Solutions

http://PeltierTech.com/