我目前正在開發一個項目,我需要根據單元格的文本內容來定位單元格,然后為緊鄰其右側的單元格初始化一個單元格對象。目前這是我的功能:from pptx import Presentationpptx = "path-to-my-pptx"def update_table(): prs = Presentation(pptx) for slide in prs.slides: for shape in slide.shapes: if shape.has_table: for cell in shape.table.iter_cells(): if cell.text == "string-im-looking-for": ### get the current cell's row and col values row = cell.row_idx col = cell.col_idx ### the cell next to it should logically be next_cell = shape.table.cell(row, col+1) 文檔本身在此處提到了 _Cell 對象上的 col_idx 和 row_idx 方法: https ://python-pptx.readthedocs.io/en/latest/user/table.html#a-few-snippets-that-might-be-handy但是我得到: AttributeError: '_Cell' object has no attribute 'row_idx'有人知道我哪里可能出錯嗎?或者如何將 row_idx 和 col_idx 方法添加到 pptx 包的 table.py 文件中的 _Cell 對象?多謝。
2 回答

慕運維8079593
TA貢獻1876條經驗 獲得超5個贊
只需進行不同的迭代來跟蹤循環中的那些:
for row_idx, row in enumerate(table.rows):
for col_idx, cell in enumerate(row.cells):
print("%r is cells[%d][%d]" % (cell, row_idx, col_idx))

MMTTMM
TA貢獻1869條經驗 獲得超4個贊
沒關系!我通過將以下內容插入到tables.py _Cell 對象中找到了修復方法(大約第184行):
@property
def row_idx(self):
return self._tc.row_idx
@property
def col_idx(self):
return self._tc.col_idx
現在可以通過以下方式檢索單元格 x 和 y 坐標:
x = cell.col_idx
y = cell.row_idx
希望這對其他人有幫助!
添加回答
舉報
0/150
提交
取消