批量转换word为pdf

自己写的一个小工具,用于批量转换word为pdf,使用方式:

将完整代码拷贝到文档中,并修改名称为words2pdfs.py
将该文件拷贝到需要转换的文档目录下
在终端中输入python words2pdfs.py
终端会列出来是否需要转换以下文档,输入yes即可。
注意:运行后会在当前目录下生成一个pdfs的文件夹,里面就是转换后的所有文件

import os,shutil
from win32com import client

def doc2pdf(doc_name, pdf_name):
    """
    :word文件转pdf
    :param doc_name word文件名称
    :param pdf_name 转换后pdf文件名称
    """
    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转docx
    """
    try:
        # 首先将doc转换成docx
        word = client.Dispatch("Word.Application")
        doc = word.Documents.Open(doc_name)
        #使用参数16表示将doc转换成docx
        doc.SaveAs(docx_name,16)
    except:
        pass
    finally:
        doc.Close()
        word.Quit()

def createDirs(basePath=os.getcwd()):
    # 存放转化后的pdf文件夹
    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')

---------------------
版权声明:本文为CSDN博主「Fantastic_Liar」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Fantastic_Liar/article/details/90452928

猜你喜欢

转载自www.cnblogs.com/cangqinglang/p/11332548.html