Batch convert word to pdf

A small tool written by myself is used to convert word to pdf in batch. How to use it:

  1. Copy the full code into the document, And change the name to words2pdfs.py.
  2. Copy the file to the document directory to be converted
  3. Enter python words2pdfs.py in the terminal
  4. The terminal will list whether the following documents need to be converted. Enter yes.
  • Note: after running, a pdfs folder will be generated in the current directory, which contains all the converted files
import os,shutil
from win32com import client

def doc2pdf(doc_name, pdf_name):
    """
    :word File transfer pdf
    :param doc_name word File name
    :param pdf_name After conversion pdf File name
    """
    try:
        word = client.DispatchEx("Word.Application")
        if os.path.exists(pdf_name):
            os.remove(pdf_name)
        worddoc = word.Documents.Open(doc_name,ReadOnly = 1)
        worddoc.SaveAs(pdf_name, FileFormat = 17)
        return pdf_name
    except Exception as e:
        print(e)
        return 1
    finally:
        worddoc.Close()
        word.Quit()

def doc2docx(doc_name,docx_name):
    """
    :doc turn docx
    """
    try:
        # First convert doc to docx
        word = client.Dispatch("Word.Application")
        doc = word.Documents.Open(doc_name)
        #Use parameter 16 to convert doc to docx
        doc.SaveAs(docx_name,16)
    except:
        pass
    finally:
        doc.Close()
        word.Quit()

def createDirs(basePath=os.getcwd()):
    # Store converted pdf folder
    pdfs_dir = basePath + '/pdfs'
    if not os.path.exists(pdfs_dir):
        os.mkdir(pdfs_dir)
    return pdfs_dir

def getFileNames(basePath=os.getcwd()):
    filenames=[]
    # move all .words files to words_dir
    for file in os.listdir(basePath):
        if file.endswith('.docx'):
            filenames.append(file)
        elif file.endswith('.doc'):
            filenames.append(file)
        else:
            pass
    return filenames

def convert(basePath=os.getcwd(),filenames=[]):
    pdfs_dir=createDirs(basePath)
    for filename in filenames:
        pdfName='.'.join(filename.split('.')[:-1])+'.pdf'
        doc2pdf(os.path.join(basePath,filename),os.path.join(pdfs_dir,pdfName))


if __name__ == '__main__':
    basePath=os.getcwd()
    lfileNames=getFileNames(basePath)
    print('are you going to convert these files to pdf?')
    for filename in lfileNames:
        print(filename)
    print("yes/no?")
    while True:
        command=input()
        if command=='yes':
            convert(basePath,lfileNames)
            break
        elif command=='no':
            break
        else:
            print('wrong command,input yes or no please')

Tags: Python

Posted on Wed, 06 Nov 2019 18:20:43 -0500 by j007w