import openpyxl
# 创建一个workbook对象,而且会在workbook中至少创建一个表worksheet
wb = openpyxl.Workbook()
# 获取当前活跃的worksheet,默认就是第一个worksheet
ws = wb.active
from openpyxl.styles import PatternFill, Font, Alignment, Border, Side
def init_sheet(ws):
for r in range(100):
for c in range(100):
ws.cell(row=r+1, column=c+1).fill = PatternFill('solid', fgColor='000000')
def set_cell_style(ws, r, c, color):
ws.cell(row=r, column=c).fill = PatternFill('solid', fgColor=color)
ws.cell(row=r, column=c).font = Font(name="微软雅黑", size=14, bold=True)
ws.cell(row=r, column=c).alignment = Alignment(horizontal='right', vertical='center')
side = Side(style="medium", color="004B3C")
ws.cell(row=r, column=c).border = Border(top=side, bottom=side, left=side, right=side)
import calendar
# 将表格填充成白色
init_sheet(ws)
# 设置年月单元格的边框
side = Side(style="medium", color="004B3C")
for col in range(7):
ws.cell(row=1, column=col+1).border = Border(top=side, bottom=side, left=side, right=side)
# 合并年月单元格
ws.merge_cells(start_row=1, start_column=1, end_row=1, end_column=7)
# 写入内容和设置格式
ws.cell(row=1, column=1).value = '2022年8月'
set_cell_style(ws, r=1, c=1, color='418CFA')
# 写入星期一至星期日,并设置格式
title_data = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
for col in range(7):
ws.cell(row=2, column=col+1).value = title_data[col]
set_cell_style(ws, r=2, c=col+1, color='418CFA')
# 获取一个月的天数和第一天是星期几
monthday = calendar.monthrange(2022, 8)
# 设置日历的日期
col, row = monthday[0], 3
for i in range(len(my_nucleic['date'])):
if col < 7:
ws.cell(row=row, column=col + 1).value = i+1
col += 1
else:
col = 0
row += 1
ws.cell(row=row, column=col + 1).value = i+1
col += 1
# 设置单元格格式
set_cell_style(ws, r=row, c=col, color='000000')
# 根据核酸结果填充颜色
if my_nucleic['nucleic'] == 1:
ws.cell(row=row, column=col).fill = PatternFill('solid', fgColor='009B3C')
# 设置行高
for i in range(1, row+1):
ws.row_dimensions.height = 30
# 保存表格
wb.save(filename='show_august_nucleic.xlsx')
wb.close()
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |