feat: add KS-6a Excel/Word export
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
"""Word renderer for KS-6a export using python-docx."""
|
||||
import io
|
||||
|
||||
from docx import Document
|
||||
from docx.enum.section import WD_ORIENT
|
||||
from docx.enum.table import WD_TABLE_ALIGNMENT
|
||||
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
||||
from docx.shared import Pt
|
||||
|
||||
from app.exporters.ks6a_dto import KS6aExportDocument
|
||||
|
||||
|
||||
def render_ks6a_word(doc: KS6aExportDocument) -> bytes:
|
||||
d = Document()
|
||||
h = doc.header
|
||||
|
||||
# Landscape page for the wide table
|
||||
section = d.sections[0]
|
||||
section.orientation = WD_ORIENT.LANDSCAPE
|
||||
section.page_width, section.page_height = section.page_height, section.page_width
|
||||
|
||||
# Title
|
||||
title = d.add_paragraph()
|
||||
title.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||
run = title.add_run("КС-6а — Журнал учёта выполненных работ")
|
||||
run.bold = True
|
||||
run.font.size = Pt(14)
|
||||
|
||||
d.add_paragraph()
|
||||
|
||||
# Header block
|
||||
header_lines = [
|
||||
("Проект:", h.project_name),
|
||||
("Заказчик:", h.customer),
|
||||
]
|
||||
if h.contractor:
|
||||
header_lines.append(("Подрядчик:", h.contractor))
|
||||
header_lines.append(("Договор:", h.contract_number))
|
||||
header_lines.append((
|
||||
"Период:",
|
||||
f"{h.period_from.strftime('%d.%m.%Y')} — {h.period_to.strftime('%d.%m.%Y')}",
|
||||
))
|
||||
if h.system_name:
|
||||
header_lines.append(("Система:", h.system_name))
|
||||
|
||||
for label, value in header_lines:
|
||||
p = d.add_paragraph()
|
||||
run = p.add_run(label + " ")
|
||||
run.bold = True
|
||||
p.add_run(value)
|
||||
|
||||
d.add_paragraph()
|
||||
|
||||
# Table — one row per DailyReportItem
|
||||
table = d.add_table(rows=1, cols=8)
|
||||
table.style = "Table Grid"
|
||||
table.alignment = WD_TABLE_ALIGNMENT.CENTER
|
||||
|
||||
columns = [
|
||||
"№", "Дата", "Система", "Раздел", "№ поз.",
|
||||
"Наименование работ", "Ед. изм.", "Количество",
|
||||
]
|
||||
for idx, col_name in enumerate(columns):
|
||||
cell = table.rows[0].cells[idx]
|
||||
cell.text = col_name
|
||||
for paragraph in cell.paragraphs:
|
||||
for run in paragraph.runs:
|
||||
run.bold = True
|
||||
run.font.size = Pt(9)
|
||||
|
||||
for item in doc.items:
|
||||
row = table.add_row()
|
||||
row.cells[0].text = str(item.row_number)
|
||||
row.cells[1].text = item.report_date.strftime("%d.%m.%Y")
|
||||
row.cells[2].text = item.system_name
|
||||
row.cells[3].text = item.section_name
|
||||
row.cells[4].text = item.source_number
|
||||
row.cells[5].text = item.name
|
||||
row.cells[6].text = item.unit
|
||||
row.cells[7].text = f"{item.quantity:.2f}"
|
||||
|
||||
buf = io.BytesIO()
|
||||
d.save(buf)
|
||||
buf.seek(0)
|
||||
return buf.read()
|
||||
Reference in New Issue
Block a user