亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python-pptx:獲取當前單元格的row_idx和col_idx值

python-pptx:獲取當前單元格的row_idx和col_idx值

小唯快跑啊 2023-08-08 09:50:01
我目前正在開發一個項目,我需要根據單元格的文本內容來定位單元格,然后為緊鄰其右側的單元格初始化一個單元格對象。目前這是我的功能: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))


查看完整回答
反對 回復 2023-08-08
?
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

希望這對其他人有幫助!


查看完整回答
反對 回復 2023-08-08
  • 2 回答
  • 0 關注
  • 308 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號