从Powerpoint提取表

时间:2019-01-29 10:37:07

标签: python python-3.x powerpoint python-pptx

我正在尝试使用python-pptx从PPT中提取表格,但是,我不确定如何使用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

我找到了一条帖子here,但是接受的解决方案不起作用,出现错误,提示count属性不可用。

如何修改上面的代码,以便可以在数据框中获取表?

编辑

请在下面查看幻灯片的图像

enter image description here

1 个答案:

答案 0 :(得分:0)

这似乎对我有用。


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)```