这篇文章给大家介绍excel表格怎么利用python进行操作,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
<强> 1。下载相关python包强>
python操作excel表格可以使用以下三个包
xlrd——读excel文件
xlwt - excel写文件,这个不能修改已有excel的文件,只能写新的文件
xlutils——修改excel文件,其实就是通过xlrd拷贝一份记录,再进行修改。保存为老的名字就替换了原文件,保存为新的名字就创建一个新文件
注意事项:
。python读取excel的日期和时间时
表格内容是2019/5/13,python读到的值是43606.0,该值为从日期减1899/12/30得到的天数
表格内容是9:00:00,python读到的值是0.375,该值为时间过了一天的比例,即9/24
表格内容是2019/5/13 9:00:00,python读到的值是43598.375
日期和时间可以直接相加,因为python读到的都是转化为数字之后的值
b。python读取excel的数字时,如员工编号为181129,最后结果是181129.0,非整数
c。调用保存函数保存新excel的文件时,后缀名必须是xls
<强> 2。将python文件转为。蝙蝠格式强>
你不可能要求妹子去使用cmd,然后使用python xx.py去执行python文件,必须想个办法搞成傻瓜式的。我们可以通过。bat格式文件实现
新建文本文件,重命名为“考勤小工具。bat”,输入下面代码,@py.exe表示后面的参数是python可执行文件
@py。exe Akqfx.py
<强> 3。附上相关代码和excel格式文本强>
Akqfx。py
#该脚本为修正考勤记录 #作者:yangbao 进口操作系统 从datetime进口datetime 进口xlrd 从xlutils。复制进口复制 #定义文件是否存在 def get_list_file (): current_list=os.listdir () must_list=[& # 39;原始数据xls # 39;, & # 39;外出xls # 39;, & # 39;法定假日xls # 39;, & # 39;请假xls # 39;】 cj_set=(must_list)——集合(current_list) 如果cj_set: 因为我在cj_set: 打印(& # 39;{}不存在,请检查! & # 39;.format(我)) 返回0 其他: 返回1 #定义是否存在流程 def get_qjorwc (file_name person_id input_time): 书=xlrd.open_workbook (file_name) book_sheet=book.sheet_by_index (0) 国旗=0 我的范围(1,book_sheet.nrows): 如果int (book_sheet。1)cell_value(我)==int (person_id): #文件不同,时间处理不同 如果file_name==& # 39;请假xls # 39;: cell_begin=book_sheet。cell_value(我,4) cell_end=book_sheet。cell_value(我,5) 其他: cell_begin=book_sheet。cell_value(我,3)+ book_sheet。cell_value(我,4) cell_end=book_sheet。cell_value (5) + book_sheet。cell_value(我,6) #判断原始数据旷工和迟到是否在请假或外出流程里 #给额外5分钟的宽限时间 如果cell_begin-5/1440 & lt;=input_time & lt;=cell_end + 5/1440: 标志=1 打破 返回国旗 #定义是否是法定假日 def get_fdjr (input_time): 书=xlrd.open_workbook(& # 39;法定假日xls # 39;) book_sheet=book.sheet_by_index (0) 国旗=0 我的范围(1,book_sheet.nrows): dt=datetime (* xlrd.xldate_as_tuple (book_sheet。cell_value(我,0),0)) 如果dt.strftime (& # 39; Y % - % - % d # 39;)==input_time: 标志=1 打破 返回国旗 def main (): ys_book=xlrd.open_workbook(& # 39;原始数据xls # 39;) ys_book_sheet=ys_book.sheet_by_index (0) new_ys_book=复制(ys_book) new_ys_book_sheet=new_ys_book.get_sheet (0) unnormal_list=[& # 39;旷工& # 39;,& # 39;迟到& # 39;】 因为我在范围(ys_book_sheet.nrows): #查上班时间 如果ys_book_sheet。在unnormal_list cell_value(我,5): #查是否是法定假日 dt=ys_book_sheet。cell_value(我,3)[10] 如果get_fdjr (dt): new_ys_book_sheet。写(我5 & # 39;* & # 39;) #查是否有流程 如果ys_book_sheet。cell_value(我,4)!=& # 39;& # 39;: cell_on_time=ys_book_sheet。cell_value(我,3)[10]+ & # 39;& # 39;+ ys_book_sheet。cell_value(我,4) cell_on_time_format=datetime。strptime (cell_on_time“Y % - % - % d % H: % m: % S") \ - datetime.strptime (& # 39; 1899 - 12 - 30 - & # 39;, & # 39; Y - % - % d % # 39;) cell_on_time_number=cell_on_time_format。天+ cell_on_time_format。秒/(24 * 3600) 如果12 & lt;cell_on_time_format。秒/3600 & lt;13: cell_on_time_number=cell_on_time_format。天+ 11.5/24 其他: cell_on_time=ys_book_sheet。cell_value(我,3)[10] cell_on_time_format=datetime。strptime (cell_on_time“Y % - % - % d") \ - datetime.strptime (& # 39; 1899 - 12 - 30 - & # 39;, & # 39; Y - % - % d % # 39;) cell_on_time_number=cell_on_time_format。天+ cell_on_time_format。秒/(24 * 3600)+ 9/24 qj_on_flag=get_qjorwc(& # 39;请假xls # 39;, ys_book_sheet。cell_value(我,1),cell_on_time_number) wc_on_flag=get_qjorwc(& # 39;外出xls # 39;, ys_book_sheet。cell_value(我,1),cell_on_time_number) 如果qj_on_flag==1或wc_on_flag==1: new_ys_book_sheet。写(我5 & # 39;已有流程& # 39;) new_ys_book_sheet。写(我11 & # 39;& # 39;) #查下班时间 如果ys_book_sheet。在unnormal_list cell_value(我,7): #查是否是法定假日 dt=ys_book_sheet。cell_value(我,3)[10] 如果get_fdjr (dt): new_ys_book_sheet。写(我7 & # 39;* & # 39;) new_ys_book_sheet。写(我11 & # 39;& # 39;) #查是否有流程 如果ys_book_sheet。cell_value(我,6)!=& # 39;& # 39;: cell_out_time=ys_book_sheet。cell_value(我,3)[10]+ & # 39;& # 39;+ ys_book_sheet。cell_value(我,6) cell_out_time_format=datetime。strptime (cell_out_time“Y % - % - % d % H: % m: % S") \ - datetime.strptime (& # 39; 1899 - 12 - 30 - & # 39;, & # 39; Y - % - % d % # 39;) cell_out_time_number=cell_out_time_format。天+ cell_out_time_format。秒/(24 * 3600) 如果12 & lt;cell_out_time_format。秒/3600 & lt;13: cell_out_time_number=cell_out_time_format。天+ 13.5/24 其他: cell_out_time=ys_book_sheet。cell_value(我,3)[10] cell_out_time_format=datetime。strptime (cell_out_time“Y % - % - % d") \ - datetime.strptime (& # 39; 1899 - 12 - 30 - & # 39;, & # 39; Y - % - % d % # 39;) cell_out_time_number=cell_out_time_format。天+ cell_out_time_format。秒/(24 * 3600)+ 18/24 null null null null null null null null null null null null null null null null null nullexcel表格怎么利用python进行操作