Python設(shè)置Word全局樣式和文本樣式的示例代碼
上一章節(jié)我們學(xué)習(xí)了如何生成 word 文檔以及在文檔行中添加各種內(nèi)容,今天我們基于上一章節(jié)的內(nèi)容進(jìn)行添磚加瓦 —> 對內(nèi)容進(jìn)行各種樣式的設(shè)置,讓其能夠看起來更加的美觀。
全局樣式的定義
通過全局樣式的設(shè)置,可以使得 word 全文都可以繼承這樣的樣式效果:
使用方法:
style = document_obj.styles['Normal']
通過 Document 對象調(diào)用 styles 對象集,通過中括號的方式選擇全局樣式,獲得 樣式對象 。
for style in document_obj.styles: # 通過 for 循環(huán)可以查看 styles 對象集
print(style)
styles 對象集如下:

全局定義的基本樣式舉例:
字體:style.font.name = '微軟雅黑'
字體顏色:style.font.color.rgb = RGBColor(255, 0, 0)
通過 from docx.shared import RGBColor 調(diào)用 docx 包的三原色模塊
字體大?。?/strong>style.font.size = Pt(20)
通過 from docx.shared import Pt 調(diào)用 docx 包的字體大小設(shè)置模塊
代碼示例如下:(在上一章節(jié)的代碼基礎(chǔ)上進(jìn)行 全局樣式的代碼演示)
# coding:utf-8
from docx import Document
from docx.shared import Inches, RGBColor, Pt
doc = Document()
style = doc.styles['Normal'] # 使用標(biāo)準(zhǔn)樣式
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 標(biāo)題
title.add_run('\n - 測試版本') # 針對 title 標(biāo)題進(jìn)行內(nèi)容追加(換行)
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'] # 定義表格的第一行的標(biāo)題
table = doc.add_table(rows=1, cols=3) # 定義表格的行數(shù)、列數(shù)
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 的內(nèi)容,準(zhǔn)備將其追加寫入表格
('Neo', '18', 'man'),
('Adem', '17', 'man'),
('Lily', '18', 'women')
]
for i in data: # 利用 for 循環(huán)將 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 標(biāo)題
doc.save('test.docx')運(yùn)行結(jié)果如下:


文本樣式的定義
標(biāo)題與段落
從上面的截圖可以看出,當(dāng)我們設(shè)置全局字體的顏色和大小的時候,只有段落收到了影響,而標(biāo)題未受影響,這就引出了文本的樣式。
字體:
obj.font.name = "微軟雅黑" 這里的 obj 就表示的是 標(biāo)題與段落的對象 ;與設(shè)置全局字體的方式一致。
字體顏色:
obj.font.color.rgb = RGBColor(255, 0, 0)
字體大?。?/strong>
obj.font.size = Pt(20)
標(biāo)題居中:
obj.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER 需要導(dǎo)入: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'] # 使用標(biāo)準(zhǔn)樣式
style.font.name = '微軟雅黑' # 使用 "微軟雅黑" 字體
# style.font.color.rgb = RGBColor(255, 0, 0) # 使用紅色作為字體顏色
style.font.size = Pt(14)
title = doc.add_heading('', 0) # 添加 word 文件的 title 標(biāo)題;(需要注意的是,這里第一行的標(biāo)題是不能設(shè)置為斜體等類型的)
# 若想要將標(biāo)題設(shè)置為斜體,需在這一行標(biāo)題內(nèi)容為空,然后針對追加內(nèi)容寫入標(biāo)題設(shè)置為斜體
title.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 標(biāo)題居中
title.style.font.size = Pt(20)
title_run = title.add_run('this is title\n測試版本') # 針對 title 標(biāo)題進(jìn)行內(nèi)容追加(換行)
title_run.italic = True # 將追加的內(nèi)容轉(zhuǎn)為斜體字
title_run.blod = True # 將追加的內(nèi)容轉(zhuǎn)為粗體字
title_run.underline = True
# print(dir(title)) # 通過 dir 函數(shù)查看當(dāng)前 title 標(biāo)題可以使用的更多有趣的函數(shù)
para = doc.add_paragraph('這是 \"test.docx\" 文件的第一行段落')
para.add_run('\n這是 \"test.docx\" 文件追加的的第二行段落').italic = True # 將第二行段落設(shè)置為斜體
para.add_run('\n這是 \"test.docx\" 文件追加的的第三行段落').blod = True # 將第三行段落設(shè)置為粗體
para.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 將段落設(shè)置為居中顯示
print(dir(para)) # 通過 dir 函數(shù)查看當(dāng)前 para 段落可以使用的更多有趣的函數(shù)
image = doc.add_picture('test_image.png', width=Inches(3), height=Inches(1.5)) # 添加圖片
table_title = ['name', 'age', 'sex'] # 定義表格的第一行的標(biāo)題
table = doc.add_table(rows=1, cols=3) # 定義表格的行數(shù)、列數(shù)
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 的內(nèi)容,準(zhǔn)備將其追加寫入表格
('Neo', '18', 'man'),
('Adem', '17', 'man'),
('Lily', '18', 'women')
]
for i in data: # 利用 for 循環(huán)將 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 標(biāo)題
doc.save('test.docx')運(yùn)行結(jié)果如下:


到此這篇關(guān)于Python設(shè)置Word全局樣式和文本樣式的示例代碼的文章就介紹到這了,更多相關(guān)Python Word樣式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用Flask-Migrate拓展數(shù)據(jù)庫表結(jié)構(gòu)
這篇文章主要介紹了如何使用Flask-Migrate拓展數(shù)據(jù)庫表結(jié)構(gòu),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Python中OpenCV圖像特征和harris角點(diǎn)檢測
Harris角點(diǎn)檢測算子是于1988年由CHris Harris & Mike Stephens提出來的。在具體展開之前,不得不提一下Moravec早在1981就提出來的Moravec角點(diǎn)檢測算子。本文重點(diǎn)給大家介紹OpenCV圖像特征harris角點(diǎn)檢測知識,一起看看吧2021-09-09
python?DataFrame中l(wèi)oc與iloc取數(shù)據(jù)的基本方法實(shí)例
這篇文章主要給大家介紹了關(guān)于python?DataFrame中l(wèi)oc與iloc取數(shù)據(jù)的基本方法,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
pandas對齊運(yùn)算的實(shí)現(xiàn)示例
本文主要介紹了pandas對齊運(yùn)算的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Python中staticmethod和classmethod的作用與區(qū)別
今天小編就為大家分享一篇關(guān)于Python中staticmethod和classmethod的作用與區(qū)別,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10

