Python docx庫用法示例分析
本文實例分析了Python docx庫用法。分享給大家供大家參考,具體如下:
打開及保存文件:
from docx import Document document = Document('test.docx') document.save('test.docx')
添加文本:
document.add_paragraph('test text')
調(diào)整文本位置格式為居中:
from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH document = Document('test.docx') paragraph = document.add_paragraph('123') paragraph.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER document.save('test.docx')
調(diào)整左縮進(jìn)0.3英寸:
document = Document('test.docx') paragraph = document.add_paragraph('this is test for left_indent with inches') paragraph_format = paragraph.paragraph_format paragraph_format.left_indent = Inches(0.3) document.save('test.docx')
首行縮進(jìn):
paragraph_format.first_line_indent = Inches(0.3)
上行間距:
paragraph_format.space_before = Pt(18)
下行間距:
paragraph_format.space_after = Pt(12)
行距:
paragraph_format.line_spacing = Pt(18)
分頁格式:
緊跟上段:
paragraph_format.keep_together
若本頁無法完全顯示,另起一頁:
paragraph_format.keep_with_next
強制另起一頁:
paragraph_format.page_break_before
字體格式:
p = document.add_paragraph() run = p.add_run('test typeface') #加粗 run.font.bold = True #斜體 run.font.italic = True #下劃線 run.font.underline = True
WD_UNDERLINE 中有所有下劃線格式
調(diào)用樣例:
run.underline = WD_UNDERLINE.DOT_DASH
字體顏色:
from docx.shared import RGBColor test = document.add_paragraph().add_run('color') font = test.font font.color.rgb = RGBColor(0x42, 0x24 , 0xE9)
調(diào)用預(yù)設(shè)顏色:
from docx.enum.dml import MSO_THEME_COLOR font.color.theme_color = MSO_THEME_COLOR.ACCENT_1
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
pyCaret效率倍增開源低代碼的python機器學(xué)習(xí)工具
這篇文章主要介紹了pyCaret一款可以使效率倍增的開源低代碼的python機器學(xué)習(xí)工具,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11Django認(rèn)證系統(tǒng)實現(xiàn)的web頁面實現(xiàn)代碼
這篇文章主要介紹了Django認(rèn)證系統(tǒng)實現(xiàn)的web頁面實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08pytorch + visdom CNN處理自建圖片數(shù)據(jù)集的方法
這篇文章主要介紹了pytorch + visdom CNN處理自建圖片數(shù)據(jù)集的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06回歸預(yù)測分析python數(shù)據(jù)化運營線性回歸總結(jié)
本文主要介紹了python數(shù)據(jù)化運營中的線性回歸一般應(yīng)用場景,常用方法,回歸實現(xiàn),回歸評估指標(biāo),效果可視化等,并采用了回歸預(yù)測分析的數(shù)據(jù)預(yù)測方法2021-08-08Python繼承和子類從Person到Student演示實例
這篇文章主要為大家介紹了Python繼承和子類從Person到Student演示實例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Python實現(xiàn)定期檢查源目錄與備份目錄的差異并進(jìn)行備份功能示例
這篇文章主要介紹了Python實現(xiàn)定期檢查源目錄與備份目錄的差異并進(jìn)行備份功能,涉及Python基于filecmp模塊的文件比較及讀寫等相關(guān)操作技巧,需要的朋友可以參考下2019-02-02