脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python docx库代码演示

Python docx库代码演示

2022-02-17 14:27浪速之星 Python

这篇文章主要介绍了Python docx库用法,结合实例形式分析了docx库相关的docx文件读取、文本添加、格式操作,需要的朋友可以参考下

Python docx库代码演示

安装

?
1
2
需要lxml
pip install python-docx

主业务代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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成功')

测试代码(设置字体)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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')

总结

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

原文链接:https://blog.csdn.net/weixin_44476410/article/details/120943891

延伸 · 阅读

精彩推荐