仅从最左边的单元格读取文本

read text from left most cell only

我在一个 docx 文件中有很多表格,我正在尝试从第一列的单元格中获取文本。

我有这段代码可以搜索整行

for table in doc.tables:
    for row in table.rows:
        for cell in row.cells:
            for paragraph in cell.paragraphs:
                result = ReqRegex.search(paragraph.text)
                if result:
                    file.write(result.group()+"\n")

但我正在尝试将其更改为仅检查第一列

for table in doc.tables:
    for column in table.columns:
        for cell in table.column_cells(0):
            for paragraph in cell.paragraphs:
                result = ReqRegex.search(paragraph.text)
                if result:
                    file.write(result.group()+"\n")

你能告诉我我可以改变什么来使这段代码工作吗?

我不熟悉 python-docx,但按照正常的 python 规则,这应该可以

for table in doc.tables:
    for row in table.rows:
            for paragraph in row.cells(0).paragraphs:
                result = ReqRegex.search(paragraph.text)
                if result:
                    file.write(result.group()+"\n")

终于解决了我的问题。也许对某人有用

for table in doc.tables:
    rowNo = 0
    for row in table.rows:
        columnNo = 0
        for cell in row.cells:
            columnNo += 1
            for paragraph in cell.paragraphs:
                result = ReqRegex.search(paragraph.text)
                if columnNo == 1:
                    print(cell.text)
                    if result:
                        file.write(result.group()+"\n")
        rowNo += 1