一 前言
word的操作也是经常必备的技能之一,今天有空整理了一份给大家!!加油哟!!
公众号:知识追寻者
知识追寻者(Inheriting the spirit of open source, Spreading technology knowledge;)
二 操作Word
安装 python-docx
pip install python-docx
2.1 读取word
word 中的存档格式 为 一个 Document 对象; 每个Document对象 包含许多 Paragraph
对象 和 table
对象;其中 Paragraph 对象 有许多行内元素 Run
对象; Run对象 又有 字体(font),数据(text),颜色(color),字号(size)等对象;table对象比较简单,可以跟excel联系起来,就是由 行(row)列(column)组成;
读取每段内容示例
from docx import Document
path = r'C:mydatageneratorpytt.docx'
# 获取Document对象
doc = Document(path)
# 读取每段
contents = [ paragraph.text for paragraph in doc.paragraphs]
# 读取每行
for line in contents: