我正在尝试使用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属性不可用。

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

编辑

请参阅下面的幻灯片的图像

python - 从Powerpoint提取表-LMLPHP

最佳答案

这似乎为我工作。


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





关于python - 从Powerpoint提取表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54419118/

10-11 06:48