Created
October 25, 2019 23:33
-
-
Save elibroftw/b7bc38f7f57786ef9016069265822488 to your computer and use it in GitHub Desktop.
Converts .docx or .doc files to a PDF. With threading
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 os | |
import pythoncom | |
# import comtypes.client | |
import win32com | |
import win32com.client as client | |
from shutil import copyfile, rmtree | |
import threading | |
wdFormatPDF = 17 | |
files = ['list of paths'] # CHANGE THIS | |
pythoncom.CoInitialize() | |
word = client.Dispatch('Word.Application') | |
# word = comtypes.client.CreateObject('Word.Application') | |
def convert_to_pdf(in_file, word_id): | |
pythoncom.CoInitialize() | |
word = client.Dispatch(pythoncom.CoGetInterfaceAndReleaseStream(word_id, pythoncom.IID_IDispatch)) | |
word.Visible = False | |
in_file = os.path.abspath(in_file) | |
out_file = in_file.replace('.docx','.pdf').replace('.doc', '.pdf') | |
doc = word.Documents.Open(in_file) | |
doc.SaveAs(out_file, FileFormat=wdFormatPDF) | |
doc.Close() | |
threads = [] | |
for file in notes: | |
word_id = pythoncom.CoMarshalInterThreadInterfaceInStream(pythoncom.IID_IDispatch, word) | |
t = threading.Thread(target=convert_to_pdf, args=(file, word_id)) | |
t.start() | |
threads.append(t) | |
print("Creating PDF's") | |
for t in threads: t.join() | |
word.Quit() | |
print("Created PDF's") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment