refactor(reports): модульное разделение excel_exporter на styles, calculators и билдеры отчетов
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
"""
|
||||
===============================================================================
|
||||
FILE: services/reports/raw_scud_builder.py
|
||||
ROLE: Генерация Excel-файла сырых данных СКУД.
|
||||
===============================================================================
|
||||
"""
|
||||
|
||||
import os
|
||||
import pandas as pd
|
||||
import xlsxwriter
|
||||
from config import REPORTS_DIR
|
||||
from services.reports.styles import safe_close_workbook, create_xlsx_format
|
||||
|
||||
|
||||
def export_raw_scud(df_scud, filename="СКУД_Сырые_данные.xlsx"):
|
||||
output_path = os.path.join(REPORTS_DIR, filename)
|
||||
target_dir = os.path.dirname(output_path)
|
||||
wb = xlsxwriter.Workbook(output_path)
|
||||
ws = wb.add_worksheet("Сырые_данные")
|
||||
|
||||
fmt_hdr = create_xlsx_format(wb, bold=True, bg_color='#D9E1F2', align='center')
|
||||
fmt_cell = create_xlsx_format(wb, align='left')
|
||||
|
||||
headers = list(df_scud.columns)
|
||||
ws.set_row(0, 28)
|
||||
for col_idx, header in enumerate(headers):
|
||||
ws.write(0, col_idx, str(header), fmt_hdr)
|
||||
|
||||
col_widths = [len(str(h)) for h in headers]
|
||||
|
||||
for row_idx, row_values in enumerate(df_scud.values, start=1):
|
||||
ws.set_row(row_idx, 19)
|
||||
for col_idx, val in enumerate(row_values):
|
||||
val_str = "" if (pd.isna(val) or val is None) else ("Да" if isinstance(val, bool) and val else ("Нет" if isinstance(val, bool) else str(val)))
|
||||
ws.write(row_idx, col_idx, val_str, fmt_cell)
|
||||
if len(val_str) > col_widths[col_idx]:
|
||||
col_widths[col_idx] = len(val_str)
|
||||
|
||||
for col_idx, width in enumerate(col_widths):
|
||||
ws.set_column(col_idx, col_idx, min(max(width + 3, 10), 45))
|
||||
|
||||
safe_close_workbook(wb, output_path, target_dir, filename)
|
||||
Reference in New Issue
Block a user