Python3处理文档_word文档(二)
添加分页符
你想要下一个文本在一个单独的页面,即使你所在的一个不是满的。“hard”分页符可以做到这一点:
document.add_page_break()
doc.add_heading('扫描报告', 0)
doc.add_page_break()
设置标题格式
from docx import Document
from docx.shared import RGBColor #设置字体颜色
from docx.shared import Pt #设置字体大小
from docx.oxml.ns import qn##qn#设置中文字体
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Cm
doc=Document()
Head = doc.add_heading("", level=0) # 这里不填标题内容
run = Head.add_run('道德经')
run.font.name = 'Times New Roman'
run.font.name = u'宋体'
run.font.size = Pt(22)
run.font.color.rgb = RGBColor(255,255, 0)
run.bold = True
doc.save('1.docx')
接下来定义一个标题的样式
def head_style(input_text,level,size):
head=doc.add_heading("",level=level)
run=head.add_run(input_text)
run.font.name = 'Times New Roman'
run.font.size = Pt(size)
run.font.color.rgb = RGBColor(255, 255, 0)
run.bold = True
运行一下整体的代码:
from docx import Document
from docx.shared import RGBColor #设置字体颜色
from docx.shared import Pt #设置字体大小
from docx.oxml.ns import qn##qn#设置中文字体
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Cm
doc=Document()
Head = doc.add_heading("", level=0) # 这里不填标题内容
run = Head.add_run('道德经')
run.font.name = 'Times New Roman'
run.font.name = u'宋体'
run.font.size = Pt(22)
run.font.color.rgb = RGBColor(255,255, 0)
run.bold = True
def head_style(input_text,level,size):
head=doc.add_heading("",level=level)
run=head.add_run(input_text)
run.font.name = 'Times New Roman'
run.font.size = Pt(size)
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run.font.color.rgb = RGBColor(255, 255, 0)
run.bold = True
input_text1='标题一'
head_style(input_text1,1,22)
input_text2='标题二'
head_style(input_text2,1,22)
input_text3='标题三'
head_style(input_text3,1,22)
doc.save('1.docx')
设置缩进和对齐
这里还用了昨天的段落样式
paragraph_format_p4 = p4_1.paragraph_format # 获取段落的格式属性
# paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 水平对齐设为居中
paragraph_format_p4.first_line_indent = Cm(0.74) # 首行缩进0.74厘米,即2个字符
from docx import Document
from docx.shared import RGBColor #设置字体颜色
from docx.shared import Pt #设置字体大小
from docx.oxml.ns import qn##qn#设置中文字体
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Cm
doc=Document()
Head = doc.add_heading("", level=0) # 这里不填标题内容
run = Head.add_run('道德经')
run.font.name = 'Times New Roman'
run.font.name = u'宋体'
run.font.size = Pt(22)
run.font.color.rgb = RGBColor(255,255, 0)
run.bold = True
def create_style(document, style_name, style_type, font_size=-1, font_color=None, font_name=None, align=None):
"""
创建一个样式
:param align:
:param document:
:param style_name: 样式名称
:param style_type: 样式类型,1:段落样式, 2:字符样式, 3:表格样式
:param font_name:
:param font_color:
:param font_size:
:return:
"""
if font_color is None:
font_color = []
# 注意:必须要判断样式是否存在,否则重新添加会报错
style_names = [style.name for style in document.styles]
if style_name in style_names:
# print('样式已经存在,不需要重新添加!')
return
font_style = document.styles.add_style(style_name, style_type)
# 字体大小
if font_size != -1:
font_style.font.size = Pt(font_size)
# 字体颜色
font_style.font.color.rgb = RGBColor(0,0,0)
# 对齐方式
# 注意:段落、表格才有对齐方式
if style_type != 2 and align:
font_style.paragraph_format.alignment = align
# font_style.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
# font_style.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
# font_style.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
# 字体名称
if font_name:
font_style.font.name = font_name
font_style.font.name = 'Times New Roman'
# 中文字体名称
font_style._element.rPr.rFonts.set(qn('w:eastAsia'), font_name)
##这里实际上是font_style._element
##font_style._element 再.rPr.rFonts.set
return font_style
def head_style(input_text,level,size):
head=doc.add_heading("",level=level)
run=head.add_run(input_text)
run.font.name = 'Times New Roman'
run.font.size = Pt(size)
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run.font.color.rgb = RGBColor(255, 0, 0)
run.bold = True
input_text1='标题一'
head_style(input_text1,1,22)
p4 = create_style(document=doc, style_name="style2", style_type=1, font_size=12, font_name='宋体')
p4_1 = doc.add_paragraph('这是首行缩进两个字符。', p4)
paragraph_format_p4 = p4_1.paragraph_format # 获取段落的格式属性
# paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 水平对齐设为居中
paragraph_format_p4.first_line_indent = Cm(0.74) # 首行缩进0.74厘米,即2个字符
p4 = create_style(document=doc, style_name="style3", style_type=1, font_size=12, font_name='宋体')
p4_1 = doc.add_paragraph('这是水平居中。', p4)
paragraph_format_p4 = p4_1.paragraph_format # 获取段落的格式属性
paragraph_format_p4.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 水平对齐设为居中
#paragraph_format_p4.first_line_indent = Cm(0.74) # 首行缩进0.74厘米,即2个字符
input_text2='标题二'
head_style(input_text2,1,22)
input_text3='标题三'
head_style(input_text3,1,22)
doc.save('1.docx')
运行结果:
插入图片:
doc.add_picture('god_mellon.jpg')
doc.add_picture('god_mellon.jpg', width=Cm(4.0)) # 宽度为4cm
运行结果:
明天再搞
我公众号大家可以关注支持一哈
主要记录一些网络安全和Python的学习。
作者:那酒不要留
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!知识源于分享!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!