Files
med-check_generator/main.py

55 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pandas as pd
import openpyxl
import os
from tqdm import tqdm
from datetime import datetime
DB_FILE = 'files/source.xlsx'
TEMPLATE_FILE = 'files/template.xlsx'
OUTPUT_FOLDER = 'files/output/'
MAPPING = {
'Дата МО': 'Q1',
'Время МО': 'Q2',
'Номер направления': 'A13',
'Фамилия, имя, отчество': 'D21',
'Дата рождения': 'D22',
'Структурное подразделение': 'F26',
'Должность работника ': 'F27',
'''Наименование вредных производственных факторов или фидов работ (приложение к приказу Минздрава РФ от 28.01.2021 №29Н)''': 'F30'
}
if not os.path.exists(OUTPUT_FOLDER):
os.makedirs(OUTPUT_FOLDER)
df = pd.read_excel(DB_FILE)
print(f"Всего сотрудников в списке: {len(df)}")
for index, row in tqdm(df.iterrows(), total=len(df), desc="Обработка"):
wb = openpyxl.load_workbook(TEMPLATE_FILE)
ws = wb.active
for excel_col, cell_address in MAPPING.items():
value = row.get(excel_col, "")
if pd.isna(value):
value = ""
if cell_address == 'A13':
ws[cell_address] = f"НАПРАВЛЕНИЕ № {value}" # type: ignore
elif isinstance(value, (datetime, pd.Timestamp)):
ws[cell_address] = value.strftime('%d.%m.%Y') # type: ignore
else:
ws[cell_address] = str(value) # type: ignore
num = str(row.get('Номер направления', index + 1)) # type: ignore
fio = str(row.get('Фамилия, имя, отчество')).strip().replace(' ', '_')
file_name = f"Направление_№{num}_{fio}.xlsx"
save_path = os.path.join(OUTPUT_FOLDER, file_name)
wb.save(save_path)
print(f"\nГотово! Файлы сохранены в '{OUTPUT_FOLDER}'")