Python docx庫代碼演示

Python docx庫代碼演示

安裝

需要lxml 
pip install python-docx

主業務代碼

from openpyxl import Workbook
from openpyxl import load_workbook
from docx import Document
from docx.oxml.ns import qn
from docx.shared import Pt,RGBColor,Cm
from docx.enum.style import WD_STYLE_TYPE
import os
# 新建輸出
if os.path.exists('test_out.docx'):
    os.remove('test_out.docx')
# 讀表   
i1 = load_workbook('業務節點分析20211022103647(1).xlsx')
print('讀表結束')
i = i1['業務節點分析']
out = []
# range(x,y) x到y-1行
for h in range(523,528):
    lie = []
    for l in i[h]:
        lie.append(l.value)
    out.append(lie[8])
print(out)
print(len(out))
print('等待生成word文檔')
# 創建word
doc = Document()
doc.styles['Normal'].font.name = u'宋體'
doc.styles['Normal'].font.size = Pt(10.5)
# 設置黑體字樣式
style_hei = doc.styles.add_style('hei', WD_STYLE_TYPE.CHARACTER) 
style_hei.font.name = '黑體'
doc.styles['hei']._element.rPr.rFonts.set(qn('w:eastAsia'), u'黑體')
# 設置宋體字樣式
style_hei = doc.styles.add_style('song', WD_STYLE_TYPE.CHARACTER) 
style_hei.font.name = '宋體'
doc.styles['song']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')
# 完成進度
    # 段1
p1 = doc.add_paragraph()
    # 文字
r1 = p1.add_run('完成進度',style='song')
r1.font.size = Pt(22)
r1.font.bold = True
p1.paragraph_format.space_before = Pt(17)
p1.paragraph_format.space_after = Pt(16.5)
p1.paragraph_format.line_spacing = 1.0
# 表1
table1 = doc.add_table(rows=len(out)+1,cols=4,style='Table Grid')
table1.cell(0,0).paragraphs[0].add_run('序號',style='song')
# 第二行的cell會改變原本的行寬
table1.cell(0,0).width=Cm(2)
table1.cell(0,1).paragraphs[0].add_run('案例名稱',style='song')
table1.cell(0,2).paragraphs[0].add_run('測試狀態',style='song')
table1.cell(0,3).paragraphs[0].add_run('時間',style='song')
for i in range(1,len(out)+1):
    table1.cell(i,0).paragraphs[0].add_run(str(i),style='song')
    table1.cell(i,0).width=Cm(2)
    table1.cell(i,1).paragraphs[0].add_run(out[i-1],style='song')
    table1.cell(i,2).paragraphs[0].add_run('待測試/測試中/完成',style='song')
'''
# 產品變更
    # 段2
p2 = doc.add_paragraph()
    # 文字
r2 = p2.add_run('\n一、產品變更',style='song')
r2.font.size = Pt(22)
r2.font.bold = True
p2.paragraph_format.space_before = Pt(17)
p2.paragraph_format.space_after = Pt(16.5)
p2.paragraph_format.line_spacing = 1.0
'''
# 前置條件+測試步驟描述
for i in range(1,len(out)+1):
    st = str(i)+'.'+out[i-1]
    # 段3
    p3 = doc.add_paragraph()
        # 文字
    r3 = p3.add_run(st,style='hei')
    r3.font.size = Pt(16)
    r3.font.bold = True
    p3.paragraph_format.space_before = Pt(13)
    p3.paragraph_format.space_after = Pt(13)
    p3.paragraph_format.line_spacing = 1.0
    # 段4
    p4 = doc.add_paragraph()
        # 文字
    r4 = p4.add_run('前置條件',style='song')
    r4.font.size = Pt(18)
    r4.font.bold = True
    p4.paragraph_format.line_spacing = 1.0
    # 表2
    table2 = doc.add_table(rows=4, cols=6,style ='Table Grid')
    t0 = table2.cell(0,0).paragraphs[0].add_run('號碼',style='song')
    t0.font.size = Pt(12)
    t0.font.color.rgb = RGBColor(0, 0, 255)
    table2.cell(0,1).merge(table2.cell(0,2)).merge(table2.cell(0,3)).merge(table2.cell(0,4)).merge(table2.cell(0,5))
    t1 = table2.cell(1,0).paragraphs[0].add_run('user_ID',style='song')
    t1.font.size = Pt(12)
    t1.font.color.rgb = RGBColor(0, 0, 255)
    table2.cell(1,1).merge(table2.cell(1,2)).merge(table2.cell(1,3)).merge(table2.cell(1,4)).merge(table2.cell(1,5))
    t2 = table2.cell(2,0).paragraphs[0].add_run('acct_ID',style='song')
    t2.font.size = Pt(12)
    t2.font.color.rgb = RGBColor(0, 0, 255)
    table2.cell(2,1).merge(table2.cell(2,2)).merge(table2.cell(2,3)).merge(table2.cell(2,4)).merge(table2.cell(2,5))
    t3 = table2.cell(3,0).paragraphs[0].add_run('cust_ID',style='song')
    t3.font.size = Pt(12)
    t3.font.color.rgb = RGBColor(0, 0, 255)
    table2.cell(3,1).merge(table2.cell(3,2)).merge(table2.cell(3,3)).merge(table2.cell(3,4)).merge(table2.cell(3,5))
    # 段5
    p5 = doc.add_paragraph()
        # 文字
    r5 = p5.add_run('\n測試步驟描述',style='song')
    r5.font.size = Pt(18)
    r5.font.bold = True
    p5.paragraph_format.line_spacing = 1.0
    # 表3
    table3 = doc.add_table(rows=2, cols=6,style ='Table Grid')
    t0 = table3.cell(0,0).paragraphs[0].add_run('Order_id',style='song')
    t0.font.size = Pt(12)
    t0.font.color.rgb = RGBColor(0, 0, 255)
    table3.cell(0,1).merge(table3.cell(0,2)).merge(table3.cell(0,3)).merge(table3.cell(0,4)).merge(table3.cell(0,5))
    t1 = table3.cell(1,0).paragraphs[0].add_run('Trade_id',style='song')
    t1.font.size = Pt(12)
    t1.font.color.rgb = RGBColor(0, 0, 255)
    table3.cell(1,1).merge(table3.cell(1,2)).merge(table3.cell(1,3)).merge(table3.cell(1,4)).merge(table3.cell(1,5))
    # 段6
    p6 = doc.add_paragraph()
        # 文字
    r6 = p6.add_run('\n(1)測試結果前臺截圖\n\n(2)測試結果後臺驗證\n相關TRADE表:\n相關SQL驗證:\n    ①\n    ②',style='song')  
    r6.font.size = Pt(12)
    r6.font.color.rgb = RGBColor(0, 0, 255)
    p6.paragraph_format.space_after = Pt(10)
    p6.paragraph_format.line_spacing = 1.5
# 保存
doc.save('test_out.docx')
print('生成test_out.docx成功')

測試代碼(設置字體)

from openpyxl import Workbook
from openpyxl import load_workbook
from docx import Document
from docx.oxml.ns import qn
from docx.shared import Pt,RGBColor
from docx.enum.style import WD_STYLE_TYPE
import os
document = Document() # 新建docx文檔
style_song = document.styles.add_style('Song', WD_STYLE_TYPE.CHARACTER) # 設置Song字樣式
style_song.font.name = '宋體'
document.styles['Song']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體') # 將段落中的所有字體
style_song = document.styles.add_style('Kai', WD_STYLE_TYPE.CHARACTER)
style_song.font.name = '楷體'
document.styles['Kai']._element.rPr.rFonts.set(qn('w:eastAsia'), u'楷體') # 將段落中的所有字體
style_song = document.styles.add_style('Lishu', WD_STYLE_TYPE.CHARACTER)
style_song.font.name = '隸書'
document.styles['Lishu']._element.rPr.rFonts.set(qn('w:eastAsia'), u'隸書') # 將段落中的所有字體
paragraph1 = document.add_paragraph() # 添加段落
run = paragraph1.add_run(u'aBCDefg這是中文', style='Song') # 設置宋體樣式
font = run.font #設置字體
font.name = 'Cambira' # 設置西文字體
paragraph1.add_run(u'aBCDefg這是中文', style='Kai').font.name = 'Cambira'
paragraph1.add_run(u'aBCDefg這是中文', style='Lishu').font.name = 'Cambira'
document.save('1.docx')

總結

本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容!

推薦閱讀: