日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

python excel操作单元格_python 操作excel表格的方法

發布時間:2023/11/27 生活经验 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python excel操作单元格_python 操作excel表格的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

說明:由于公司oa暫缺,人事妹子在做考勤的時候,需要通過幾個excel表格去交叉比對員工是否有曠工或遲到,工作量大而且容易出錯。

這時候it屌絲的機會來啦,花了一天時間給妹子擼了一個自動化腳本。

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格式文本

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文件,考勤分析失敗,退出程序,按任意鍵結束')

該文檔僅作個人記錄用

以上就是python 操作excel表格的方法的詳細內容,更多關于python 操作excel表格的資料請關注我們其它相關文章!

本文標題: python 操作excel表格的方法

本文地址: http://www.cppcns.com/jiaoben/python/368072.html

總結

以上是生活随笔為你收集整理的python excel操作单元格_python 操作excel表格的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。