Skip to content

Instantly share code, notes, and snippets.

@peremen
Created February 3, 2020 17:07
Show Gist options
  • Save peremen/50495a242bc4a5ba6789e35e66a16b9f to your computer and use it in GitHub Desktop.
Save peremen/50495a242bc4a5ba6789e35e66a16b9f to your computer and use it in GitHub Desktop.
Convert *.doc(x) files into PDF with bookmarks
#!/usr/bin/python3
import glob
import os
import sys
import win32com.client as win32
import fnmatch
def convert_pdf(word, doc_fname, pdf_fname):
C = win32.constants
doc = word.Documents.Open(doc_fname)
if doc == None:
return
try:
doc.ExportAsFixedFormat(pdf_fname, ExportFormat = C.wdExportFormatPDF, OpenAfterExport = False,
OptimizeFor = C.wdExportOptimizeForPrint, Range = C.wdExportAllDocument,
CreateBookmarks = C.wdExportCreateHeadingBookmarks, IncludeDocProps = True,
Item = C.wdExportDocumentWithMarkup)
except:
print('Error while converting %s' % doc_fname)
doc.Close()
if __name__ == '__main__':
#word = win32.gencache.EnsureDispatch('Word.Application')
word = win32.DispatchEx('Word.Application')
word.Visible = False
docs = []
for dir in sys.argv[1:]:
for root, dirnames, filenames in os.walk(dir):
for filename in fnmatch.filter(filenames, '*.doc*'):
docs.append(os.path.abspath(os.path.join(root, filename)))
for doc_fname in docs:
path, doc_fname_base = os.path.split(doc_fname)
doc_basename, ext = os.path.splitext(doc_fname_base)
pdf_fname = os.path.join(path, doc_basename + '.pdf')
print('Converting %s into %s' % (doc_fname, pdf_fname))
convert_pdf(word, doc_fname, pdf_fname)
word.Quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment