Python设置Word全局样式和文本样式的示例代码

来自:网络
时间:2022-05-14
阅读:
目录

上一章节我们学习了如何生成 word 文档以及在文档行中添加各种内容,今天我们基于上一章节的内容进行添砖加瓦 —> 对内容进行各种样式的设置,让其能够看起来更加的美观。

全局样式的定义

通过全局样式的设置,可以使得 word 全文都可以继承这样的样式效果:

使用方法:

style = document_obj.styles['Normal']

通过 Document 对象调用 styles 对象集,通过中括号的方式选择全局样式,获得 样式对象 。

for style in document_obj.styles:	# 通过 for 循环可以查看 styles 对象集
    print(style)

styles 对象集如下:

Python设置Word全局样式和文本样式的示例代码

全局定义的基本样式举例:

字体:style.font.name = '微软雅黑'

字体颜色:style.font.color.rgb = RGBColor(255, 0, 0)

通过 from docx.shared import RGBColor 调用 docx 包的三原色模块

字体大小:style.font.size = Pt(20)

通过 from docx.shared import Pt 调用 docx 包的字体大小设置模块

代码示例如下:(在上一章节的代码基础上进行 全局样式的代码演示)

# coding:utf-8

from docx import Document
from docx.shared import Inches, RGBColor, Pt

doc = Document()

style = doc.styles['Normal']    # 使用标准样式
style.font.name = '微软雅黑'     # 使用 "微软雅黑" 字体
style.font.color.rgb = RGBColor(255, 0, 0)      # 使用红色作为字体颜色
style.font.size = Pt(25)

title = doc.add_heading('this is title', 1)    # 添加 word 文件的 title 标题
title.add_run('\n - 测试版本')      # 针对 title 标题进行内容追加(换行)

para = doc.add_paragraph('这是 \"test.docx\" 文件的第一行段落')
para.add_run('\n这是 \"test.docx\" 文件追加的的第二行段落')

image = doc.add_picture('test_image.png', width=Inches(3), height=Inches(1.5))      # 添加图片

table_title = ['name', 'age', 'sex']    # 定义表格的第一行的标题
table = doc.add_table(rows=1, cols=3)   # 定义表格的行数、列数
table_cells = table.rows[0].cells       # 将 table_title 的每列的名称写入表格
table_cells[0].text = table_title[0]
table_cells[1].text = table_title[1]
table_cells[2].text = table_title[2]

data = [            # 定义 data 的内容,准备将其追加写入表格
    ('Neo', '18', 'man'),
    ('Adem', '17', 'man'),
    ('Lily', '18', 'women')
]

for i in data:      # 利用 for 循环将 data 追加写入表格
    row_cells = table.add_row().cells
    row_cells[0].text = i[0]
    row_cells[1].text = i[1]
    row_cells[2].text = i[2]

doc.add_page_break()        # 添加 word 文件的分页
title = doc.add_heading('this is page_2 title', 1)    # 添加 word 文件的第二分页的 title 标题

doc.save('test.docx')

运行结果如下:

Python设置Word全局样式和文本样式的示例代码

Python设置Word全局样式和文本样式的示例代码

文本样式的定义

标题与段落

从上面的截图可以看出,当我们设置全局字体的颜色和大小的时候,只有段落收到了影响,而标题未受影响,这就引出了文本的样式。

字体:

obj.font.name = "微软雅黑" 这里的 obj 就表示的是 标题与段落的对象 ;与设置全局字体的方式一致。

字体颜色:

obj.font.color.rgb = RGBColor(255, 0, 0)

字体大小:

obj.font.size = Pt(20)

标题居中:

obj.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER 需要导入:from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

PS:除了居中之外,还可以居左、居右;left 或者 right

字体斜体:

obj.italic = True

为 True 是斜体;为 False 则是正常字体

字体粗体:

obj.blod = True

为 True 是粗体;为 False 则是正常字体

字体下划线:

obj.underline = True

为 True 是增加下划线;为 False 则是正常字体

代码示例如下:

# coding:utf-8

from docx import Document
from docx.shared import Inches, RGBColor, Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

doc = Document()

style = doc.styles['Normal']    # 使用标准样式
style.font.name = '微软雅黑'     # 使用 "微软雅黑" 字体
# style.font.color.rgb = RGBColor(255, 0, 0)      # 使用红色作为字体颜色
style.font.size = Pt(14)

title = doc.add_heading('', 0)      # 添加 word 文件的 title 标题;(需要注意的是,这里第一行的标题是不能设置为斜体等类型的)
                                    # 若想要将标题设置为斜体,需在这一行标题内容为空,然后针对追加内容写入标题设置为斜体
title.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER      # 标题居中
title.style.font.size = Pt(20)
title_run = title.add_run('this is title\n测试版本')      # 针对 title 标题进行内容追加(换行)
title_run.italic = True                        # 将追加的内容转为斜体字
title_run.blod = True                          # 将追加的内容转为粗体字
title_run.underline = True
# print(dir(title))       # 通过 dir 函数查看当前 title 标题可以使用的更多有趣的函数

para = doc.add_paragraph('这是 \"test.docx\" 文件的第一行段落')
para.add_run('\n这是 \"test.docx\" 文件追加的的第二行段落').italic = True    # 将第二行段落设置为斜体
para.add_run('\n这是 \"test.docx\" 文件追加的的第三行段落').blod = True      # 将第三行段落设置为粗体
para.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER     # 将段落设置为居中显示
print(dir(para))       # 通过 dir 函数查看当前 para 段落可以使用的更多有趣的函数

image = doc.add_picture('test_image.png', width=Inches(3), height=Inches(1.5))      # 添加图片

table_title = ['name', 'age', 'sex']    # 定义表格的第一行的标题
table = doc.add_table(rows=1, cols=3)   # 定义表格的行数、列数
table_cells = table.rows[0].cells       # 将 table_title 的每列的名称写入表格
table_cells[0].text = table_title[0]
table_cells[1].text = table_title[1]
table_cells[2].text = table_title[2]

data = [            # 定义 data 的内容,准备将其追加写入表格
    ('Neo', '18', 'man'),
    ('Adem', '17', 'man'),
    ('Lily', '18', 'women')
]

for i in data:      # 利用 for 循环将 data 追加写入表格
    row_cells = table.add_row().cells
    row_cells[0].text = i[0]
    row_cells[1].text = i[1]
    row_cells[2].text = i[2]

doc.add_page_break()        # 添加 word 文件的分页
title = doc.add_heading('this is page_2 title', 1)    # 添加 word 文件的第二分页的 title 标题

doc.save('test.docx')

运行结果如下:

Python设置Word全局样式和文本样式的示例代码

Python设置Word全局样式和文本样式的示例代码

返回顶部
顶部