Python docx庫用法示例分析

2022-10-04 18:03:10 字數 1788 閱讀 8910

開啟及儲存檔案:

from docx import document

document = document('test.docx')

document.s**e('test.docx')

新增文字:

document.add_paragraph('test text')

調整文字位置格式為居中:

from docx import document

from docx.enum.text import wd_align_paragraph

document = document('test.docx')

paragraph = document.add_para程式設計客棧graph('123')

paragraph.paragraph_format.alignment = wd_align_paragraph.center

document.s**e('test.docx')

調整左縮排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.s**e('test.docx')

首行縮排:

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 = t

#斜體run.font.italic = true

#下劃線

run.font.underline = true

wd_underline 中有所有下劃線格式

呼叫樣例:

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)

程式設計客棧

呼叫預設顏色:

from docx.enum.dml 程式設計客棧import mso_theme_color

font.color.theme_color = mso_theme_color.accent_1

安裝python docx庫時產生的問題的解決方法

安裝python docx時產生的問題 學習自然語言處理知識時,在win7作業系統 anaconda 4.3.1 64 bit 環境下,利用網上常用的方法,即在 命令提示符 程式中輸入命令pip install python docx安裝python docx時,經常產生異常,無法成功安裝。下圖是安...

ArrayList用法示例

一 public class student implements comparable public string getname public int compareto student stu public string tostring 二 public class add 向array中新...

Mysql limit offset 用法示例

mysql limit offset示例 例1,假設資料庫表student存在13條資料。示例 語句1 select from student limit 9,4 語句2 slect from student limit 4 offset 9 語句1和2均返回表student的第10 11 12 1...