python 操作 word 文件

需求
需要把图1格式的word文件中的部分内容填写到图2的表格中,存在大量需要复制粘贴的操作,用python批量读取处理,省时省力。


图1

图2
准备
使用python-docx类库
word文档与python代码放在同级目录下
获取用户需求标题
代码如下:
结果如下:

官方文档地址:https://python-docx.readthedocs.io/en/latest/(需科学上网)
p.style.name有如下默认值:
Normal
Body Text
Body Text 2
Body Text 3
Caption
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Heading 7
Heading 8
Heading 9
Intense Quote
List
List 2
List 3
List Bullet
List Bullet 2
List Bullet 3
List Continue
List Continue 2
List Continue 3
List Number
List Number 2
List Number 3
List Paragraph
Macro Text
No Spacing
Quote
Subtitle
TOCHeading
Title
获取软件需求功能描述
代码如下:
结果如下:

版权声明: 本文为 InfoQ 作者【wjchenge】的原创文章。
原文链接:【http://xie.infoq.cn/article/0de3552879189086fe941ad49】。文章转载请联系作者。
评论