Created
April 20, 2018 15:06
-
-
Save giorgi-ghviniashvili/968169634ae9454be32a7da268c78f46 to your computer and use it in GitHub Desktop.
convert docs to pdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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