:批量制作档案表,要从excel表格中将每个人的数据导入到docx档案

该博客介绍了一个使用Python实现的自动化脚本,该脚本读取Excel表格中的数据,并将其填充到预设的Word模板文件中,生成多个定制化的Word文档。通过docxtpl和openpyxl库,脚本遍历Excel工作表中的每一行,提取姓名和字段名称信息,然后利用DocxTemplate的render方法将这些信息渲染到Word模板中,最后保存为新的Word文件。
摘要由CSDN通过智能技术生成

https://www.pythonf.cn/read/149081 Python自动将Excel数据填充到word的指定位置,Word,中

在这里插入图片描述

具体代码如下:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from docxtpl import DocxTemplate
from openpyxl import load_workbook
import os


def replace(obj):
    if obj is None:
        obj = ''
        return obj


# 加载要填入的数据
wb = load_workbook(r"./detailContent.xlsx")
ws = wb['sheet1']

contexts = []
for row in range(2, ws.max_row + 1):
    name = ws["A" + str(row)].value
    #c_name = ws["B" + str(row)].value
    ziduanName = ws["C" + str(row)].value
    # num = ws["D" + str(row)].value
    # time = ws["E" + str(row)].value
    # time = str(time)[:-9]
    # money = ws["F" + str(row)].value
    # address = ws["G" + str(row)].value
    # replace_peo = ws["H" + str(row)].value

    # context = {"name": name, "c_name": c_name, "code": code, "num": num, "time": time,
    #            "money": money, "address": address, "replace_peo": replace_peo}
    context = {"name":name,"ziduanName":ziduanName}
    contexts.append(context)
#contexts
print(contexts)
print(len(contexts))
# 创建要保存的文件夹
os.mkdir("./word1")

for context in contexts:
    print(context)
    tpl = DocxTemplate(r"./model.docx")
    tpl.render(context)
    tpl.save("./word1/数据表-{}.docx".format(context["name"]))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值