Python docx、Excel、PPT转pdf
1、docx转pdf
下载
python -m pip install docx2pdf
使用
import os from docx2pdf import convert def __create_folder(folder): folder = os.path.abspath(folder) if not os.path.exists(folder): try: os.makedirs(folder) msg = 'Success create folder({})'.format(folder) print(msg) except Exception as e: msg = 'Failed create folder({}), exception({})'.format(folder, e) print(msg) def docx_to_pdf(docx_name, pdf_name): docx_name = os.path.abspath(docx_name) pdf_name = os.path.abspath(pdf_name) __create_folder(os.path.dirname(pdf_name)) if os.path.isfile(docx_name): try: convert(docx_name, pdf_name) msg = 'Success convert docx to pdf, docx path "{}", ' \ 'pdf path "{}"'.format(docx_name, pdf_name) print(msg) except Exception as e: msg = 'Fail convert docx to pdf, docx path "{}", ' \ 'pdf path "{}" exception "{}"'.format(docx_name, pdf_name, e) print(msg) def debug(): docx_to_pdf("Docx_Example.docx", "a.pdf") if __name__ == '__main__': debug()
2、Excel转pdf
import os from win32com import client def __create_folder(folder): folder = os.path.abspath(folder) if not os.path.exists(folder): try: os.makedirs(folder) msg = 'Success create folder({})'.format(folder) print(msg) except Exception as e: msg = 'Failed create folder({}), exception({})'.format(folder, e) print(msg) def excel_to_pdf(excel_name, pdf_name): excel_name = os.path.abspath(excel_name) pdf_name = os.path.abspath(pdf_name) __create_folder(os.path.dirname(pdf_name)) try: xlApp = client.Dispatch("Excel.Application") books = xlApp.Workbooks.Open(excel_name) books.ExportAsFixedFormat(0, pdf_name) xlApp.Quit() msg = 'Success convert excel to pdf, ' \ 'excel path "{}", pdf path "{}"'.format(excel_name, pdf_name) print(msg) except Exception as e: msg = 'Fail convert excel to pdf, excel path "{}", pdf path "{}" ' \ 'exception "{}"'.format(excel_name, pdf_name, e) print(msg) def debug(): excel_to_pdf(os.path.abspath("Excel_Example.xlsx"), os.path.abspath("a/a1.pdf")) if __name__ == '__main__': debug()
3、ppt转pdf
import os from win32com.client import gencache def __create_folder(folder): folder = os.path.abspath(folder) if not os.path.exists(folder): try: os.makedirs(folder) msg = 'Success create folder({})'.format(folder) print(msg) except Exception as e: msg = 'Failed create folder({}), exception({})'.format(folder, e) print(msg) def ppt_to_pdf(ppt_name, pdf_name): ppt_name = os.path.abspath(ppt_name) pdf_name = os.path.abspath(pdf_name) __create_folder(os.path.dirname(pdf_name)) try: p = gencache.EnsureDispatch("PowerPoint.Application") ppt = p.Presentations.Open(ppt_name, False, False, False) ppt.ExportAsFixedFormat(pdf_name, 2, PrintRange=None) p.Quit() msg = 'Success convert ppt to pdf, ' \ 'ppt path "{}", pdf path "{}"'.format(ppt_name, pdf_name) print(msg) except Exception as e: msg = 'Fail convert ppt to pdf, ppt path "{}", pdf path "{}" ' \ 'exception "{}"'.format(ppt_name, pdf_name, e) print(msg) def debug(): ppt_to_pdf(os.path.abspath("PPTX_Example.pptx"), os.path.abspath("a/a1.pdf")) if __name__ == '__main__': debug()
学习链接:
https://www.jianshu.com/p/020f4f62beb9
https://www.cnblogs.com/Iloveyy/p/13304073.html
https://www.codeleading.com/article/9032753153/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏