python-docx是一个word稳定读取、创建、编辑报错的Python 库,注意仅支持Microsoft Word 2007+ (.docx) 文件。
目录
pip install python-docx
需求:创建一个word文档,并添加一段文字“It was a dark and stormy night.”,保存该文档到dark-and-stormy.docx文件中
- from docx import Document
-
- document = Document()
- document.add_paragraph("It was a dark and stormy night.")
- print(document)
需求:读取文档dark-and-stormy.docx,读取文档第一段的文字内容,并输出打印。
- document.save("dark-and-stormy.docx")
-
- document = Document("dark-and-stormy.docx")
- print(document.paragraphs[0].text)
'It was a dark and stormy night.'
功能包含:
- from docx import Document
- from docx.shared import Inches
-
- document = Document()
'Document Title'表示标题内容,0代表级别。
document.add_heading('Document Title', 0)
“A plain paragraph having some ”为添加文字内容,从add_paragraph是指另起一段添加文字,add_run是在目前段落后添加文字,不会另起段落,此处要注意。还可能通过add_run('\n')进行段内换行,也一定要留意。
p = document.add_paragraph('A plain paragraph having some ')
.bold=True表示文字加粗,反之False为不加粗
p.add_run('bold').bold = True
.italic = True表示文字倾斜,反之False为不倾斜
p.add_run('italic.').italic = True
'Heading, level 1'表示标题内容,1代表标题级别。
document.add_heading('Heading, level 1', level=1)
'Intense quote'表示引用内容,style表示引用
document.add_paragraph('Intense quote', style='Intense Quote')
'first item in unordered list'表示列表内容,style表示无序列表
- document.add_paragraph(
- 'first item in unordered list', style='List Bullet'
- )
'first item in ordered list' 表示列表内容,style表示有序列表
- document.add_paragraph(
- 'first item in ordered list', style='List Number'
- )
'monty-truth.png'为图片路径,width为边框尺寸
document.add_picture('monty-truth.png', width=Inches(1.25))
添加一个1行3列的表格
table = document.add_table(rows=1, cols=3)
给表格添加内容
- records = (
- (3, '101', 'Spam'),
- (7, '422', 'Eggs'),
- (4, '631', 'Spam, spam, eggs, and spam')
- )
-
- table = document.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'
- for qty, id, desc in records:
- row_cells = table.add_row().cells
- row_cells[0].text = str(qty)
- row_cells[1].text = id
- row_cells[2].text = desc
通过add_page_break()函数添加分页符
document.add_page_break()
保存新word文档
document.save('demo.docx')
- from docx import Document
- from docx.shared import Inches
-
- document = Document()
-
- document.add_heading('Document Title', 0)
-
- p = document.add_paragraph('A plain paragraph having some ')
- p.add_run('bold').bold = True
- p.add_run(' and some ')
- p.add_run('italic.').italic = True
-
- document.add_heading('Heading, level 1', level=1)
- document.add_paragraph('Intense quote', style='Intense Quote')
-
- document.add_paragraph(
- 'first item in unordered list', style='List Bullet'
- )
- document.add_paragraph(
- 'first item in ordered list', style='List Number'
- )
-
- document.add_picture('monty-truth.png', width=Inches(1.25))
-
- records = (
- (3, '101', 'Spam'),
- (7, '422', 'Eggs'),
- (4, '631', 'Spam, spam, eggs, and spam')
- )
-
- table = document.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'
- for qty, id, desc in records:
- row_cells = table.add_row().cells
- row_cells[0].text = str(qty)
- row_cells[1].text = id
- row_cells[2].text = desc
-
- document.add_page_break()
-
- document.save('demo.docx')
🌷🌷🍀🍀🌾🌾🍓🍓🍂🍂🙋🙋🐸🐸🙋🙋💖💖🍌🍌🔔🔔🍉🍉🍭🍭🍋🍋🍇🍇🏆🏆📸📸⛵⛵⭐⭐🍎🍎👍👍🌷🌷