从 Powerpoint 中提取表格

chi*_*n s 4 python powerpoint python-3.x python-pptx

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

我在这里找到了一篇文章,但接受的解决方案不起作用,给出count属性不可用的错误。

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

编辑

请参阅下面的幻灯片图片

在此输入图像描述

小智 6

这似乎对我有用。


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





Run Code Online (Sandbox Code Playgroud)