• 技术文章 >Python技术 >Python基础教程

    如何编写python3处理word文档代码?

    小妮浅浅小妮浅浅2020-12-01 15:54:22原创2479

    直接使用word文档已经难不倒大家了,有没有想过用python构建一个word文档写点文章呢?当然这个文章的框架需要我们用代码一点点的建立,在过程上有一点繁琐,一下子看不懂的小伙伴可以把它拆分成几个部分来看。下面就在python3处理word文档的代码给大家带来讲解,还会有一些设置文章格式的技巧。


    一个Word文档,主要由下面这些内容元素构成,每个元素都有对应的方法处理:

    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

    import pathlib

    from docx import Document

    from docx.shared import Inches, Pt

    from docx.oxml.ns import qn

      

    path = list(pathlib.Path.cwd().parents)[1].joinpath('data/automate/003word')

    out_path = path.joinpath('003word_create.docx')

    img_path = path.joinpath('dance.jpg')

      

    document = Document()

    document.add_heading('Python1024_自动生成标题', 0)

    document.add_heading('基本:文本', level=1)

    p = document.add_paragraph('测试文本\n测试内容\n')

    p.add_run('粗体部分内容\n').bold = True

    p.add_run('斜体部分\n').italic = True

    p.add_run('下划线部分\n').underline = True

    p.add_run('字体设置\n').font.size = Pt(24)

    # 测试第三方字体

    x = p.add_run('三方字体测试\n')

    x.font.name = 'Source Han Sans CN' # 思源字体

    x.element.rPr.rFonts.set(qn('w:eastAsia'), 'Source Han Sans CN')

    # 段落和引用

    document.add_heading('标题一:段落', level=1)

    document.add_paragraph('引用块', style='Intense Quote')

    document.add_heading('标题1.1、无序列表', level=2)

    opts = ['选项1','选项2', '选项3']

    # 无需列表

    for opt in opts:

        document.add_paragraph(opt, style='List Bullet')

    document.add_heading('标题1.2、有序列表', level=2)

    # 有序列表

    for opt in opts:

        document.add_paragraph(opt, style='List Number')

    document.add_heading('标题二:图片', level=1)

    document.add_picture(str(img_path), width=Inches(5))

    document.add_page_break()

    document.add_heading('标题三:表格', level=1)

    records = (

        (1, '电风扇', '无叶风扇'),

        (2, '吹风机', '离子风机'),

        (3, 'Macbook pro', 'Apple macbook pro 15寸')

    )

    # 表格

    table = document.add_table(rows=1, cols=3)

    # 表头

    hdr_cells = table.rows[0].cells

    hdr_cells[0].text = '数量'

    hdr_cells[1].text = 'ID'

    hdr_cells[2].text = '描述信息'

    # 表格数据

    for qty, cid, desc in records:

        row_cells = table.add_row().cells

        row_cells[0].text = str(qty)

        row_cells[1].text = cid

        row_cells[2].text = desc

    # 保存文档

    document.save(out_path)



    设置段落样式,如下:

    1

    document.add_paragraph('这是一个样式为 ListBullet 的段落', style='ListBullet')


    1

    2

    paragraph = document.add_paragraph('这是一个样式为 ListBullet 的段落')

    paragraph.style = 'List Bullet'


    设置段落间距

    分为 段前 段后 ,设置值用 Pt 单位是 磅 ,如下:

    1

    2

    paragraph_format.space_before = Pt(18)

    paragraph_format.space_after = Pt(12)


    设置段落行距

    当行距为 最小值 固定值 时,设置值单位为 ,需要用 Pt ;当行距为 多倍行距 时,设置值为数值,如下:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    from docx.shared import Length

      

    #SINGLE         =>  单倍行距(默认)

    #ONE_POINT_FIVE =>  1.5倍行距

    #DOUBLE2        =>  倍行距

    #AT_LEAST       =>  最小值

    #EXACTLY        =>  固定值

    #MULTIPLE       =>  多倍行距

      

    paragraph.line_spacing_rule = WD_LINE_SPACING.EXACTLY #固定值

    paragraph_format.line_spacing = Pt(18) # 固定值18磅

    paragraph.line_spacing_rule = WD_LINE_SPACING.MULTIPLE #多倍行距

    paragraph_format.line_spacing = 1.75 # 1.75倍行间距


    这里设置word的几个格式技巧就讲完啦,有的小伙伴直呼困难。小编想说的是只有我们亲自去参与框架的搭建,才能对于以后操作word有更好的理解。更多Python学习指路:PyThon学习网教学中心

    专题推荐:python3代码
    上一篇:python3处理excel文本内容代码怎么写? 下一篇:布局背景颜色代码在python3中如何写?

    相关文章推荐

    • 如何用python3代码玩小游戏?• Python3代码可以用py2exe加密吗?• python3代码怎样快速测试结果?• python3代码如何自动补全?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网