数据分析--word文档自动化2

数据分析--word文档自动化2

数据分析–word文档自动化2

表格操作

def table_merge_text(a, b, t):
    """
    合并单元格 并写入文字
    Args:
        a: cell a
        b: cell b
        t: 文字

    Returns:
        cell obj
    """
    m = a.merge(b)
    m.text = t
    for pgh in m.paragraphs:
        pgh.alignment = WD_TABLE_ALIGNMENT.CENTER
    return m


def table_write_rows(cells, txt_list):
    """
    批量写入
    Args:
        cells: 单元格列表
        txt_list: 文字列表

    Returns:

    """
    for c, t in zip(cells, txt_list):
        c.text = str(t)
        for pgh in c.paragraphs:
            pgh.alignment = WD_TABLE_ALIGNMENT.CENTER

实战


doc_title = 'Title'
title_size = 1
table_title = 'table title'
table_content = ['a1', 'a2', 'a3', 'a4']
pngs_path = ['发.png', '2022.05.02.png']
doc_save = 'mytest.docx'

doc = new_document()

# 添加标题
add_head(doc, doc_title, title_size)

# 添加统计表格
tb = add_table(doc, 3, 4)
table_merge_text(tb.rows[0].cells[0], tb.rows[0].cells[-1], table_title)
table_write_rows(tb.rows[1].cells, table_content)

# 调整表格样式
tw = int(tb.columns[0].width * 1.3)
tb.columns[0].width = tw
for cell in tb.column_cells(0):
    cell.paragraphs[0].alignment = WD_TABLE_ALIGNMENT.LEFT
    cell.width = tw

# 添加图片
doc_add_picture(doc, pngs_path)

save_document(doc, doc_save)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值