我知道这是一个重复的问题,但其他答案对我没有用。 我有一个包含一个表格的Word文件。我希望将该表作为我的Python程序输出。我正在使用Python 3.6,并已安装python-docx。以下是我的数据提取代码:
from docx.api import Document
document = Document('test_word.docx')
table = document.tables[0]
data = []
keys = None
for i, row in enumerate(table.rows):
text = (cell.text for cell in row.cells)
if i == 0:
keys = tuple(text)
continue
row_data = dict(zip(keys, text))
data.append(row_data)
print (data)
我希望得到与Word Docx文件完全相同的结果。提前感谢。
python-docx
包的存在联系起来非常有用。 - temoto