Skip to content

Text not in the expected cell #970

@zifeiYv

Description

@zifeiYv

Here is my document:
image

I want to extract tuples like("户名", "张三"),("户号", "1102"), but I found I cannot locate values correctly using the following code:

from docx import Document

file = '/paht/to/my_doc.docx'

doc = Document(file)
tables = doc.tables
table = tables[0]  # now, I get the table object successfully
# Intuitively, "户    名" is in the first cell of second row. When I try to find it:
row_num = 1
cell_num = 0
row = table.rows[row_num]
cell = row.cells[cell_num]
print(cell.text)  # will print "张三"
# Actually,"户    名"  is in the last cell of first row:
row_num = 0
row = table.rows[row_num]
cell = row.cells[-1]
print(cell.text)  # will print "户    名"

Could anybody please tell me what's wrong with it:)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions