Skip to content

lịch trung chuyển #1

@Quanganh955

Description

@Quanganh955

import pandas as pd
from openpyxl import Workbook
from openpyxl.styles import PatternFill, Alignment
from openpyxl.utils import get_column_letter
from datetime import datetime, timedelta

======= CẤU HÌNH CƠ BẢN =======

lai_xe = ["Lái 1", "Lái 2", "Lái 3", "Lái 4"]
xe = ["Xe 01", "Xe 02", "Xe 03"]

Anh chỉ cần đổi tháng ở đây 👇

thang = 11
nam = 2025

Tính số ngày tự động theo tháng

if thang == 2:
so_ngay = 29 if (nam % 4 == 0 and (nam % 100 != 0 or nam % 400 == 0)) else 28
elif thang in [1, 3, 5, 7, 8, 10, 12]:
so_ngay = 31
else:
so_ngay = 30

======= TẠO LỊCH XOAY =======

start_date = datetime(nam, thang, 1)
data = []

for i in range(so_ngay):
ngay = start_date + timedelta(days=i)
thu = ngay.strftime("%A") # Lấy thứ tiếng Anh
row = {"Ngày": ngay.strftime("%d/%m/%Y"), "Thứ": thu}

for j, x in enumerate(xe):
    row[x] = lai_xe[(i + j) % len(lai_xe)]
row["Nghỉ"] = [l for l in lai_xe if l not in row.values()][0]
data.append(row)

df = pd.DataFrame(data)
file_name = f"Lich_4_Lai_3_Xe_{thang}_{nam}.xlsx"

with pd.ExcelWriter(file_name, engine='openpyxl') as writer:
df.to_excel(writer, sheet_name="Lịch_Trực", index=False)

# ======= THÊM SHEET THỐNG KÊ =======
wb = writer.book
ws2 = wb.create_sheet("Thống_kê_Công")
ws2.append(["Tên Lái", "Số ngày trực", "Số ngày nghỉ"])

# COUNTIF động
for i, lx in enumerate(lai_xe, start=2):
    ws2.cell(row=i, column=1, value=lx)
    ws2.cell(row=i, column=2, value=f"=COUNTIF('Lịch_Trực'!C:E,\"{lx}\")")
    ws2.cell(row=i, column=3, value=f"=COUNTIF('Lịch_Trực'!F:F,\"{lx}\")")

# ======= ĐỊNH DẠNG MÀU =======
ws = wb["Lịch_Trực"]
fills = {
    "xe01": PatternFill(start_color="FFF2CC", end_color="FFF2CC", fill_type="solid"),
    "xe02": PatternFill(start_color="C6EFCE", end_color="C6EFCE", fill_type="solid"),
    "xe03": PatternFill(start_color="BDD7EE", end_color="BDD7EE", fill_type="solid"),
    "nghi": PatternFill(start_color="E7E6E6", end_color="E7E6E6", fill_type="solid"),
    "cuoi_tuan": PatternFill(start_color="FFD7D7", end_color="FFD7D7", fill_type="solid")
}

for row in ws.iter_rows(min_row=2, max_row=so_ngay + 1, min_col=1, max_col=6):
    ngay_cell, thu_cell, xe01, xe02, xe03, nghi = row
    # Tô màu cuối tuần (Thứ 7 & CN)
    if thu_cell.value in ["Saturday", "Sunday"]:
        for cell in row:
            cell.fill = fills["cuoi_tuan"]
    else:
        xe01.fill = fills["xe01"]
        xe02.fill = fills["xe02"]
        xe03.fill = fills["xe03"]
        nghi.fill = fills["nghi"]

    for cell in row:
        cell.alignment = Alignment(horizontal="center", vertical="center")

# ======= CHỈNH ĐỘ RỘNG =======
for col in range(1, 7):
    ws.column_dimensions[get_column_letter(col)].width = 16
for col in range(1, 4):
    ws2.column_dimensions[get_column_letter(col)].width = 20

wb.save(file_name)

print(f"✅ Đã tạo file Excel: {file_name}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions