python-word解析编辑

 

需要安装包pip install python-docx

print(tables[0].rows[0].cells[0].text)

from docx import Document

document = Document('path.docx')
#遍历word每一行
for paragraph in document.paragraphs:
    # 修改段落中的文本
    paragraph.text = '新的文本内容'

tables = document.tables
#遍历每一个表格
for table in tables:
    #遍历表格每一行
    for row in table.rows:
        # 遍历表格每一行内容
        for cell in row.cells:
            data = cell.text
            print(data)
            #修改
            cell.text='文本'

document.save('new_path.docx')

 

posted @ 2024-01-20 23:59  南方的墙  阅读(13)  评论(0编辑  收藏  举报