Save word doc as PDF

6514
7
02-01-2012 09:21 AM
JohnLay
Occasional Contributor
I have a problem I am hoping someone can help with. I've written a script that opens a word doc, populates several bookmarks, then saves to PDF and closes. It runs beautifully on my machine, however, I have been unable to get it to work anywhere else. The problem appears to be with the line of code that saves my document to pdf having stripped out this section to test by itself. I have tried both win32client and comtypes. Both give me a similar error, but for the life of me I have no idea what the error means.

the win32client code:

import arcpy, os, string, win32com.client

wdFormatPDF = 17

# Local variables:
OUTPATH = str(arcpy.GetParameterAsText(0))

# Create table of Contents
out_TOC = OUTPATH + r"\Table of Contents.pdf"

#Start Word Application
app = win32com.client.Dispatch("Word.Application")
app.visible = 0
TABLEOFCONTENTS = "C:\\Files\\Table of Contents.dotx"
doc = app.Documents.Open(TABLEOFCONTENTS)

doc.SaveAs(out_TOC, FileFormat=wdFormatPDF)
doc.Close()
app.Quit()

Error:
Traceback (most recent call last):
  File "C:\Program Files\Hurricane Tools\Scripts\CreateAffectedAreaMaps.py", line 17, in <module>
    doc.SaveAs(out_TOC, FileFormat=wdFormatPDF)
  File "<COMObject Open>", line 8, in SaveAs
com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Word', u'Command failed', u'C:\\Program Files\\Microsoft Office\\Office12\\1033\\WDMAIN11.CHM', 36966, ), None)

comtypes code:

import arcpy, os, string, comtypes.client

wdFormatPDF = 17

# Local variables:
OUTPATH = str(arcpy.GetParameterAsText(0))

# Create table of Contents
out_TOC = OUTPATH + r"\Table of Contents.pdf"

#Start Word Application
app = comtypes.client.CreateObject('Word.Application')
app.visible = 0
TABLEOFCONTENTS = "C:\\Files\\Table of Contents.dotx"
doc = app.Documents.Open(TABLEOFCONTENTS)

doc.SaveAs(out_TOC, FileFormat=wdFormatPDF)
doc.Close()
app.Quit()

comtypes error:

Traceback (most recent call last):
  File "C:\Program Files\Hurricane Tools\Scripts\comtypesdoctopdf.py", line 17, in <module>
    doc.SaveAs(out_TOC, FileFormat=wdFormatPDF)
COMError: (-2146824090, None, (u'Command failed', u'Microsoft Word', u'C:\\Program Files\\Microsoft Office\\Office12\\1033\\WDMAIN11.CHM', 36966, None))

Like I said, it runs perfectly on my system, but on an identical system (identical in that we are running the same versions of Arc, python, and word) it does not. Any clue what those errors are?
Tags (2)
0 Kudos
7 Replies
StacyRendall1
Occasional Contributor III
The errors are COM related (i.e. not directly related to Python), are they the exact same errors on every machine you test them on?

Both errors mention this file:
'C:\\Program Files\\Microsoft Office\\Office12\\1033\\WDMAIN11.CHM'

What is it (my computer doesn't have it)? Can you open or read it? From memory .CHM files are a kind of help file...
0 Kudos
BruceNielsen
Occasional Contributor III
I have that file (Office '07), but it's 0 bytes.
0 Kudos
AndrewChapkowski
Esri Regular Contributor
Instead of having a dependacy on MS Word, you can use reportlab or you can try docx module found here.

docx will create a MS Word document and reportlab will allow you to create the PDF documents.
0 Kudos
JohnLay
Occasional Contributor
Thanks for the replies. I had never written a line of python before last week and managed to get a fairly complicated script to run perfectly on my machine; it just doesn't work on anyone else's. Since I'm in "crash course" mode, I'll give docx and reportlab a try.
0 Kudos
JohnLay
Occasional Contributor
OK, for the life of me I can't figure out how to get docx to work. There is no documentation provided (that I could find anyway) and what little coding examples I have been able to locate do not work for me. I get a "module object has no attribute open" or "open is not defined" when I simply try to open the doc file. ReportLab also does not appear to be a solution as I am first editing my document template with SearchCursor and Bookmarks commands before exporting to PDF.

Any other possible work arounds?
0 Kudos
AndrewChapkowski
Esri Regular Contributor
0 Kudos
HILLARYHALL
New Contributor II
Check my vb codes on Word to pdf converting, and the Word can be 2007 or any later version, I have tested this converting project several times, and it works well within .net framework 2.0 and all the above versions. Hope it can help you.

Dim PDF As New RasterEdgeImaging()

     Public Sub WordConvertToPdf()
     If True Then
     WordInputFile = ("C:/1.docx")
     ImageOutputFile = ImageFormat.pdf
     End If
     End Sub
     doc.Save(@"C:/1.docx", 1, @"C:/1.pdf")
0 Kudos