温馨提示×

python怎么读取word文档内容

小亿
86
2023-12-20 21:30:28
栏目: 编程语言

要读取Word文档的内容,你可以使用python-docx库。首先,需要安装该库,可以使用以下命令进行安装:

pip install python-docx

安装完成后,你可以使用以下代码来读取Word文档的内容:

from docx import Document

# 打开文档
doc = Document('example.docx')

# 读取段落
for para in doc.paragraphs:
    print(para.text)

# 读取表格
for table in doc.tables:
    for row in table.rows:
        for cell in row.cells:
            print(cell.text)

上述代码会打印出Word文档中的所有段落和表格内容。

0