from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH #設置對象居中、對齊等。 from docx.enum.text import WD_TAB_ALIGNMENT,WD_TAB_LEADER #設置制表符等 from docx.shared import Inches #設置圖像大小 from docx.shared import Pt #設置像素、縮進等 from docx.shared import RGBColor #設置字體顏色 from docx.shared import Length #設置寬度
模板里的表格和行列遍歷
from docx import Document #導入模塊 document = Document("template.docx") #打開模板 for t,table in enumerate(document.tables): #遍歷所有表格,這個模板里有3個表格 for r,row in enumerate(table.rows): #遍歷表格的行 for c,cell in enumerate(row.cells): #遍歷每一行的列 cell.text = "%s表%s行%s列%s\n"%(cell.text,t,r,c) #在單元格里寫入當前的位置(表格、行、列)
處理word文檔
新建文檔類
首先新建一個空白文檔類 Document ,如下:
from docx import Document document = Document()
獲取文件中的表格集
file = docx.Document(filedocx) #打開文件 tables = file.tables # 獲取文件中的表格集 table=tables[0] ###獲取第一個表格
編輯已存在的word文檔
其實吧,這玩意兒只能編輯已存在的word文檔,之所以有個“創建空白文檔”的功能,只不過是拷貝一份空白word文檔到工作區間,再在空白文檔上編輯,看起來似乎是“創建空白文檔”罷了。本質上還是編輯已存在的word文檔,捂臉中...
打開一個word文檔,編輯完后,一定要記得保存。如果保存文件名和原文件名不一樣,則會另存為一份word文檔;若文件名一樣,則會不加提示的保存修改內容。如下:
from docx import Document document = Document('existing-document-file.docx') document.save('new-file-name.docx')
新增段落
在word中 段落
是最常見的,創建段落 paragraph 的操作如下:
paragraph = document.add_paragraph('這是個段落。')
在此段落之前插入一個段落,如下:
prior_paragraph = paragraph.insert_paragraph_before('這是前面的段落。')
新增標題
新增標題代碼如下:
document.add_heading('這是個標題')
修改標題大小,有1-9種規格,如下:
document.add_heading('The role of dolphins', level=2)
如果使用 level=0
,則會新增一個帶有下划線樣式的標題。
新增分頁符
代碼如下:
document.add_page_break()
新增表格
創建一個2行2列的表格 Table,如下:
table = document.add_table(rows=2, cols=2)
獲取第一行第二列的單元格類,如下:
cell = table.cell(0, 1)
寫入數據,如下:
cell.text = '這是第一行第二列的單元格'
不僅如此,還能以數組的形式獲取整個行或列,如下:
row = table.rows[1] row.cells[0].text = '第二行第一列' row.cells[1].text = '第二行第二列'
或循環操作,如下:
for row in table.rows: for cell in row.cells: print(cell.text)
用 len()
方法獲取行數或列數,如下:
row_count = len(table.rows)
col_count = len(table.columns)
增加行,如下:
row = table.add_row()
設置表格樣式,如下:
table.style = 'LightShading-Accent1'
插入圖片
插入本地圖片,如下:
document.add_picture('demo.png')
默認情況下,圖片大小往往不盡如人意,調整圖片大小,如下:
from docx.shared import Inches document.add_picture('demo.png', width=Inches(1.0), height=Inches(1.0))
若同時定義寬度和高度,則圖片會被拉伸或壓縮到指定大小;若僅定義寬度或高度,則圖會自適應調整大小。所以,建議僅定義寬度即可。
段落操作
設置段落樣式
設置段落樣式,如下:
document.add_paragraph('這是一個樣式為 ListBullet 的段落', style='ListBullet')
或
paragraph = document.add_paragraph('這是一個樣式為 ListBullet 的段落') paragraph.style = 'List Bullet'
設置段落對齊方式
段落對齊方式有 左對齊
、 文字居中
、 右對齊
、 文本兩端對齊
等,更多對齊方式請移步 WD_ALIGN_PARAGRAPH
from docx.enum.text import WD_ALIGN_PARAGRAPH # LEFT => 左對齊 # CENTER => 文字居中 # RIGHT => 右對齊 # JUSTIFY => 文本兩端對齊 paragraph = document.add_paragraph("你說啥") paragraph_format = paragraph.paragraph_format paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
設置段落縮進
設置段落縮進,可為負值,如下:
from docx.shared import Inches paragraph = document.add_paragraph("你說啥") paragraph_format = paragraph.paragraph_format paragraph_format.left_indent = Inches(0.5)
也可以設置首行縮進,如下:
paragraph_format.first_line_indent = Inches(-0.25)
設置段落制表符
詳情請移步 TabStops
設置段落間距
分為 段前
和 段后
,設置值用 Pt
單位是 磅
,如下:
paragraph_format.space_before = Pt(18)
paragraph_format.space_after = Pt(12)
設置段落行距
當行距為 最小值
和 固定值
時,設置值單位為 磅
,需要用 Pt
;當行距為 多倍行距
時,設置值為數值,如下:
from docx.shared import Length #SINGLE => 單倍行距(默認) #ONE_POINT_FIVE => 1.5倍行距 #DOUBLE2 => 倍行距 #AT_LEAST => 最小值 #EXACTLY => 固定值 #MULTIPLE => 多倍行距 paragraph.line_spacing_rule = WD_LINE_SPACING.EXACTLY #固定值 paragraph_format.line_spacing = Pt(18) # 固定值18磅 paragraph.line_spacing_rule = WD_LINE_SPACING.MULTIPLE #多倍行距 paragraph_format.line_spacing = 1.75 # 1.75倍行間距
設置段落分頁
- 孤行控制
防止在頁面頂端單獨打印段落末行或在頁面底端單獨打印段落首行。 - 與下段同頁
防止在選中段落與后面一段間插入分頁符。 - 段中不分頁
防止在段落中出現分頁符。 - 段前分頁
在選中段落前插入分頁符。
#widow_control => 孤行控制 #keep_with_next => 與下段同頁 #page_break_before => 段前分頁 #keep_together => 段中不分頁 paragraph_format.keep_with_next = True
字體操作
設置粗體和斜體
在設置粗體和斜體之前,我們先簡單了解一下 段落
里的運行機制。段落包含很多塊級的格式,比如縮進、行高、制表符等。每一個小片段叫做一個 run ,可以對 run
設置粗體和斜體等屬性。
我們可以設置如下:
paragraph = document.add_paragraph() paragraph.add_run('這是一個帶有') paragraph.add_run('粗體').bold = True paragraph.add_run('和') paragraph.add_run('斜體').italic = True paragraph.add_run('的段落。')
設置字體屬性
對 run
設置字體、大小、顏色下划線等,更多屬性請移步 Font ,如下:
from docx.shared import RGBColor,Pt #all_caps => 全部大寫字母 #bold => 加粗 #color => 字體顏色 #complex_script => 是否為“復雜代碼” #cs_bold => “復雜代碼”加粗 #cs_italic => “復雜代碼”斜體 #double_strike => 雙刪除線 #emboss => 文本以凸出頁面的方式出現 #hidden => 隱藏 #imprint => 印記 #italic => 斜體 #name => 字體 #no_proof => 不驗證語法錯誤 #outline => 顯示字符的輪廓 #shadow => 陰影 #small_caps => 小型大寫字母 #snap_to_grid => 定義文檔網格時對齊網絡 #strike => 刪除線 #subscript => 下標 #superscript => 上標 #underline => 下划線 paragraph = document.add_paragraph() paragraph.add_run('這是一個帶有') paragraph.add_run('顏色').font.color.rgb = RGBColor(54, 95, 145) paragraph.add_run('的') paragraph.add_run('大字').font.size = Pt(36) # 字體大小設置,和word里面的字號相對應
設置字符樣式
除了設置段落樣式外,還可以設置一組字符樣式,比如字體、大小、顏色、粗體、斜體等,如下:
# 自定義樣式 Emphasis paragraph = document.add_paragraph('這是一個帶有') paragraph.add_run('自定義樣式', 'Emphasis') paragraph.add_run('的段落')
或
paragraph = document.add_paragraph('這是一個帶有 ') run = paragraph.add_run('自定義樣式') run.style = 'Emphasis' paragraph.add_run('的段落')
頁眉和頁腳
更多內容請移步 Working with Headers and Footers