excel表格怎么利用python进行操作

发布时间:2020-12-07 15:21:46 作者:Leah
来源:亿速云 阅读:199

这篇文章给大家介绍excel表格怎么利用python进行操作,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

1. 下载相关python包

python操作excel表格可以使用以下三个包
xlrd - 读excel文件
xlwt - 写excel文件,这个不能修改已有的excel文件,只能写新的文件
xlutils - 修改excel文件,其实就是通过xlrd拷贝一份记录,再进行修改。保存为老的名字就替换了原文件,保存为新的名字就创建一个新文件

注意事项:
a. 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. 调用save函数保存新的excel文件时,后缀名必须是.xls

2. 将python文件转为.bat格式
你不可能要求妹子去使用cmd,然后使用python xx.py去执行python文件,必须想个办法搞成傻瓜式的。我们可以通过.bat格式文件实现
新建文本文件,重命名为“A考勤小工具.bat”,输入下面代码,@py.exe表示后面的参数是python可执行文件
@py.exe Akqfx.py

3. 附上相关代码和excel格式文本

excel表格怎么利用python进行操作

excel表格怎么利用python进行操作

Akqfx.py

# 该脚本为修正考勤记录
# author: yangbao

import os
from datetime import datetime
import xlrd
from xlutils.copy import copy


# 定义文件是否存在
def get_list_file():
  current_list = os.listdir()
  must_list = ['原始数据.xls', '外出.xls', '法定假日.xls', '请假.xls']
  cj_set = set(must_list) - set(current_list)
  if cj_set:
    for i in cj_set:
      print('{} 不存在,请检查!'.format(i))
      return 0
  else:
    return 1


# 定义是否存在流程
def get_qjorwc(file_name, person_id, input_time):
  book = xlrd.open_workbook(file_name)
  book_sheet = book.sheet_by_index(0)
  flag = 0
  for i in range(1, book_sheet.nrows):
    if int(book_sheet.cell_value(i, 1)) == int(person_id):
      # 文件不同,时间处理不同
      if file_name == '请假.xls':
        cell_begin = book_sheet.cell_value(i, 4)
        cell_end = book_sheet.cell_value(i, 5)
      else:
        cell_begin = book_sheet.cell_value(i, 3) + book_sheet.cell_value(i, 4)
        cell_end = book_sheet.cell_value(i, 5) + book_sheet.cell_value(i, 6)
      # 判断原始数据旷工和迟到是否在请假或外出流程里
      # 给额外5min的宽限时间
      if cell_begin-5/1440 <= input_time <= cell_end+5/1440:
        flag = 1
        break
  return flag


# 定义是否是法定假日
def get_fdjr(input_time):
  book = xlrd.open_workbook('法定假日.xls')
  book_sheet = book.sheet_by_index(0)
  flag = 0
  for i in range(1, book_sheet.nrows):
    dt = datetime(*xlrd.xldate_as_tuple(book_sheet.cell_value(i, 0), 0))
    if dt.strftime('%Y-%m-%d') == input_time:
      flag = 1
      break
  return flag


def main():
  ys_book = xlrd.open_workbook('原始数据.xls')
  ys_book_sheet = ys_book.sheet_by_index(0)
  new_ys_book = copy(ys_book)
  new_ys_book_sheet = new_ys_book.get_sheet(0)
  unnormal_list = ['旷工', '迟到']
  for i in range(ys_book_sheet.nrows):
    # 查上班时间
    if ys_book_sheet.cell_value(i, 5) in unnormal_list:
      # 查是否是法定假日
      dt = ys_book_sheet.cell_value(i, 3)[:10]
      if get_fdjr(dt):
        new_ys_book_sheet.write(i, 5, '*')

      # 查是否有流程
      if ys_book_sheet.cell_value(i, 4) != '':
        cell_on_time = ys_book_sheet.cell_value(i, 3)[:10] + ' ' + ys_book_sheet.cell_value(i, 4)
        cell_on_time_format = datetime.strptime(cell_on_time, "%Y-%m-%d %H:%M:%S") \
                   - datetime.strptime('1899-12-30', '%Y-%m-%d')
        cell_on_time_number = cell_on_time_format.days + cell_on_time_format.seconds / (24 * 3600)
        if 12 < cell_on_time_format.seconds / 3600 < 13:
          cell_on_time_number = cell_on_time_format.days + 11.5/24
      else:
        cell_on_time = ys_book_sheet.cell_value(i, 3)[:10]
        cell_on_time_format = datetime.strptime(cell_on_time, "%Y-%m-%d") \
                   - datetime.strptime('1899-12-30', '%Y-%m-%d')
        cell_on_time_number = cell_on_time_format.days + cell_on_time_format.seconds / (24 * 3600) + 9/24

      qj_on_flag = get_qjorwc('请假.xls', ys_book_sheet.cell_value(i, 1), cell_on_time_number)
      wc_on_flag = get_qjorwc('外出.xls', ys_book_sheet.cell_value(i, 1), cell_on_time_number)
      if qj_on_flag == 1 or wc_on_flag == 1:
        new_ys_book_sheet.write(i, 5, '已有流程')
        new_ys_book_sheet.write(i, 11, '')

    # 查下班时间
    if ys_book_sheet.cell_value(i, 7) in unnormal_list:
      # 查是否是法定假日
      dt = ys_book_sheet.cell_value(i, 3)[:10]
      if get_fdjr(dt):
        new_ys_book_sheet.write(i, 7, '*')
        new_ys_book_sheet.write(i, 11, '')

      # 查是否有流程
      if ys_book_sheet.cell_value(i, 6) != '':
        cell_out_time = ys_book_sheet.cell_value(i, 3)[:10] + ' ' + ys_book_sheet.cell_value(i, 6)
        cell_out_time_format = datetime.strptime(cell_out_time, "%Y-%m-%d %H:%M:%S") \
                    - datetime.strptime('1899-12-30', '%Y-%m-%d')
        cell_out_time_number = cell_out_time_format.days + cell_out_time_format.seconds / (24 * 3600)

        if 12 < cell_out_time_format.seconds / 3600 < 13:
          cell_out_time_number = cell_out_time_format.days + 13.5/24

      else:
        cell_out_time = ys_book_sheet.cell_value(i, 3)[:10]
        cell_out_time_format = datetime.strptime(cell_out_time, "%Y-%m-%d") \
                    - datetime.strptime('1899-12-30', '%Y-%m-%d')
        cell_out_time_number = cell_out_time_format.days + cell_out_time_format.seconds / (24 * 3600) + 18/24

      qj_out_flag = get_qjorwc('请假.xls', ys_book_sheet.cell_value(i, 1), cell_out_time_number)
      wc_out_flag = get_qjorwc('外出.xls', ys_book_sheet.cell_value(i, 1), cell_out_time_number)
      if qj_out_flag == 1 or wc_out_flag == 1:
        new_ys_book_sheet.write(i, 7, '已有流程')
        new_ys_book_sheet.write(i, 11, '')

  new_excel_name = datetime.now().strftime('%Y%m%d_%H%M%S')+'校正后.xls'
  new_ys_book.save(new_excel_name)


if __name__ == '__main__':
  if get_list_file():
    print('开始考勤分析...')
    main()
    print('考勤分析结束...')
    input('按任意键结束')
  else:
    input('因为缺少相关excel文件,考勤分析失败,退出程序,按任意键结束')

关于excel表格怎么利用python进行操作就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. Python学习—对excel表格的操作
  2. python对excel表格的操作

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python excel

上一篇:python执行shell命令无法获取返回值如何解决

下一篇:如何在python中利用subprocess pipe实现实时输出日志

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》