使用python-docx完成word操作

Table of Contents

from docx import Document
import os 
os.chdir('D:\docx_test')

打开存储(另存为)文件

document = Document()   # 创建一个空的文档
document.save('test.docx')  # 保存文档
document = Document('test.docx')  # 打开文档 
document.save('test1.docx')  #  另存为文档

增加标题

document = Document('test.docx')  # 打开文档 
document.add_heading('这是一个标题',level= 0)  # 增加一级标题
document.add_heading('这是一个小标题',level= 1)  # 增加二级标题 

增加段落和文字

 # add_paragraph 增加段落 可以用|n和|t等
paragraph1 = document.add_paragraph("这是第1个段落。\n 这里换行",) 
paragraph2 = document.add_paragraph("这是第2个段落") 
# insert_paragraph_before 在段落之前插入新的一段 
paragraph2.insert_paragraph_before('之前插入一段')
# 清除段落
paragraph2.clear()
document.save('test.docx')  # 保存文档

document = Document()   # 创建一个空的文档
paragraph1 = document.add_paragraph()
paragraph1.add_run('\n这是段落的主题内容').italic = True  # 设置斜体 
paragraph1.add_run('\n这是段落的主题内容').bold = True  # 设置粗体
paragraph1.add_run('\n这是段落的主题内容').underline= True  # 设置粗体
document.save('test.docx')  # 保存文档

字体样式设置

from docx.shared import Inches  #定义英尺
from docx.shared import Pt  #定义像素大小
from docx.shared import RGBColor  # 字体颜色
from docx.enum.text import WD_ALIGN_PARAGRAPH  # 对其方式 
from docx.oxml.ns import qn # 导入qn  不导入qn字体不能修改中文字体 
document = Document()   
# 创建自定义段落样式(第一个参数为样式名, 第二个参数为样式类型, 1为段落样式, 2为字符样式, 3为表格样式)
# 如同时设置斜体和粗体,应该使用style
# 字体样式设置
UserStyle1 = document.styles.add_style('UserStyle1', 1)
UserStyle1.font.size = Pt(20) # 设置字体尺寸
UserStyle1.font.color.rgb = RGBColor(216, 11, 123) # 设置字体颜色
UserStyle1.font.name = "Times New Roman" # 设置英文字体
UserStyle1._element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋')  # 设置中文字体 
UserStyle1.font.bold = True 
UserStyle1.font.underline = True

段落样式设置

'alignment':文本对齐方式,
'element',
'first_line_indent':首行缩进,
'keep_together':段中不分页,
'keep_with_next':和下一段保持同一页,
'left_indent':段落到页面左边距距离,
'line_spacing':行间距,
'line_spacing_rule',
'page_break_before':与前端分页,
'part',
'right_indent',
'space_after':段前间距,
'space_before':段后间距,
'tab_stops',
'widow_control'
UserStyle1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.LEFT # 居中文本  LEFT:左对齐 CENTER:居中对齐 RIGHT:右对齐等
UserStyle1.paragraph_format.line_spacing = Pt(30) # 行间距 距离有CM厘米 Pt磅数 和 Inches英尺 
UserStyle1.paragraph_format.first_line_indent = Pt(40) # 首行缩进 如果缩进2字符 可以用字体磅数*2 

paragraph2 = document.add_paragraph('加强贷款资金管理。贷款资金发放、本息回收代扣、止付等关键环节由银行自主决策,指令由银行发起。采用自主支付的,资金应直接发放至借款人银行账户;采用受托支付的,商业银行应当履行受托支付责任,将贷款资金最终支付给符合借款人合同约定用途的交易对象。商业银行应当自主完整保留贷款资金发放、本息回收等账户流水信息,主动加强贷款资金管理,并采取有效措施对贷款用途进行监测,确保贷款资金安全,防范合作机构截留、汇集、挪用',style = UserStyle1)
#
document.save('test.docx')  # 保存文档

document中样式修改

文档中的样式库可以在document.styles中查看 这里对默认字体进行修改

document = Document()   
document.styles['Normal'].font.name = u'宋体'
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
document.styles['Normal'].font.size = Pt(20)
document.add_paragraph('新的段落')
document.save('test.docx')  # 保存文档

有序和无序类表

document = Document()   
document.styles['Normal'].font.name = u'宋体'
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
document.styles['Normal'].font.size = Pt(20)
#  无序列表
document.add_paragraph('第一点',style="List Bullet")
document.add_paragraph('第二点',style="List Bullet")
#  有序列表
document.add_paragraph('第一点',style="List Number")
document.add_paragraph('第二点',style="List Number")

document.save('test.docx')  # 保存文档

添加图片

from docx.shared import Cm
document = Document()   
pic = document.add_picture('picture1.jpg',width=Cm(10))  # 添加图片 
document.paragraphs[-1].alignment = WD_ALIGN_PARAGRAPH.CENTER  # 将图片进行居中
document.save('test.docx')  # 保存文档

添加表格

table 有增加行 增加列 布局 自适应 等等操作 

add_column’,
‘add_row’,
‘alignment’,
‘autofit’,
‘cell’,
‘column_cells’,
‘columns’,
‘part’,
‘row_cells’,
‘rows’,
‘style’,
‘table’,
‘table_direction’

document = Document()  
table = document.add_table(rows = 2, cols= 2 )

cell = table.cell(0,0)  # 读取0,0表格
cell.text = 'allen'   # 修改表格文字 
row = table.rows[1]  # 读取第2行 
row.cells[0].text = 'row01'   # 修改表格文字 
row.cells[1].text = 'row02'
document.save('test.docx')  # 保存文档

在表格中操作图片 实现图片的布局

document = Document()  
table = document.add_table(rows = 1, cols= 2, )
table.alignment = WD_ALIGN_PARAGRAPH.CENTER # 表格居中对齐 
cell = table.cell(0,0)  #  定位0,0 
ph =cell.paragraphs[0]  # 定位0,0中第一个段落
ph.alignment  = WD_ALIGN_PARAGRAPH.CENTER # 段落居中对齐 
# 在段落中添加图片 
run = ph.add_run()  
run.add_picture('picture1.jpg', width=Cm(5))

# 在另一个表格中加入图片 实现两个图片比列 
cell = table.cell(0,1)
ph =cell.paragraphs[0]
run = ph.add_run()
run.add_picture('picture2.jpg', width=Cm(5))
ph.alignment  = WD_ALIGN_PARAGRAPH.CENTER
document.save('test.docx')  # 保存文档

表格样式 合并 行高 列宽 文字垂直居中

from docx.enum.table import WD_ALIGN_VERTICAL
document = Document()  
table = document.add_table(rows = 2, cols= 3, style='Table Grid')  # 设置框线
table.rows[0].height=Cm(2)  # 设置行高 
table.cell(0,0).merge(table.cell(0,1))  # 合并第一行第一个和第二个单元格表格 
# 段落中文字居中
table.cell(0,2).paragraphs[0].add_run('1')
table.cell(0,2).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER  
table.cell(0,2).vertical_alignment = WD_ALIGN_VERTICAL.CENTER # 垂直居中
table.cell(0,0).text = "0"
table.cell(0,0).width = Cm(15)  # 设置列宽
# table.cell(0,1).text = "1"
document.save('test.docx')  # 保存文档

页眉页脚 分页

document = Document()  
# 页眉 
header = document.sections[0].header 
ph1 = header.add_paragraph('页眉')
ph1.alignment = WD_ALIGN_PARAGRAPH.CENTER 
# 页脚 
footer = document.sections[0].footer
footer.add_paragraph('页脚')
# 分页 
document.add_paragraph('段落1')
document.add_page_break()
document.add_heading('标题',level=2)
document.save('test.docx')  # 保存文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值