python实现操作excel,数据写⼊excel的⼀⾏或者⼀列# _*_ coding:utf-8 _*_
import random
import xlwt,string
class ImportData(object):
def create_num(self,num):
"""
⾃动⽣成11位号段:
:param num: 输⼊数据数量
:return:
"""
num_start = ['134', '135', '136', '137', '138', '139', '140', '141',
'142', '148', '149', '147', '143', '144', '145',
'147', '130', '131', '132', '146']
for i in range(num):
start = random.choice(num_start)
# random.sample()⽣成随机数字 # .join()函数可以⽤来将字符串进⾏拼接 # digits⽅法的作⽤是⽣成数组,包括0-9
median = ''.join(random.sample(string.digits, 4))
end = ''.join(random.sample(string.digits, 4))
res = start + median + end
lumn0)
lumn0
def write_excel(self, num):
f = xlwt.Workbook()
sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True) # 创建sheet
row0 = [u'终端编号', u'终端类型', u'车牌号码', u'SIM卡', u'服务密码', u'ICCID号', u'车架号',
u'发动机号', u'安装⽇期', u'车主', u'联系⽅式', u'安装地点', u'备注', u'安装⼈员',
u'车型', u'续费年限']
create_nums0 = ImportData()
column0 = ate_num(num)
status = [u'A5B', u'A5C-3', u'A5C-5', u'A5C-8', u'K10']
# ⽣成字段名(第⼀⾏)
for i in range(0, len(row0)):
sheet1.write(0, i, row0[i])
# ⽣成第⼀列
for i in range(0, len(column0)):
sheet1.write(i + 1, 0, column0[i]) # sheet1.write(⾏数,列数,内容) ⾏数和列数从0开始
# ⽣成第⼆列
i = 0
while i < num/5 * len(status):
for j in range(0, len(status)):
sheet1.write(j + i + 1, 1, status[j])
i += 5
# ⽣成第三列
for j in range(0, len(column0)):
sheet1.write(j + 1, 2, column0[j])
# ⽣成第四列
for s in range(0, len(column0)):
sheet1.write(s + 1, 3, column0[s])
f.save('批量导⼊模板.xlsx') # 保存⽂件
if__name__ == '__main__':
nums = ImportData()
nums.write_excel(1000)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论