前一期介绍了【NLP】python-docx库简介-CSDN博客,感兴趣的朋友可以先去学习基本知识,再看看本篇基于python-docx库构建不同层级标题和正文字体、段落设置。
目录
🌷🌷正文字体格式设置
这种正文字体设置可以对全文正文内容进行统一设置
d = Document()
# 设置西文字体
d.styles['Normal'].font.name = u'Times New Roman'
# 设置中文字体
d.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
🍋🍋标题字体设置
上面这种方式无法对add_heading()起作用,需要对标题设置字体,需要使用下面这种方式
run = d.add_heading("", level=3).add_run("三级标题")
# 设置西文字体
run.font.name = u'Times New Roman'
# 设置中文字体
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
🍀🍀字号和磅值对应表
Pt(30)设置字体大小,Pt表示磅,与字号对应关系如下
号数 | 磅值 (Pt) |
---|---|
初号 | 42 |
小初 | 36 |
一号 | 26 |
小一 | 24 |
二号 | 22 |
小二 | 18 |
三号 | 16 |
小三 | 15 |
四号 | 14 |
小四 | 12 |
五号 | 10.5 |
小五 | 9 |
六号 | 7.5 |
小六 | 6.5 |
七号 | 5.5 |
八号 | 5 |
🌾🌾段落对齐
段落对齐格式WD_PARAGRAPH_ALIGNMENT
有多个可选项
LEFT
表示左对齐CENTER
表示居中对齐RIGHT
表示右对齐JUSTIFY
表示两端对齐DISTRIBUTE
表示散列对齐
🍂🍂添加段落属性
add_paragraph()
对象可以设置paragraph_format
的多种属性,包括:
- 对齐方式
alignment
- 段落缩进
right_indent
left_indent
first_line_indent
- 段落间距
space_before
space_after
- 行间距
line_spacing
🍌🍌分页设置
分页设置 keep_together
keep_with_next
page_break_before
widow_control
分页设置的属性说明:
keep_together
段中不分页keep_with_next
与下段同页page_break_before
在段落前增加分页符widow_control
孤行控制,控制页面的孤行和孤立行
💖💖样式的优先级问题
run > heading >paragraph > document
🍉🍉独立设置标题样式
举例说明一级标题设置字体段落等,其他等级类似
# 设置1级标题
def heading_1(docx,str_b1):
heading_1 = docx.add_heading('',level=1)#返回1级标题段落对象,标题也相当于一个段落
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
heading_1.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 中心对齐
heading_1.paragraph_format.space_before=Pt(0.5)#设置段前 0.5磅
heading_1.paragraph_format.space_after=Pt(0.5) #设置段后 0.5 磅
heading_1.paragraph_format.line_spacing=1.5 #设置行间距为 1.5
heading_1.paragraph_format.left_indent=Inches(5)#设置左缩进 5英寸
heading_1.paragraph_format.right_indent=Inches(5)#设置右缩进 5 英寸
run=heading_1.add_run(str_b1)
run.font.name=u'Times New Roman' #设置为新罗马
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')#设置为宋体
run.font.size=Pt(16)#设置1级标题文字的大小为“三号” 为16磅
run.font.color.rgb=RGBColor(0,0,0)#设置颜色为黑色
🐸🐸参数说明
- docx:需要写入的word文档,可以通过docx = Document()来定义得到
- add_heading('',level=1):level=1表示添加一级标题
- WD_PARAGRAPH_ALIGNMENT.CENTER:中心对齐,也是居中对齐
- paragraph_format.space_before:设置段前间距,后面的数据表示磅数,0.5表示0.5磅
- paragraph_format.space_after:设置段后间距,后面的数据表示磅数,0.5表示0.5磅
- paragraph_format.line_spacing:设置行间距,后面的数据表示倍数,1.5表示行间距1.5倍
- paragraph_format.left_indent:设置向左缩进,后面的数据表示英寸,5表示5英寸
- paragraph_format.right_indent:设置向右缩进,后面的数据表示英寸,5表示5英寸
- add_run:添加标题文字内容
- run.font.name=u'Times New Roman' 设置英文字体为新罗马
- run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体'):设置中文字体为宋体
- run.font.size:设置1级标题文字的大小为“三号” 为16磅
- run.font.color.rgb=RGBColor(0,0,0):设置颜色为黑色
👍👍独立设置正文样式
设置正文格式时需要用add_paragraph进行操作,而不是add_heading,
python代码如下:
# 设置正文格式
def text(docx, str):
paragrapha = docx.add_paragraph(str)
# 将字体设置为12磅,即小四字体
paragrapha.style.font.size = Pt(12)
from docx.shared import Cm
paragrapha.paragraph_format.first_line_indent = Cm(0.74)#左缩进0.74厘米即缩进2字符
docx.styles['Normal'].font.name = 'Times New Roman'
docx.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
paragrapha.paragraph_format.space_before=Pt(0.5)#设置段前 0.5 磅
paragrapha.paragraph_format.space_after=Pt(0.5) #设置段后 0.5 磅
paragrapha.paragraph_format.line_spacing=1.15 #设置行间距为 1.5
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
paragrapha.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT # 两端左对齐
🍎🍎python代码说明正文字体格式设置
- 字号12磅,即小四字体
- 首行缩进0.74厘米,即缩进2字符
- 英文字体为新罗马
- 中文字体为宋体
🍎🍎python代码说明正文段落格式设置
- 段前0.5磅
- 段后0.5磅
- 行间距1.15倍
- 段落左对齐
整理不易,欢迎一键三连!!!
送你们一条美丽的--分割线--
🌷🌷🍀🍀🌾🌾🍓🍓🍂🍂🙋🙋🐸🐸🙋🙋💖💖🍌🍌🔔🔔🍉🍉🍭🍭🍋🍋🍇🍇🏆🏆📸📸⛵⛵⭐⭐🍎🍎👍👍🌷🌷