Python办公自动化 | 从PPT到Word2

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.效果如图

https://zhuanlan.zhihu.com/p/196384253

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值