pu369com

python docx转pdf

需要将docx转pdf,试试python,参考Python--word批量转化为pdf - 简书 (jianshu.com)

复制代码
import os
import comtypes.client


def get_path():
    # 获取当前运行路径
    path = os.getcwd()
    # 获取所有文件名的列表
    filename_list = os.listdir(path)
    # 获取所有word文件名列表
    wordname_list = [filename for filename in filename_list \
                     if filename.endswith((".doc", ".docx"))]
    for wordname in wordname_list:
        # 分离word文件名称和后缀,转化为pdf名称
        pdfname = os.path.splitext(wordname)[0] + '.pdf'
        # 如果当前word文件对应的pdf文件存在,则不转化
        if pdfname in filename_list:
            continue
        # 拼接 路径和文件名
        wordpath = os.path.join(path, wordname)
        pdfpath = os.path.join(path, pdfname)
        #生成器
        yield wordpath,pdfpath


def convert_word_to_pdf():
    word = comtypes.client.CreateObject("Word.Application")
    word.Visible = 0
    for wordpath,pdfpath in get_path():
        newpdf = word.Documents.Open(wordpath)
        newpdf.SaveAs(pdfpath, FileFormat=17)
        newpdf.Close()
    # ppt转化为pdf
    # ppt = comtypes.client.CreateObject("Powerpoint.Application")
    # ppt.Visible = 1
    # newpdf = ppt.Presentations.Open(in_file)
    # newpdf.SaveAs(out_file, FileFormat=32)
    # newpdf.Close()


if __name__ == "__main__":
    convert_word_to_pdf()
复制代码

 

posted on   pu369com  阅读(1260)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-02-16 纯golang爬虫实战-(六)-关于cookiejar的理解 (2020-02-14 13:50)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示