python实现ppt内容输出为word形式

一、只输出文本内容

from pptx import Presentation
from docx import Document

wordfile = Document()
# 给定ppt文件所在的路径
filepath = r"C:\Users\18703\Desktop\智家经分\智家经分v2.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)
#word文档存放的路径
save_path =  r"C:\Users\18703\Desktop\智家经分\智家经分.docx"
wordfile.save(save_path)

二、输出文本、表格、图片内容

import pandas as pd
from pptx import Presentation
from pptx.shapes.picture import Picture

prs = Presentation(r"C:\Users\18703\Desktop\智家经分\智家经分v2.pptx")#这是你ppt的路径
index = 1
#读取幻灯片的每一页
wordfile = Document()
for slide in prs.slides:
    # 读取每一板块
    for shape in slide.shapes:
        # print(dir(shape))
        #是否有文字框
        if shape.has_text_frame:
            #读文字框的每一段落
            for paragraph in shape.text_frame.paragraphs:
                if paragraph.text:
                    # 输出段落文字,也有一些属性,可以用dir查看
                    # print(dir(paragraph))
                    print(paragraph.text)
        #是否有表格
        elif shape.has_table:
            one_table_data = []
            for row in shape.table.rows:  # 读每行
                row_data = []
                for cell in row.cells:  # 读一行中的所有单元格
                    c = cell.text
                    row_data.append(c)
                one_table_data.append(row_data)  # 把每一行存入表
            #用二维列表输出表格行和列的数据
                print(one_table_data)
        # 是否有图片
        elif isinstance(shape, Picture):
            #shape.image.blob:二进制图像字节流,写入图像文件
            with open(f'{index}.jpg', 'wb') as f:
                f.write(shape.image.blob)
                index += 1
            print(f)
            

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rubyw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值