亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python docx庫(kù)用法示例分析

 更新時(shí)間:2019年02月16日 11:05:00   作者:BSOD_aura  
這篇文章主要介紹了Python docx庫(kù)用法,結(jié)合實(shí)例形式分析了docx庫(kù)相關(guān)的docx文件讀取、文本添加、格式操作,需要的朋友可以參考下

本文實(shí)例分析了Python docx庫(kù)用法。分享給大家供大家參考,具體如下:

打開及保存文件:

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)

分頁(yè)格式:

緊跟上段:

paragraph_format.keep_together

若本頁(yè)無(wú)法完全顯示,另起一頁(yè):

paragraph_format.keep_with_next

強(qiáng)制另起一頁(yè):

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文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論