python读取pptx

安装库

pip install python-pptx

读取文字、表格、图片

from pptx import Presentation
from pptx.shapes.picture import Picture

prs = Presentation("1.pptx")
index = 1
#读取幻灯片的每一页
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

### 如何用 Python 读取 PPTX 文件中的占位符数值 `python-pptx` 是一个用于创建和操作 PowerPoint (.pptx) 文档的强大库。通过该库可以轻松访问幻灯片上的各个元素,包括占位符及其内容。 要读取 PPTX 文件中占位符的数值,可以通过遍历每张幻灯片并检查其形状列表来实现。以下是具体方法: #### 遍历幻灯片和形状 每个幻灯片都包含多个形状对象(Shape),其中一些可能是占位符(Placeholder)。这些占位符通常存储了特定的内容,比如标题、副标题或其他文本框内的数据[^1]。 ```python from pptx import Presentation def read_placeholder_values(presentation_path): presentation = Presentation(presentation_path) placeholder_data = [] for slide in presentation.slides: for shape in slide.shapes: if shape.has_text_frame and shape.placeholder_format: # 判断是否为占位符 text_frame = shape.text_frame content = "\n".join([paragraph.text.strip() for paragraph in text_frame.paragraphs]) placeholder_info = { 'slide_number': presentation.slides.index(slide) + 1, 'placeholder_type': shape.placeholder_format.type, 'content': content } placeholder_data.append(placeholder_info) return placeholder_data # 调用函数 data = read_placeholder_values('example.pptx') for item in data: print(f"Slide {item['slide_number']}, Placeholder Type: {item['placeholder_type']}, Content: {item['content']}") ``` 上述代码会返回所有幻灯片上占位符的信息以及它们所包含的具体文本内容[^4]。 #### 占位符类型的判断 在 `shape.placeholder_format.type` 中可以获得占位符的类型,例如标题 (TITLE) 或子标题 (SUBTITLE)。这有助于进一步筛选目标占位符的数据。 --- ### 注意事项 如果某些占位符嵌套有表格或图表,则需要额外处理这些复杂结构的对象。对于表格的情况,可参考如下方式提取单元格内容[^2]: ```python if shape.shape_type == MSO_SHAPE_TYPE.TABLE: table = shape.table rows, cols = table.rows, table.columns for row_idx, row in enumerate(table.rows): for col_idx, cell in enumerate(row.cells): print(f"Cell ({row_idx},{col_idx}): {cell.text}") ``` 至于调整边框样式等问题,虽然不直接影响占位符值的获取,但在实际应用中有助于美化输出效果[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值