0
点赞
收藏
分享

微信扫一扫

【Python】读取Word获得数据 -操作Excel、Word、CSV(6)(保姆级图文注释+测试代码+api例程)

目录


API说明:

创建有标题的 Word例程

读取已有的 Word 文件,只会得到文字!

# @Time    : 2022/1/11 18:59
# @Author  : 南黎
# @FileName: 2.6读取word.py
# 引入库
from docx import Document

# 打开文档1
doc1 = Document('创建章节和段落.docx')

# 读取每段内容
pl = [ paragraph.text for paragraph in doc1.paragraphs]

print('###### 创建章节和段落.docx的内容 ######')
# 输出读取到的内容
for i in pl:
    print(i)

# 打开文档2
doc2 = Document('项目列表.docx')

print('\n###### 项目列表.docx的内容 ######')

pl2 = [ paragraph.text for paragraph in doc2.paragraphs]

# 输出读取到的内容
for j in pl2:
    print(j)

# 读取表格材料,并输出结果
tables = [table for table in doc2.tables]
for table in tables:
    for row in table.rows:
        for cell in row.cells:
            print (cell.text,end='  ')
        print()
    print('\n')

章节和段落的内容
在这里插入图片描述
项目列表的内容
在这里插入图片描述
程序运行后读取到的内容
在这里插入图片描述

总结

大家喜欢的话,给个👍,点个关注!继续跟大家分享敲代码过程中遇到的问题!

所有文件已经上传至码云

https://gitee.com/miao-zehao/python_to_-excel-and-word-and-csv/tree/master/

在这里插入图片描述
在这里插入图片描述


举报

相关推荐

0 条评论