欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python docx库代码演示

程序员文章站 2022-03-19 08:46:51
目录python docx库代码演示主业务代码测试代码(设置字体)总结python docx库代码演示安装需要lxml pip install python-docx主业务代码from openpyx...

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')

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!

相关标签: Python docx