python处理excel的一个示例脚本
Posted dadadadashixiong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python处理excel的一个示例脚本相关的知识,希望对你有一定的参考价值。
###此脚本为有需要购买车票的脚本,根据起始读取对应excel表中对应的数据,再根据此脚本中对应的规则计算出需要的数据输出两个excel
import xlrd import os import time import random ###################基础信息填写 #def mk_file(mounth): mounth = ‘5‘ ###########定义文件名和表头 filenameout = ((mounth) + ‘月份车票.xlsx‘) #给发票卖家的文档 filenamein = (‘工单服务组_庄艳彬‘ + ‘%s‘ % (mounth) + ‘月份打车记录.xlsx‘) ###上传km的文档 mesout = (‘日期‘ + ‘\t‘ + ‘打车日期‘ + ‘\t‘ + ‘起始时间‘ + ‘\t‘ + ‘到达时间‘ + ‘\t‘ + ‘金额‘ + ‘\n‘) mesin = (‘中文姓名‘ + ‘\t‘ + ‘打车日期‘ + ‘\t‘ + ‘事由‘ + ‘\t‘ + ‘实际上下班时间‘ + ‘\t‘ + ‘上车地点‘ + ‘\t‘ + ‘下车地点‘ + ‘\t‘ + ‘金额‘ + ‘\n‘) file_dir = os.getcwd() mkdir_now = (‘%s‘ % (file_dir) + "\\%s" % (mounth) + ‘月‘) folder = os.path.exists(mkdir_now) if not folder: # 判断是否存在文件夹如果不存在则创建为文件夹 os.makedirs(mkdir_now) # makedirs 创建文件时如果路径不存在会创建这个路径 print("--- CREATE DIRECTORY... ---") print("--- CREATE SUCCESS!! ---") else: print("--- DIRECTORY IS EXIST! ---") fileout=(‘%s‘ % (file_dir) + "\\%s" % (mounth) + ‘月‘+"\\%s"%(filenameout)) filein=(‘%s‘ % (file_dir) + "\\%s" % (mounth) + ‘月‘+"\\%s"%(filenamein)) with open(fileout, ‘w‘)as f: f.write(mesout) f.close() with open(filein, ‘w‘)as f: f.write(mesin) f.close() #####将整理后的数据写入文件out、in def record_out(date, classes_1, StartTime, EndTime, money): date=str(date) classes_1=str(classes_1) StartTime=str(StartTime) EndTime=str(EndTime) money=str(money) #filenameout = ((mounth) + ‘月份车票.xlsx‘) mesout = (date + ‘\t‘ + classes_1 + ‘\t‘ + StartTime + ‘\t‘ + EndTime + ‘\t‘ + money + ‘\n‘) with open(fileout, ‘a‘)as outself: outself.write(mesout) outself.flush() ####将整理后的数据写入文件out、in def record_in(name, date, classes_1, on_time, on_address, live_address, money): name=str(name) date = str(date) classes_1 = str(classes_1) on_time = str(on_time) on_address = str(on_address) money = str(money) #filenamein = (‘工单服务组_庄艳彬‘ + ‘%s‘ % (mounth) + ‘月份打车记录.xlsx‘) mesin = str(name + ‘\t‘ + date + ‘\t‘ + classes_1 + ‘\t‘ + on_time + ‘\t‘ + on_address + ‘\t‘ + live_address + ‘\t‘ + money + ‘\n‘) with open(filein, ‘a‘)as inself: inself.write(mesin) def write_data(filename): ###################拿源文件中的数据############## N3_time = ‘16:00-00:00‘ N9_time = ‘22:30-次日9:00‘ workbook = xlrd.open_workbook(u‘%s‘ % (filename)) sheet_names = workbook.sheet_names() sheetN = sheet_names[0] sheet1 = workbook.sheet_by_name(sheetN) colsNum = sheet1.col_values(2) data_Num = colsNum.__len__() for i in range(0, data_Num): rows = sheet1.row_values(i) date_classes = int(rows[0]) date_classes3=(date_classes+1) #print(date3) classes = int(rows[1]) money = int(rows[2]) date = (‘2018/‘ + ‘%s/‘ % (mounth) + ‘%s‘ % (date_classes)) date3 = (‘2018/‘ + ‘%s/‘ % (mounth) + ‘%s‘ % (date_classes3)) if classes == 3: on_address = company_address live_address = home_address on_time = N3_time Land = int((money - 13) / 2.3) LandTime_minute = int((Land / random.randint(60, 80)) * 60) StartTime_hour = 00 # N3时起始时间(小时) StartTime_minute = (random.randint(30, 50)) # N3时起始时间(分钟) EndTime_minute = StartTime_minute + LandTime_minute # 到达时间:分钟(起始时间分钟数+路上时间分钟数) if EndTime_minute >= 60: # 如果路上总时间大于60分钟,起始时间小时+1,分钟等于总时间分钟数-60 endtime_hour = int(StartTime_hour) + 1 endtime_minute = (EndTime_minute - 60) elif EndTime_minute < 60: # 如果路上总时间小于60分钟,到达时间_小时=起始时间_小时,到达时间_分钟=总时间分钟数 endtime_hour = StartTime_hour endtime_minute = EndTime_minute StartTime_hour = str(StartTime_hour).zfill(2) StartTime_minute = str(StartTime_minute).zfill(2) StartTime = (‘%s:‘ % (StartTime_hour) + ‘%s‘ % (StartTime_minute)) EndTime = (‘%s:‘ % (endtime_hour) + ‘%s‘ % (endtime_minute)) # 输出此次总信息 classes_1 = str(‘N‘ + ‘%s‘ % (classes)) #print(date, classes_1, StartTime, EndTime, money) record_out(date3, classes_1, StartTime, EndTime, money) record_in(name, date, classes_1, on_time, on_address, live_address, money) elif classes == 9: on_address = home_address live_address = company_address on_time = N9_time Land = int((money - 13) / 2.3) LandTime_minute = int((Land / random.randint(60, 80)) * 60) StartTime_hour = 21 # N3时起始时间(小时) StartTime_minute = (random.randint(0, 15)) # N3时起始时间(分钟) EndTime_minute = StartTime_minute + LandTime_minute # 到达时间:分钟(起始时间分钟数+路上时间分钟数) if EndTime_minute >= 60: # 如果路上总时间大于60分钟,起始时间小时+1,分钟等于总时间分钟数-60 endtime_hour = int(StartTime_hour) + 1 endtime_minute = (EndTime_minute - 60) elif EndTime_minute < 60: # 如果路上总时间小于60分钟,到达时间_小时=起始时间_小时,到达时间_分钟=总时间分钟数 endtime_hour = StartTime_hour endtime_minute = EndTime_minute StartTime_hour = str(StartTime_hour).zfill(2) StartTime_minute = str(StartTime_minute).zfill(2) StartTime = (‘%s:‘ % (StartTime_hour) + ‘%s‘ % (StartTime_minute)) EndTime = (‘%s:‘ % (endtime_hour) + ‘%s‘ % (endtime_minute)) # 输出此次总信息 classes_1 = str(‘N‘ + ‘%s‘ % (classes)) #print(date,classes_1,StartTime,EndTime,money) record_out(date, classes_1, StartTime, EndTime, money) record_in(name, date, classes_1, on_time, on_address, live_address, money) #创建月份文件夹 if __name__==‘__main__‘: name = ‘gary‘ mounth = ‘5‘ filename = ‘5.xls‘ home_address = ‘ad1‘ company_address = ‘ad2‘ # mk_file(mounth) # print(fileout) write_data(filename)
以上是关于python处理excel的一个示例脚本的主要内容,如果未能解决你的问题,请参考以下文章
嗨,我有一个 python 脚本可以使用 openpyxl 处理 excel 数据,但是在 2500 行之后我得到了内存错误。谁可以帮我这个事?