Python自动化办公实例excel数据批量导入word模板中

一、需要扩展库

需要提前安装第三方库,分别为:python-docx,xlrd。
在运行中输入 cmd 进入命令控制
安装方法:pip install python-docx
pip install xlrd
输入pip list,查看是否安装成功在这里插入图片描述

``
from docx import Document
import xlrd
import random

def change_data(title, type1, num):
document = Document(‘地标号.docx’)
for i in document.paragraphs:
i.text = i.text.replace(‘xxx’, title)
i.text = i.text.replace(‘x00’, type1)
i.text = i.text.replace(‘x11’, num)
title = str(random.randint(1,9999))+title
document.save(title+’.docx’)

excel = xlrd.open_workbook(‘地标号.xlsx’)
table = excel.sheet_by_index(0)
nows = table.nrows

for i in range(nows):
title = table.cell(i, 2).value
type1 = str(table.cell(i, 7).value)
num = str(int(table.cell(i, 8).value))
change_data(title, type1, num)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值