Python docx库代码演示

 更新时间:2021年10月25日 14:34:26   作者:浪速之星  
这篇文章主要介绍了Python docx库用法,结合实例形式分析了docx库相关的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')

总结

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

相关文章

  • python矩阵的基本运算及各种操作

    python矩阵的基本运算及各种操作

    python的numpy库提供矩阵运算的功能,因此我们在需要矩阵运算的时候,需要导入numpy的包,下面这篇文章主要给大家介绍了关于python矩阵的基本运算及各种操作的相关资料,需要的朋友可以参考下
    2022-11-11
  • 利用python3 的pygame模块实现塔防游戏

    利用python3 的pygame模块实现塔防游戏

    这篇文章主要介绍了利用python3 的pygame模块实现塔防游戏,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12
  • python3中int(整型)的使用教程

    python3中int(整型)的使用教程

    这篇文章主要介绍了关于python3中int(整型)的使用教程,文中介绍的非常详细,相信对大家学习或者使用python3能具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-03-03
  • python多维数组切片方法

    python多维数组切片方法

    下面小编就为大家分享一篇python多维数组切片方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • python GUI图形化编程wxpython的使用

    python GUI图形化编程wxpython的使用

    这篇文章主要介绍了python GUI图形化编程wxpython的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • python使用ctypes调用第三方库时出现undefined symbol错误详解

    python使用ctypes调用第三方库时出现undefined symbol错误详解

    python中时间的库有time和datetime,pandas也有提供相应的时间处理函数,下面这篇文章主要给大家介绍了关于python使用ctypes调用第三方库时出现undefined symbol错误的相关资料,需要的朋友可以参考下
    2023-02-02
  • Numpy维度知识总结

    Numpy维度知识总结

    这篇文章主要介绍了Numpy维度知识总结,因为在numpy里一维既可以做行向量也可以做列向量,那对于任意一个给定的一维向量,我们就无法确定他到底是行向量还是列向量,为了防止这种尴尬的境地,习惯上用二维矩阵而不是一维矩阵来表示行向量和列向量,需要的朋友可以参考下
    2023-09-09
  • 使用tensorflow将图片灰度化以及遇到的坑解决

    使用tensorflow将图片灰度化以及遇到的坑解决

    这篇文章主要介绍了使用tensorflow将图片灰度化以及遇到的坑解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • python typing模块--类型提示支持

    python typing模块--类型提示支持

    这篇文章主要介绍python typing模块类型提示支持, typing 模块只有在python3.5以上的版本中才可以使用,pycharm目前支持typing检查,下面进入文章一起了解详细内容吧
    2021-10-10
  • 对python:print打印时加u的含义详解

    对python:print打印时加u的含义详解

    今天小编就为大家分享一篇对python:print打印时加u的含义详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12

最新评论