安装
- pip install python-docx (python3)
- easy_jinstall python-docx (python2)
docx文档基本操作
写在前面
from docx import Document
document = Document()
写入段落标题
paragraph = document.add_paragraph('这是我的文本段落1111……')
piror_para = paragraph.insert_paragraph_before('这是我的文本段落00000……')
document.add_heading('测试文本01')
document.add_heading('测试文本02',level=2)
添加分页
from docx import Document
document = Document()
document.add_page_break()
document.save('text1.docx')
添加表格
from docx import Document
document = Document()
table = document.add_table(rows=2,cols=2)
cell = table.cell(0,1)
cell.test = 'name'
document.save('test2_table.docx')
获取行写入值
row = table.rows[1]
row.cells[0].text = '我是第二行第一列'
row.cells[1].text = '我是第二行第二列'
读取表格值
for row in table.rows:
for cell in row.cells:
print(cell.text)
row_count = list(table.rows)
document.save('test2_table.docx')
col_count = len(table.columns)
docx添加表格数据基本操作
表格数据的构建
datas = (
(7,'1024','Plush kittens'),
(3,'2042','Furbees'),
(1,'1288','French poodle collars deluxe'),
)
添加表头数据
table = document.add_table(1,3)
heading_cells = table.rows[0].cells
heading_cells[0].text = 'Qty'
heading_cells[1].text = 'SKU'
heading_cells[2].text = 'Description'
实现在表头下添加构建的数据
for data in datas:
cells = table.add_row().cell
cell[0].text = str(data[0])
cell[1].text = data[1]
cell[2].text = data[2]
docx图片基本操作
添加图片
from docx import Document
document = Document()
document.add_picture('测试.jpg')
设置图片大小
from docx.shared import Inches
document.add_picture('测试.jpg',width=Inches(1.0))
document.save('test_image.docx')
docx设置段落样式基本操作
from docx import Document
document = Document()
document.add_paragraph('我是段落1111',style='List Bullet')
document.save('test_style.docx')
lis_para = document.add_paragraph('我是段落22222222')
lis_para.style = 'List Bullet'
para3 = document.add_paragraph('我是段落33333')
para3.add_run('我是添加在段落3333后面的内容')
docx设置字体样式基本操作
设置加粗
para4 = document.add_paragraph('我是段落4444444')
run = para4.add_run('我是追加在para4后边的内容')
run.bold = True
para4.add_run('我是追加在run后面的字体').bold = True
设置倾斜
para5 = document.add_paragraph('我是段落555555')
para5.add_run('我是para5后边的内容','Emphasis')
para6 = document.add_poaragraph('我是段落666666')
run = para6.add_run('我是添加在para6后边的内容')
run.style = 'Emphasis'
docx打开文档并另存为
from docx import Document
document = Document()
document.save('test.docx')
document = Document('existing-document-file.docx')
document.save('new-file-name.docx')
docx设置段落格式
设置段落对齐方式
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
document = Document()
para = document.add_paragraph('我是段落11111')
para_format = para.paragraph_format
para_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
缩进
from docx.shared import Inches
para2 = document.add_paragraph('这是段落22222')
para2_format = para2.paragraph_format
from docx.shared import Pt
para2_format.left_indent = Inches(0.5)
para3 = document.add_paragraph('我是段落33333')
para3_format = para3.paragraph_format
para3_format.right_indent = Pt(24)
para4 = document.add_paragraph('以梦为马,指把自己的梦想作为前进方向和动力。马,在这里是指动力,亦有希望的意思。以马这种强健美好的动物作为希望的载体,承载内心的理念和梦想,为未来的人生提供内在支持。')
para4_format =para4.paragraph_format
para4_format.first_Line_index = Inches(0.25)
设置段落间距
para4_format_space_before = Pt(100)
para_format.space_after = Pt(50)
设置行间距
para4_format.line.spacing = Pt(40)
para4_format.keep_together = True
para4_format.keep_with_next = True
para4_format.window_control = True
document.save('text5_align.docx')