Read From PowerPoint Table in Python?

2019-09-01 10:17发布

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)

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.

2条回答
劳资没心,怎么记你
2楼-- · 2019-09-01 10:52

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:

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
查看更多
一夜七次
3楼-- · 2019-09-01 11:03

This works for me:

    def access_table(): 
            slide = prs.slides[0] #first slide
            table = slide.shapes[2].table # maybe 0..n
            for r in table.rows:
                    s = ""
                    for c in r.cells:
                            s += c.text_frame.text + " | "
                            #to write
                            #c.text_frame.text = "example"
                    print s
查看更多
登录 后发表回答