从Python的PowerPoint表中读取? [英] Read From PowerPoint Table in Python?

查看:321
本文介绍了从Python的PowerPoint表中读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python pptx模块自动更新PowerPoint文件中的值。我可以使用以下代码提取文件中的所有文本:

I am using the python pptx module to automatically update values in a powerpoint file. I am able to extract all the text in the file using the code below:

from pptx import Presentation
prs = Presentation(path_to_presentation)
# text_runs will be populated with a list of strings,
# one for each text run in presentation
text_runs = []
for slide in prs.slides:
  for shape in slide.shapes:
    if not shape.has_text_frame:
      continue
  for paragraph in shape.text_frame.paragraphs:
    for run in paragraph.runs:
      text_runs.append(run.text)

此代码将提取所有文本文件,但无法提取ppt表中的文本,我想更新其中一些值。我试图通过此问题实施一些代码:读取文本值在PowerPoint表中使用pptx?,但是不能。有任何想法吗?

This code will extract all the text in a file but fails to extract text that is in a ppt table and I would like to update some of these values. I have tried to implement some code from this question: Reading text values in a PowerPoint table using pptx? but could not. Any Ideas? Thanks.

推荐答案

您的代码将丢失更多的文本,而不仅仅是表格;例如,它不会看到属于组的形状的文本。

Your code will miss more text than just tables; it won't see text in shapes that are part of groups, for example.

对于表格,您需要做几件事:

For tables, you'll need to do a couple things:

测试形状以查看是否shape的.HasTable属性为true。如果是这样,您可以使用形状的.Table对象提取文本。从概念上讲,非常空代码:

Test the shape to see if the shape's .HasTable property is true. If so, you can work with the shape's .Table object to extract the text. Conceptually, and very aircode:

For r = 1 to tbl.rows.count
   For c = 1 to tbl.columns.count
      tbl.cell(r,c).Shape.Textframe.Text ' is what you're after

这篇关于从Python的PowerPoint表中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆