使用docx解析字典格式的表数据

2024-10-05 14:26:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用pythondocx模块解析一组特定的表数据。在

表数据如下所示 enter image description here

我需要以键值格式检索“Authorities”和相应的“version”,以便可以使用这些数据进行进一步处理。在

如果我使用-

d = OrderedDict(zip(table.cell(rowNo, 0).text, table.cell(rowNo, 2).text))

这给了我orderedDictionary,但我不能使用d['Juno']访问这些值 我希望能给我4.5.6

^{pr2}$

我在解析后得到以下格式的数据-

Juno=4.5.6
Acrux=3.5.6
Mars=5.6.7

Tags: 模块数据textversion格式tablecellzip
1条回答
网友
1楼 · 发布于 2024-10-05 14:26:08

你可以定义一个字典并实现-

from docx import Document

document = Document('myfile.docx')
data = {}
for table in document.tables:
    printTable = False
    rowNo = 0;
    for row in table.rows:
        for cell in row.cells:
            if cell.text == "Table2":
                printTable = False
        if printTable:
            data[table.cell(rowNo, 0).text] = table.cell(rowNo, 2).text
        for cell in row.cells:
            if cell.text == "Authorities":
                printTable = True
        rowNo += 1
print (data)

将以字典格式为您提供所需的数据

相关问题 更多 >