其他分享
首页 > 其他分享> > 批量转换word为pdf

批量转换word为pdf

作者:互联网

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

  1. 将完整代码拷贝到文档中,并修改名称为words2pdfs.py
  2. 将该文件拷贝到需要转换的文档目录下
  3. 在终端中输入python words2pdfs.py
  4. 终端会列出来是否需要转换以下文档,输入yes即可。
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')

标签:word,name,批量,doc,basePath,pdf,os
来源: https://blog.csdn.net/Fantastic_Liar/article/details/90452928