利用python-docx模块,python可以处理word文档。运行pip install python-docx 可以安装该模块。
需要使用时,载入 import docx
python-docx 完整说明文档 : https://python-docx.readthedocs.io/en/latest/
结构:document对象表示整个文档,包含一个Paragraph对象的列表,表示文档中的段落。
每个Paragragh对象都包含一个Run对象的列表。
需要使用时,载入 import docx
python-docx 完整说明文档 : https://python-docx.readthedocs.io/en/latest/
结构:document对象表示整个文档,包含一个Paragraph对象的列表,表示文档中的段落。
每个Paragragh对象都包含一个Run对象的列表。
一 获取文本。 具体功能实现看如下代码:
## pip install python-docx import docx ## 从word文档中获取文本 def getText(filename): doc = docx.Document(filename) print(len(doc.paragraphs) ) # 返回段落数 print(len(doc.paragraphs[1].runs) ) # 第二个段落的run对象数 print(doc.paragraphs[1].runs[0].text) # 第二个段落中第一个run对象的文本内容 fullText = [] for para in doc.paragraphs: fullText.append(para.text) # fullText.append(' ' + para.text) # 实现每段缩进 return '\n'.join(fullText) # return '\n\n'.join(fullText) # 段落之间增加空行 print(getText('demo.docx'))
二 样式设置。
python-docx 使用样式文档: https://python-docx.readthedocs.org/en/latest/user/styles.html
代码段如下:
## 样式设置 import docx doc = docx.Document('demo.docx') print(doc.paragraphs[0].text) print(doc.paragraphs[0].style) doc.paragraphs[0].style = 'Normal' # 改变段落样式 print(doc.paragraphs[1].text) print( (doc.paragraphs[1].runs[0].text,doc.paragraphs[1].runs[1].text, doc.paragraphs[1].runs[2].text,doc.paragraphs[1].runs[3].text) ) doc.paragraphs[1].runs[1].underline = True # 设置run对象样式 doc.paragraphs[1].runs[3].underline = True doc.save('result.docx')
三 写入docx文档
写入段落使用add_paragraph()方法, 在已有段落末尾添加文本,使用paragraph对象的add_run()方法。
这两个方法都接受第二个参数,是表示样式的字符串。 具体见如下代码:
## 写入DOCX文档 import docx doc = docx.Document() doc.add_paragraph('Hello world') # 添加段落1 paraObj = doc.add_paragraph('This is a second paragraph.') # 添加段落2 doc.add_paragraph('This is a third paragraph.') # 添加段落3 paraObj.add_run('This text is being added to the second paragraph.') # 段落2中增加一段 doc.add_paragraph('Hello world','Title') # 带样式添加段落 doc.save('result.docx')四 添加标题
import docx doc = docx.Document() doc.add_heading('Header 0',0) doc.add_heading('Header 1',1) doc.add_heading('Header 2',2) doc.add_heading('Header 3',3) doc.add_heading('Header 4',4) doc.save('result.docx')
五 添加换行、换页符
## 添加换行符、换页符 import docx doc = docx.Document() doc.add_paragraph('This is the first page!') doc.paragraphs[0].runs[0].add_break() # 插入换行符,换行 doc.add_paragraph('This is the first page! Next next line') doc.add_page_break() # 插入分页符,换页 doc.add_paragraph('This is the second page!') doc.save('result.docx')
六 添加图像
## 添加图像 import docx doc = docx.Document() doc.add_picture('zophie.png',width=docx.shared.Inches(1),height=docx.shared.Cm(4)) doc.save('result.docx')
七 添加表格
## 添加表格 import docx doc = docx.Document() table = doc.add_table(rows=1, cols=3) # 添加一个表格,每行三列 hdr_cells = table.rows[0].cells # 表格第一行的所含有的所有列数 hdr_cells[0].text = 'Qty' # 第一行的第一列,给这行里面添加文字 hdr_cells[1].text = 'Id' hdr_cells[2].text = 'Desc' recordset = [] for item in recordset: row_cells = table.add_row().cells # 这是在这个表格第一行 (称作最后一行更好) 下面再添加新的一行 row_cells[0].text = str(item.qty) row_cells[1].text = str(item.id) row_cells[2].text = item.desc doc.save('result.docx')