Загрузка данных


@app.route("/employee/<tabnum>/export_worker")
def export_worker(tabnum):
    try:
        conn = get_db_connection()
        cur = conn.cursor()
        cur.execute("SELECT fam, imja, otch FROM rabotnik WHERE Tabnum = %s", (tabnum,))
        worker = cur.fetchone()
        cur.close()
        conn.close()

        if not worker:
            return "Работник не найден"

        if not os.path.exists(TEMPLATE_PATH):
            return f"Файл шаблона {TEMPLATE_PATH} не найден."

        rb = xlrd.open_workbook(TEMPLATE_PATH, formatting_info=True)
        wb = xl_copy(rb)
        ws = wb.get_sheet(0)

        # Строки для ФИО (индексы 14, 15, 16)
        rows = [14, 15, 16]   # 15, 16, 17 в Excel
        start_col = 3         # колонка D (индекс 3)

        fam, imja, otch = worker

        # Пишем фамилию по буквам в строку 15
        for i, ch in enumerate(fam):
            if start_col + i < 256:  # защита от выхода за границы
                ws.write(rows[0], start_col + i, ch)

        # Пишем имя по буквам в строку 16
        for i, ch in enumerate(imja):
            if start_col + i < 256:
                ws.write(rows[1], start_col + i, ch)

        # Пишем отчество по буквам в строку 17
        for i, ch in enumerate(otch):
            if start_col + i < 256:
                ws.write(rows[2], start_col + i, ch)

        # Сохраняем во временный файл и отдаём
        fd, tmp_path = tempfile.mkstemp(suffix=".xls")
        os.close(fd)
        wb.save(tmp_path)
        return send_file(tmp_path, as_attachment=True,
                         download_name=f"BLANKS6_{tabnum}.xls",
                         mimetype="application/vnd.ms-excel")

    except Exception as e:
        return f"<h2>Ошибка экспорта</h2><pre>{traceback.format_exc()}</pre>"