2

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()
5
  • This question has been answered before and this may help you: stackoverflow.com/questions/6011115/doc-to-pdf-using-python Commented Oct 31, 2018 at 6:37
  • 1
    Is there any output at all? Can you show the stuff being printed? Have you tried running this in a debugger? Commented Oct 31, 2018 at 8:01
  • i have added the the result photo in my tobic Commented Oct 31, 2018 at 8:13
  • You have commented out the except block, are you sure the code is not throwing any error? Commented Oct 31, 2018 at 10:12
  • ok ankit , i have summarized my code for important thing only (look agin), if you can help Commented Oct 31, 2018 at 10:23

3 Answers 3

3

This is way easier:

from docx2pdf import convert

convert(word_path, pdf_path)
Sign up to request clarification or add additional context in comments.

3 Comments

ps, this needs microsoft word to be installed to work. otherwise, was exactly what i was looking for.
You can use 'convert(Output_folder)' to convert all word files in a folder
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.
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)

Comments

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.