python-docx字体设置

WORD中字号、磅值,两者具体的对应关系大约如下吧:
字号‘八号’对应磅值5
字号‘七号’对应磅值5.5
字号‘小六’对应磅值6.5
字号‘六号’对应磅值7.5
字号‘小五’对应磅值9
字号‘五号’对应磅值10.5
字号‘小四’对应磅值12
字号‘四号’对应磅值14
字号‘小三’对应磅值15
字号‘三号’对应磅值16
字号‘小二’对应磅值18
字号‘二号’对应磅值22
字号‘小一’对应磅值24
字号‘一号’对应磅值26
字号‘小初’对应磅值36
字号‘初号’对应磅值42

#!/usr/bin/python
# coding=UTF-8

from docx import Document
from docx.shared import RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import  Pt
from docx.oxml import OxmlElement
from docx.oxml.ns import  qn
from docx.shared import Inches
from docx.shared import Cm
#打开文档
document = Document()

#报告标题
head = document.add_heading()
head.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
head.paragraph_format.space_before = Pt(36)
run = head.add_run(u"生成的报告")
run.font.size=Pt(36)
run.font.color.rgb=RGBColor(0x00,0x00,0x00) #标题颜色
document.styles['Normal'].font.name = u'Times New Roman' #设置西文字体
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体') #设置中文字体使用字体2->宋体
#添加分页
document.add_page_break()

#二级标题
head2 = document.add_heading(level=2)
head2.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
run = head2.add_run(u'报告分析')
#run.font.name=u'宋体'
run.font.size=Pt(21)
run.font.color.rgb=RGBColor(14,106,85)
#段落后行距
head2.paragraph_format.space_after = Pt(30)

#二级级标题段落
#document.styles['Normal'].font.name = u'宋体'
p = document.add_paragraph()
run = p.add_run(u'''    段落1
    段落2''')
run.font.color.rgb = RGBColor(0, 0, 0)
run.font.size = Pt(15)
#单倍行距
p.paragraph_format.line_spacing = Pt(30)
#段落后行距
p.paragraph_format.space_after = Pt(30)

#三级标题
head3 = document.add_heading(level=3)
run = head3.add_run(u'分析对象:')
#run.font.name=u'宋体'
run.font.size=Pt(18)
run.font.color.rgb=RGBColor(0,0,0)
#段落后行距
head2.paragraph_format.space_after = Pt(30)
#基本信息
document.add_picture('./image/1.png', width=Cm(15.24)) #设置图片大小,这里用的是cm厘米,比较直观吧,反正我从来不懂Inches跟cm之间怎么换,也不知他有多大

#设置中文字体
#run = paragraph.add_run(u'设置中文字体,')
#run.font.name=u'宋体'
#r = run._element
#r.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
#设置粗体小四
paragraph = document.add_paragraph()
run = paragraph.add_run(u'MD5:  ')
run.bold = True
run.font.size=Pt(12)
paragraph = document.add_paragraph()
run = paragraph.add_run(u'TYPE:  ')
run.bold = True
run.font.size=Pt(12)
paragraph = document.add_paragraph()
run = paragraph.add_run(u'文件名称:  ')
run.bold = True
run.font.size=Pt(12)
paragraph = document.add_paragraph()
run = paragraph.add_run(u'文件大小:  ')
run.bold = True #加粗
run.font.size=Pt(12) #小四

#报告1
document.add_picture('./image/2.png', width=Cm(15.24))
paragraph = document.add_paragraph()
run = paragraph.add_run(u'判定:  ')
run.bold = True
run.font.size=Pt(12)

#报告2
document.add_picture('./image/3.png', width=Cm(15.24))
paragraph = document.add_paragraph()
run = paragraph.add_run(u'报告2:  ')
run.bold = True
run.font.size=Pt(12)

#分析总结
document.add_picture('./image/4.png', width=Cm(15.24))
paragraph = document.add_paragraph()
run = paragraph.add_run(u'分析结果:  ')
run.bold = True
run.font.size=Pt(12)

#检测结果
document.add_picture('./image/5.png', width=Cm(15.24))
paragraph = document.add_paragraph()
run = paragraph.add_run(u'检测结果:  ')
run.bold = True
run.font.size=Pt(12)

#关系
document.add_picture('./image/6.png', width=Cm(15.24))
paragraph = document.add_paragraph()
run = paragraph.add_run(u'关系:  ')
run.bold = True #加粗
run.font.size=Pt(12)#小四

#关键字符串
document.add_picture('./image/7.png', width=Cm(15.24))
paragraph = document.add_paragraph()
run = paragraph.add_run(u'关键字符串:  ')
run.bold = True
run.font.size=Pt(12)

head4 = document.add_heading(level=3)
run = head4.add_run(u'附件:')
run.font.size=Pt(18)
run.font.color.rgb=RGBColor(0,0,0)

#注1
head5 = document.add_heading(level=3)
run = head5.add_run(u'注1:')
run.font.size=Pt(15)
run.font.color.rgb=RGBColor(65,186,126)
head5.paragraph_format.line_spacing = Pt(30)

p = document.add_paragraph()
p.paragraph_format.line_spacing = Pt(30)
run = p.add_run(u'''    注解1段落''')
run.font.color.rgb = RGBColor(0, 0, 0)
run.font.size = Pt(12)

#注2
head6 = document.add_heading(level=3)
run = head6.add_run(u'注2:')
run.font.size=Pt(15)
run.font.color.rgb=RGBColor(65,186,126)
head6.paragraph_format.line_spacing = Pt(30)

p = document.add_paragraph()
p.paragraph_format.line_spacing = Pt(30)
run = p.add_run(u'''    注解2段落''')
run.font.color.rgb = RGBColor(0, 0, 0)
run.font.size = Pt(12)

#保存文件
document.save('demo.docx')
# 新建一个段落,增加一段文字
run_2 = doc.add_paragraph().add_run('这爱不落幕,忘了心事的国度,'
                                    '你所在之处,孤单都被征服,'
                                    '铁铸的招牌,错落着就像,'
                                    '一封封城市献给天空的情书,'
                                    '当街灯亮起,Havana漫步,'
                                    '这是世上最美丽的那双人舞。')
# 设置字体格式
run_2.font.name = 'Times New Roman'  # 注:这个好像设置 run 中的西文字体
# 设置中文字体
# 需导入 qn 模块
from docx.oxml.ns import qn
run_2.font.element.rPr.rFonts.set(qn('w:eastAsia'),'楷体')
# 设置字体大小
run_2.font.size = Pt(14)
# 设置加粗
run_2.font.bold = True
# 设置斜体
run_2.font.italic = True
# 设置字体颜色 需导入 rgb 颜色模块
from docx.shared import RGBColor
run_2.font.color.rgb = RGBColor(255,55,55) # 红色
# 设置下划线
# run_2.font.underline = True
# 设置轮廓线
# run_2.font.outline = True
# 设置阴影
# run_2.font.shadow = True
# 删除线
# run_2.font.strike = True
# 双删除线
# run_2.font.double_strike = True
# 设置下标
# run_2.font.subscript = True
# 设置上标
# run_2.font.superscript = True

你可能感兴趣的:(python-docx字体设置)