Extract table from Powerpoint

2019-05-31 09:02发布

I am trying to extract table from a PPT using python-pptx, however, the I am not sure how do I that using shape.table.

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 shape.has_table:
      tbl = shape.table
      rows = tbl.rows.count
      cols = tbl.columns.count

I found a post here but the accepted solution does not work, giving error that count attribute is not available.

How do I modify the above code so I can get a table in a dataframe?

EDIT

Please see the image of the slide below

enter image description here

1条回答
别忘想泡老子
2楼-- · 2019-05-31 10:08

This appears to work for me.


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_table:
            continue    
        tbl = shape.table
        row_count = len(tbl.rows)
        col_count = len(tbl.columns)
        for r in range(0, row_count):
            for c in range(0, col_count):
                cell = tbl.cell(r,c)
                paragraphs = cell.text_frame.paragraphs 
                for paragraph in paragraphs:
                    for run in paragraph.runs:
                        text_runs.append(run.text)

print(text_runs)```





查看更多
登录 后发表回答