1.使用 Presentation和 Document,需要下载的包如下
pip install python-pptx
pip install python-docx
2.PPT中有表格的情况,读取方式如下:
from pptx import Presentation
from docx import Document
wordfile = Document()
# 给定ppt文件所在的路径
filepath = r'**.pptx'
pptx = Presentation(filepath)
# 遍历ppt文件的所有幻灯片页
for slide in pptx.slides:
# 遍历幻灯片页的所有形状
for shape in slide.shapes:
# 判断形状是否含有文本框,如果含有则顺序运行代码
if shape.has_text_frame:
# 获取文本框
text_frame = shape.text_frame
# 遍历文本框中的所有段落
for paragraph in text_frame.paragraphs:
# 将文本框中的段落文字写入word中
wordfile.add_paragraph(paragraph.text)
if shape.has_table:
# 获取表格
myTable = shape.table
for row in myTable.rows:
for i in range(0, len(myTable.columns)):
tx = row.cells[i].text_frame.text.strip()
# 将文本框中的段落文字写入word中
wordfile.add_paragraph(tx)
save_path = r'***.docx'
wordfile.save(save_path)
3.效果如图