網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
上一章節(jié)我們學(xué)習(xí)了如何生成 word 文檔以及在文檔行中添加各種內(nèi)容,今天我們基于上一章節(jié)的內(nèi)容進(jìn)行添磚加瓦 —> 對(duì)內(nèi)容進(jìn)行各種樣式的設(shè)置,讓其能夠看起來(lái)更加的美觀。
全局樣式的定義
通過(guò)全局樣式的設(shè)置,可以使得 word 全文都可以繼承這樣的樣式效果:
使用方法:
style = document_obj.styles['Normal']
通過(guò) Document 對(duì)象調(diào)用 styles 對(duì)象集,通過(guò)中括號(hào)的方式選擇全局樣式,獲得 樣式對(duì)象 。
for style in document_obj.styles: # 通過(guò) for 循環(huán)可以查看 styles 對(duì)象集 print(style)
styles 對(duì)象集如下:
全局定義的基本樣式舉例:
字體:style.font.name = '微軟雅黑'
字體顏色:style.font.color.rgb = RGBColor(255, 0, 0)
通過(guò) from docx.shared import RGBColor
調(diào)用 docx 包的三原色模塊
字體大小:style.font.size = Pt(20)
通過(guò) 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 - 測(cè)試版本') # 針對(duì) 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 文件的分頁(yè) title = doc.add_heading('this is page_2 title', 1) # 添加 word 文件的第二分頁(yè)的 title 標(biāo)題 doc.save('test.docx')
運(yùn)行結(jié)果如下:
文本樣式的定義
標(biāo)題與段落
從上面的截圖可以看出,當(dāng)我們?cè)O(shè)置全局字體的顏色和大小的時(shí)候,只有段落收到了影響,而標(biāo)題未受影響,這就引出了文本的樣式。
字體:
obj.font.name = "微軟雅黑"
這里的 obj
就表示的是 標(biāo)題與段落的對(duì)象
;與設(shè)置全局字體的方式一致。
字體顏色:
obj.font.color.rgb = RGBColor(255, 0, 0)
字體大小:
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)容為空,然后針對(duì)追加內(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測(cè)試版本') # 針對(duì) 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)) # 通過(guò) 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)) # 通過(guò) 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 文件的分頁(yè) title = doc.add_heading('this is page_2 title', 1) # 添加 word 文件的第二分頁(yè)的 title 標(biāo)題 doc.save('test.docx')
運(yùn)行結(jié)果如下:
原文鏈接:https://blog.csdn.net/weixin_42250835/article/details/124761456
相關(guān)推薦
- 2022-03-18 .NET?6開(kāi)發(fā)TodoList應(yīng)用之實(shí)現(xiàn)全局異常處理_實(shí)用技巧
- 2023-04-26 Sklearn調(diào)優(yōu)之網(wǎng)格搜索與隨機(jī)搜索原理詳細(xì)分析_python
- 2022-11-03 python數(shù)據(jù)分析基礎(chǔ)知識(shí)之shape()函數(shù)的使用教程_python
- 2022-12-04 .NET?Core利用BsonDocumentProjectionDefinition和Lookup
- 2022-07-28 C++實(shí)例講解引用的使用_C 語(yǔ)言
- 2022-04-10 Blazor組件事件處理功能_基礎(chǔ)應(yīng)用
- 2022-05-15 Python?文件與文件對(duì)象及文件打開(kāi)關(guān)閉_python
- 2023-03-29 C語(yǔ)言交換奇偶位與offsetof宏的實(shí)現(xiàn)方法_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支