python word,ppt,excel转pdf(word转html),转图片
装依赖
- 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()
-
- 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()
-
用中转换api
- https://www.yozodcs.com/page/help-interface
-
- import pandas as pd
- import matplotlib.pyplot as plt
- from pandas.plotting import table
-
- # 解决 画图中文 方块问题
- from pylab import mpl
-
- mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 指定默认字体:解决plot不能显示中文问题
- mpl.rcParams['axes.unicode_minus'] = False
- # figsize 指定figure的宽和高,单位为英寸;
- # dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80 1英寸等于2.5cm,A4纸是 21*30cm的纸张
- fig = plt.figure(figsize=(9, 10), dpi=900)
- # frameon:是否显示边框
- ax = fig.add_subplot(111, frame_on=False,)
- # 隐藏x轴 y轴
- ax.xaxis.set_visible(False) # hide the x axis
- ax.yaxis.set_visible(False) # hide the y axis
-
- # 读取excel
- datas = pd.read_excel(r'C:\Users\ht-desktop-001\Desktop\excel.xlsx', engine='openpyxl')
- datas = datas.iloc[:, 0:]
- print(datas)
- # 生成图片
- table(ax, datas, loc='center') # where df is your data frame
- # 保存图片
- plt.savefig('photo.jpg')
-
- 注意如果为xlsx 需要pip install openpyxl
-
- import comtypes.client
- import os
- import glob
-
- if __name__ == '__main__':
- path = r'C:\Users\ht-desktop-001\Desktop\1.pptx'
- pa = r"C:\Users\ht-desktop-001\Desktop\1"
- fileNames = glob.glob(pa + r'\*')
- for fileName in fileNames: # 将pa 文件夹中的文件删除。
- os.remove(fileName)
- powerpoint = comtypes.client.CreateObject("kwpp.Application") # 使用wps的接口
- powerpoint.Visible = 1
- ppt = powerpoint.Presentations.Open(path)
- # 另存为
- ppt.SaveAs(pa + '.jpg', 17)
- # 退出
- ppt.Close()
- powerpoint.Quit()
-