生成值班表
作者:互联网
pip install xlwings
代码如下:
import xlwings as xw
from datetime import date
import datetime
import os
app = xw.App(visible=True, add_book=False)
workbook = app.books.add()
sheet = workbook.sheets.active
#周末值班人员
zm_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
no = int(input('周末值班到第几个人了:'))
temp_list = []
for i in range(len(zm_list)):
temp_list.append(zm_list[(i+no) % len(zm_list)])
sheet.range(1, 1).column_width = 13.9
sheet.range(1, 2).column_width = 15.7
sheet.range(1, 3).column_width = 21.4
sheet.range(1, 4).column_width = 22.2
sheet.range(1, 6).column_width = 12.8
sheet.range(1, 1).row_height = 44.3
sheet.range('A2:D47').row_height = 15.9
today = date.today()
day = date(today.year, today.month+1, 1)
sheet.range('A1').value = f'2022年{day.month}月份值班表'
sheet.name = f'2022-{day.month}月值班'
sheet.range('A1:D1').api.Merge()
sheet.range('A1:D47').api.HorizontalAlignment = -4108
sheet.range('A1').api.Font.Size = 14
sheet.range('A1').api.Font.Bold = True
sheet.range('A1:D60').api.Font.Name = '宋体'
row_num = int(input('请输入行数(大概40-54):'))
for i in range(2, row_num+1):
sheet.range(f'A{i}').api.Borders(9).LineStyle = 1
sheet.range(f'A{i}').api.Borders(8).LineStyle = 1
sheet.range(f'A{i}').api.Borders(7).LineStyle = 1
sheet.range(f'A{i}').api.Borders(10).LineStyle = 1
sheet.range(f'B{i}').api.Borders(9).LineStyle = 1
sheet.range(f'B{i}').api.Borders(8).LineStyle = 1
sheet.range(f'B{i}').api.Borders(7).LineStyle = 1
sheet.range(f'B{i}').api.Borders(10).LineStyle = 1
sheet.range(f'C{i}').api.Borders(9).LineStyle = 1
sheet.range(f'C{i}').api.Borders(8).LineStyle = 1
sheet.range(f'C{i}').api.Borders(7).LineStyle = 1
sheet.range(f'C{i}').api.Borders(10).LineStyle = 1
sheet.range(f'D{i}').api.Borders(9).LineStyle = 1
sheet.range(f'D{i}').api.Borders(8).LineStyle = 1
sheet.range(f'D{i}').api.Borders(7).LineStyle = 1
sheet.range(f'D{i}').api.Borders(10).LineStyle = 1
sheet.range('A2').value = '日期'
sheet.range('B2').value = '对应星期'
sheet.range('C2').value = '值班人'
sheet.range('D2').value = '时间范围'
sheet.range('A2:D2').api.Font.Bold = True
sheet.range('A2:D2').api.Font.Size = 10
def last_day_of_month(any_day):
"""
获取获得一个月中的最后一天
:param any_day: 任意日期
:return: string
"""
next_month = any_day.replace(day=28) + datetime.timedelta(days=4) # this will never fail
return next_month - datetime.timedelta(days=next_month.day)
today = date.today()
day = date(today.year, today.month, 31)
tomorrow = date(today.year, today.month+1, 1)
mark = True
i = 3
zm_num = 0
while mark:
if day.isoweekday() == 1:
if tomorrow != last_day_of_month(day):
day = tomorrow
sheet.range(f'A{i}:A{i+1}').api.Merge()
sheet.range(f'A{i}').value = day
sheet.range(f'B{i}:B{i + 1}').api.Merge()
sheet.range(f'B{i}').value = '星期二'
sheet.range(f'A{i}:D{i + 1}').color = (84, 130, 53)
tomorrow = date(day.year, day.month, day.day+1)
i += 2
for j in range(3):
list1 = ['星期三', '星期四', '星期五']
if tomorrow != last_day_of_month(day):
day = tomorrow
sheet.range(f'A{i}').value = day
sheet.range(f'B{i}').value = list1[j]
tomorrow = date(day.year, day.month, day.day + 1)
i += 1
else:
mark = False
else:
day = tomorrow
sheet.range(f'A{i}:A{i + 1}').api.Merge()
sheet.range(f'A{i}').value = day
sheet.range(f'B{i}:B{i + 1}').api.Merge()
sheet.range(f'B{i}').value = '星期二'
sheet.range(f'A{i}:D{i + 1}').color = (84, 130, 53)
mark = False
if day.isoweekday() == 5:
if tomorrow != last_day_of_month(day):
day = tomorrow
sheet.range(f'A{i}:A{i+1}').api.Merge()
sheet.range(f'A{i}').value = day
sheet.range(f'B{i}:B{i + 1}').api.Merge()
sheet.range(f'B{i}').value = '星期六'
sheet.range(f'A{i}:D{i + 1}').color = (169, 208, 142)
sheet.range(f'C{i}').value = temp_list[zm_num % len(temp_list)]
tomorrow = date(day.year, day.month, day.day + 1)
zm_num += 1
i += 2
else:
day = tomorrow
sheet.range(f'A{i}:A{i + 1}').api.Merge()
sheet.range(f'A{i}').value = day
sheet.range(f'B{i}:B{i + 1}').api.Merge()
sheet.range(f'B{i}').value = '星期六'
sheet.range(f'A{i}:D{i + 1}').color = (169, 208, 142)
sheet.range(f'C{i}').value = temp_list[zm_num % len(temp_list)]
mark = False
if day.isoweekday() == 6:
# for _ in range(2):
if tomorrow != last_day_of_month(day):
day = tomorrow
sheet.range(f'A{i}:A{i+1}').api.Merge()
sheet.range(f'A{i}').value = day
sheet.range(f'B{i}:B{i + 1}').api.Merge()
sheet.range(f'B{i}').value = '星期日'
sheet.range(f'C{i}').value = temp_list[zm_num % len(temp_list)]
sheet.range(f'A{i}:D{i + 1}').color = (169, 208, 142)
tomorrow = date(day.year, day.month, day.day + 1)
zm_num += 1
i += 2
else:
day = tomorrow
sheet.range(f'A{i}:A{i + 1}').api.Merge()
sheet.range(f'A{i}').value = day
sheet.range(f'B{i}:B{i + 1}').api.Merge()
sheet.range(f'B{i}').value = '星期日'
sheet.range(f'C{i}').value = temp_list[zm_num % len(temp_list)]
sheet.range(f'A{i}:D{i+1}').color = (169, 208, 142)
mark = False
if day.isoweekday() == 7:
if tomorrow != last_day_of_month(day):
day = tomorrow
sheet.range(f'A{i}:A{i+1}').api.Merge()
sheet.range(f'A{i}').value = day
sheet.range(f'B{i}:B{i + 1}').api.Merge()
sheet.range(f'B{i}').value = '星期一'
sheet.range(f'A{i}:D{i + 1}').color = (84, 130, 53)
tomorrow = date(day.year, day.month, day.day + 1)
i += 2
else:
day = tomorrow
sheet.range(f'A{i}:A{i + 1}').api.Merge()
sheet.range(f'A{i}').value = day
sheet.range(f'B{i}:B{i + 1}').api.Merge()
sheet.range(f'B{i}').value = '星期一'
sheet.range(f'A{i}:D{i + 1}').color = (84, 130, 53)
mark = False
for i in range(3, row_num+1):
if i % 2 == 1:
sheet.range(f'D{i}').value = '当日8:30至17:30'
else:
sheet.range(f'D{i}').value = '当日17:30至次日8:30'
for i in range(3, 12):
sheet.range(f'F{i}').api.Font.Name = '宋体'
sheet.range(f'F{i}').api.Font.Size = 9
sheet.range(f'F{i}').api.HorizontalAlignment = -4108
sheet.range(f'F{i}').api.Borders(9).LineStyle = 1
sheet.range(f'F{i}').api.Borders(8).LineStyle = 1
sheet.range(f'F{i}').api.Borders(7).LineStyle = 1
sheet.range(f'F{i}').api.Borders(10).LineStyle = 1
sheet.range(f'F{i}').value = zm_list[i-3]
sheet.range(f'F{i}').color = (169, 208, 142)
sheet.range('F2').api.Font.Name = '宋体'
sheet.range('F2').api.Font.Bold = True
sheet.range('F2').api.Font.Size = 10
sheet.range('F2').api.HorizontalAlignment = -4108
sheet.range('F2').api.Borders(9).LineStyle = 1
sheet.range('F2').api.Borders(8).LineStyle = 1
sheet.range('F2').api.Borders(7).LineStyle = 1
sheet.range('F2').api.Borders(10).LineStyle = 1
sheet.range('F2').value = '周末9人轮班'
workbook.save(f'2022-{tomorrow.month}月值班表.xls')
app.quit()
os.system('pause')
标签:sheet,value,生成,range,api,Borders,值班,day 来源: https://www.cnblogs.com/xzztj/p/15784941.html