Board logo

Title: python 建立 XLS文件,自动生辰日期、星期。 [Print this page]

Author: sky999    Time: 2023-7-2 12:58     Title: python 建立 XLS文件,自动生辰日期、星期。



CODE:
[Copy to clipboard]
import xlrd from datetime import datetime, timedelta # 打开Excel文件 workbook = xlrd.open_workbook('attendance1.xls') # 获取第一个工作表 sheet = workbook.sheet_by_index(0) # 读取D列的数据,从第二行开始 dates = sheet.col_values(3, start_rowx=1) # 获取当前日期 current_date = datetime.now() # 生成5月1日的日期 target_date = datetime(current_date.year, 5, 1) # 循环补充缺少的星期数 while current_date < target_date: # 根据当前日期判断是否存在于D列数据中 if current_date.date() not in dates: # 获取当前日期的星期 weekday = current_date.strftime('%A') # 在D列末尾添加新的日期和星期 dates.append(current_date.date()) sheet.write(len(dates), 3, current_date.date()) sheet.write(len(dates), 4, weekday) # 增加一天 current_date += timedelta(days=1) # 保存修改后的Excel文件 workbook.save('attendance1.xls')





Welcome AbyssalSwamp (http://service.caffz.com:12345/mud/AbyssalSwamp/index/) caffz.com