gpt4 book ai didi

python-3.x - 如何在python中使用pptx遍历表并编辑信息?

转载 作者:行者123 更新时间:2023-12-04 10:07:03 24 4
gpt4 key购买 nike

如何遍历表并查找和替换文本?我能够浏览如下文本框,但需要能够访问和编辑表格中的信息:

for slide in prs.slides:
for shape in slide.shapes:
if shape.has_text_frame:
j = 0
k = 0
for i in searchList:
#find the search term from the searchList
searchString = searchList[j]
j+=1
#replace the search term from the dataframe
dfIndexName = dfIndex.keys()[k]
k+=1
replaceString = df.at[0, dfIndexName]
if (shape.text.find(searchString))!=1:
text_frame = shape.text_frame
cur_text = text_frame.paragraphs[0].runs[0].text
new_text = cur_text.replace(searchString, str(replaceString))
text_frame.paragraphs[0].runs[0].text = new_text

谢谢!

最佳答案

我不确定你想用 searchList 做什么,但这是访问表格中单元格的方式。

  for slide in prs.slides:
for shape in slide.shapes:
if shape.has_table:
table = shape.table
for cell in table.iter_cells():
# here you can access the text in cell by using
# cell.text
# just remember that the shape object refers to the table in this context not the cell

关于python-3.x - 如何在python中使用pptx遍历表并编辑信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61533543/

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