装依赖
pip install comtypes==1.1.10
转换
import comtypes.client
import os
def ppt_pdf(path):
# PPT 转 PDF
pdf_path = path.replace('ppt', 'pdf') # pdf保存路径 推荐使用绝对路径
try:
p = client.CreateObject("PowerPoint.Application")
ppt = p.Presentations.Open(path)
ppt.ExportAsFixedFormat(pdf_path, 2, PrintRange=None)
ppt.Close()
p.Quit()
except Exception as e:
pass
def word_pdf(path):
# Word转pdf
pdf_path = path.replace('doc', 'pdf')
w = client.CreateObject("Word.Application")
doc = w.documents.Open(path)
doc.ExportAsFixedFormat(pdf_path, 17)
doc.Close()
w.Quit()
def excel_pdf(self, path):
# Excel转pdf
pdf_path = path.replace('xls', 'pdf')
xlApp = client.CreateObject("Excel.Application")
books = xlApp.Workbooks.Open(path)
books.ExportAsFixedFormat(0, pdf_path)
xlApp.Quit()
word转html
import win32com.client as wc # doc转docx用
from pydocx import PyDocX # docx转html用
'''
doc文件转docx文件
fullpath:路径+文件名(不带后缀)
如:D:\test\文件1
'''
def doc2docx(fullpath):
word = wc.Dispatch("WORD.Application") # 启动word进程
word.displayalerts=0 # 不警告
word.visible=0 # 不显示
#print(fullpath)
doc = word.documents.Open(fullpath + '.doc') # D:\test\文件名1.doc
doc.SaveAs(fullpath,12, False, "", True, "", False, False, False, False) #转为docx
doc.Close()
word.Quit()
'''
docx转html
fullpath:路径+文件名(不带后缀)
如:D:\test\文件2
'''
def docx2html(fullpath):
html = PyDocX.to_html(fullpath + ".docx") # 转为html,如:D:\test\文件2.docx
f = open(fullpath + ".html", 'w', encoding="utf-8") # 变为如:D:\test\文件2.html
f.write(html)
f.close()



