gpt4 book ai didi

python - 从 Powerpoint 中提取表格

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:28 25 4
gpt4 key购买 nike

我正在尝试使用 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

最佳答案

这似乎对我有用。


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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com