1

I have tons of Word and Excel files. I want to convert many Word files in folders by sub folders to PDF, and I try following code.

This code is not active (I mean there aren't Word convert to PDF) although no error.

Enter image description here

What could be the problem? Is there another solution?

This is my code:

import os
from win32com import client
path = 'D:\programing\test'
word_file_names = []
word = client.DispatchEx("Word.Application")
for dirpath, dirnames, filenames in os.walk(path):
    print (dirpath)
    for f in filenames:
        if f.lower().endswith(".docx") and re.search('Addendum', f):
            new_name = f.replace(".docx", r".pdf")
            in_file = word_file_names.append(dirpath + "\\" + f)
            new_file = word_file_names.append(dirpath + "\\" + new_name)
            doc = word.Documents.Open(in_file)
            doc.SaveAs(new_file, FileFormat = 17)
            doc.Close()
        if f.lower().endswith(".doc") and re.search('Addendum', f):
            new_name = f.replace(".doc", r".pdf")
            in_file = word_file_names.append(dirpath + "\\" + f)
            new_file = word_file_names.append(dirpath + "\\" + new_name)
            doc = word.Documents.Open(in_file)
            doc.SaveAs(new_file, FileFormat = 17)
            doc.Close()
    word.Quit()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Youssri Abo Elseod
  • 671
  • 1
  • 9
  • 23

3 Answers3

2

This is way easier:

from docx2pdf import convert

convert(word_path, pdf_path)
pyroshark
  • 49
  • 6
  • 3
    ps, this needs microsoft word to be installed to work. otherwise, was exactly what i was looking for. – Alvin Wanda Feb 10 '21 at 11:58
  • 1
    You can use 'convert(Output_folder)' to convert all word files in a folder – PyMatFlow Aug 04 '22 at 10:36
  • Do you have any idea about how to export with Bookmarks? This is an option when exporting from Word, but not sure if this option exists within win32. – Mxblsdl Oct 06 '22 at 22:08
1

You can use comtypes,

from comtypes.client import CreateObject
import os

folder = "folder path"
wdToPDF = CreateObject("Word.Application")
wdFormatPDF = 17
files = os.listdir(folder)
word_files = [f for f in files if f.endswith((".doc", ".docx"))]
for word_file in word_files:
    word_path = os.path.join(folder, word_file)
    pdf_path = word_path
    if pdf_path[-3:] != 'pdf':
        pdf_path = pdf_path + ".pdf"

    if os.path.exists(pdf_path):
        os.remove(pdf_path)

    pdfCreate = wdToPDF.Documents.Open(word_path)
    pdfCreate.SaveAs(pdf_path, wdFormatPDF)
LF00
  • 27,015
  • 29
  • 156
  • 295
0

i solved this problem and fixed the code has following

import os
import win32com.client
import re
path = (r'D:\programing\test')
word_file_names = []
word = win32com.client.Dispatch('Word.Application')
for dirpath, dirnames, filenames in os.walk(path):
    for f in filenames:  
        if f.lower().endswith(".docx") :
            new_name = f.replace(".docx", ".pdf")
            in_file =(dirpath + '/'+ f)
            new_file =(dirpath + '/' + new_name)
            doc = word.Documents.Open(in_file)
            doc.SaveAs(new_file, FileFormat = 17)
            doc.Close()
        if f.lower().endswith(".doc"):
            new_name = f.replace(".doc", ".pdf")
            in_file =(dirpath +'/' + f)
            new_file =(dirpath +'/' + new_name)
            doc = word.Documents.Open(in_file)
            doc.SaveAs(new_file, FileFormat = 17)
            doc.Close()
word.Quit()
Youssri Abo Elseod
  • 671
  • 1
  • 9
  • 23