一、需要扩展库
需要提前安装第三方库,分别为: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)