Skip to content

Instantly share code, notes, and snippets.

@giorgi-ghviniashvili
Created April 20, 2018 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giorgi-ghviniashvili/968169634ae9454be32a7da268c78f46 to your computer and use it in GitHub Desktop.
Save giorgi-ghviniashvili/968169634ae9454be32a7da268c78f46 to your computer and use it in GitHub Desktop.
convert docs to pdf
import sys
import os
from os.path import isfile, join
from os import listdir
import comtypes.client
wdFormatPDF = 17
INPUT_PAHT_DOC = 'output/doc/'
OUTPUT_PAHT_PDF = 'output/pdf/'
def getAllFileNames():
onlyfiles = [f for f in listdir(INPUT_PAHT_DOC) if f[0] != '~' and isfile(join(INPUT_PAHT_DOC, f))] # exclude files that start with ~
return onlyfiles
def convert(filename):
in_file = os.path.abspath(join(INPUT_PAHT_DOC, filename))
out_file = os.path.abspath(join(OUTPUT_PAHT_PDF, filename.replace('.docx','.pdf')))
word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
if __name__ == '__main__':
filenames = getAllFileNames()
for filename in filenames:
print("converting...{0}".format(filename))
convert(filename)
print("converted")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment