Invoking CutePDF from within Python
Tim Golden
mail at timgolden.me.uk
Fri Feb 13 03:39:01 EST 2009
John Henry wrote:
> I have a need to invoke CutePDF from within a Python program.
>
> All I need is to say "Print this to CUTEPDF and store as xyz.pdf".
> Private Sub Print_PDF()
>
> lRetVal = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\Custom PDF
> Printer", _
> 0&, vbNullString, REG_OPTION_NON_VOLATILE,
> KEY_ALL_ACCESS, _
> 0&, hKey, lRetVal)
> sValue = "C:\Sample.pdf"
> RegSetValueExString hKey, "OutputFile", 0&, REG_SZ, sValue, Len
> (sValue)
> sValue = "1"
> RegSetValueExString hKey, "BypassSaveAs", 0&, REG_SZ, sValue, Len
> (sValue)
>
> Dim worddoc As Word.Document
> Set worddoc = wordapp.Documents.Open("C:\Sample.doc")
> wordapp.ActivePrinter = "Custom PDF Printer"
> wordapp.PrintOut
> worddoc.Close
>
> sValue = "0"
> RegSetValueExString hKey, "BypassSaveAs", 0&, REG_SZ, sValue, Len
> (sValue)
> RegCloseKey (hKey)
Direct translation (untested since I don't have CutePDF installed)
<code>
import win32api
import win32con
import win32com
hKey, ret = win32api.RegCreateKeyEx (
win32con.HKEY_CURRENT_USER,
"Software\Custom PDF Printer",
win32con.KEY_ALL_ACCESS
)
win32api.RegSetValueEx (hKey, "OutputFile", None, win32con.REG_SZ, r"c:\sample.pdf")
win32api.RegSetValueEx (hKey, "BypassSaveAs", None, win32con.REG_SZ, r"1")
word = win32com.client.gencache.EnsureDispatch ("Word.Application")
doc = word.Documents.Open (r"c:\sample.doc")
doc.ActivePrinter = "Custom PDF Printer"
word.Printout ()
doc.Close ()
win32api.RegSetValueEx (hKey, "BypassSaveAs", None, win32con.REG_SZ, r"0")
</code>
FWIW, I usually generate PDFs by printing to a Postscript printer
(some random Apple Laserthingy) and then using Ghostscript to
do the conversion. I'm happy to post if you're interested.
TJG
More information about the Python-list
mailing list