python_docx製作word文件詳細使用說明
阿新 • • 發佈:2019-01-30
在實習工作中,遇到用python製作word模板的任務,其實說白了就是python-docx的使用。目前網上對這一個庫的介紹得很少,很零散,所以很多功能我是儘量參考其官網,但是官網上面很多功能目前只有說明文件,而程式碼並還沒有及時更新,以至於按照官網上面做了,python卻報錯。比如:自定義表格的高度。下面,我對我在此次工作任務中,所遇到的一些基本的功能分別做一下說明與展示。我用的是python2.7
1.建立文件
from docx import Document
document = Document()
若括號裡面寫入word檔案路徑,則表明開啟該檔案
2.新增標題
document.add_heading('This is my title', 0)
但是,這個有個問題是標題下面有一條橫線,對於重度強迫症的我是無法容忍的。所以我直接新增段落文字表示標題
3.新增段落文字
document.add_paragraph('my paragraph')
但是,這隻實現了預設格式的段落文字新增,且這裡的文字只能是英文。如果要設定中文字型,且對文字設定對齊,顏色,大小等設定,則:
from docx.shared import RGBColor from docx.shared import Pt from docx.enum.text import WD_ALIGN_PARAGRAPH document.styles['Normal'].font.name = u'黑體' #可換成word裡面任意字型 p = document.add_paragraph() p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER #段落文字居中設定 run = p.add_run(u'我新增的段落文字') run.font.color.rgb = RGBColor(54,95,145) #顏色設定,這裡是用RGB顏色 run.font.size = Pt(36) #字型大小設定,和word裡面的字號相對應
4.新增圖片
pic = document.add_picture('pic.png',width = Inches(1.5)) #圖片和python檔案不在同一個資料夾下面的時候,要補全檔案地址
預設情況下,圖片在文件中是左對齊的,如果要對圖片進行居中顯示,在網上找了很多方法都不可行,最後找到一種方法是直接加入以下程式碼:
last_paragraph = document.paragraphs[-1]
last_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER #圖片居中設定
5.新增表格
table = document.add_table(rows=2, cols=3,style="Table Grid") #新增2行3列的表格
表格的style有很多種,預設情況下表格是沒有邊框的,Table Grid格式是普通的黑色邊框表格,更多表格樣式可以百度。但是,我們很多時候希望對錶格進行更為漂亮的修改,比如自定義表格某一列的寬度,表格的高度。
from docx.shared import Inches
# table.autofit = False
col = table.columns[1]
col.width = Inches(5) #設定表格第2列寬度為Inches(5) 預設情況下表格是自動適應文件寬度
對於表格的高度官網上面有說明文件,但是其庫函式的程式碼沒有更新,所以找了很久才找到下面一種方法,以後官網要是更新了程式碼可以按照官網上面的方法進行設定更為簡單一些。
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
for i in range(rows): #遍歷表格的所有行
tr = table.rows[i]._tr
trPr = tr.get_or_add_trPr()
trHeight = OxmlElement('w:trHeight')
trHeight.set(qn('w:val'), "450")
trPr.append(trHeight) #表格的每一行進行高度設定,450這個值可以任意修改
6.表格裡面新增文字
heading_cells = table.rows[0].cells #將表格的第一行設定為表頭
for i in range(cols): #cols為表格的列數
p = heading_cells[i].paragraphs[0] #利用段落功能新增文字
run = p.add_run(Arr[i]) #把表頭放在一個數組裡面的,這樣方便賦值
p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER #居中設定,預設是左對齊
還有一種直接對錶格賦值的方式:table.cell(i,j).text = u'表格文字' #在表格的i行j列設定文字,預設文字在表格中是左對齊
7.新增表格行
row = table.add_row()
若需要對新增的行進行賦值,其方法和上面是一樣的。
8.文件的儲存
document.save('test.docx') #可以設定其他路徑
整體程式碼如下:
from docx import Document
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
from docx.shared import RGBColor
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Inches
document = Document()
document.add_heading('This is my title', 0)
document.add_paragraph('my paragraph')
document.styles['Normal'].font.name = u'黑體'
p = document.add_paragraph()
p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
run = p.add_run(u'我新增的段落文字 ')
run.font.color.rgb = RGBColor(54, 95, 145)
run.font.size = Pt(36)
pic = document.add_picture('logo1.PNG')
last_paragraph = document.paragraphs[-1]
last_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER # 圖片居中設定
rows = 2
cols = 3
table = document.add_table(rows=rows, cols=cols,style = "Table Grid") # 新增2行3列的表格
for i in range(rows):
tr = table.rows[i]._tr
trPr = tr.get_or_add_trPr()
trHeight = OxmlElement('w:trHeight')
trHeight.set(qn('w:val'), "450")
trPr.append(trHeight) # 表格高度設定
# table.autofit = False
col = table.columns[1]
col.width = Inches(5)
arr = [u'序號',u"型別",u"詳細描述"]
heading_cells = table.rows[0].cells
for i in range(cols):
p = heading_cells[i].paragraphs[0]
run = p.add_run(arr[i])
run.font.color.rgb = RGBColor(54, 95, 145) # 顏色設定,這裡是用RGB顏色
run.font.size = Pt(12) # 字型大小設定,和word裡面的字號相對應
p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
table.cell(1, 1).text = u'表格文字'
table.add_row()
document.save('test1.docx')
執行結果如下: