feat: polish document exports and fix filename extensions

This commit is contained in:
Арсентьев Александр
2026-09-27 17:18:15 +03:00
parent d8e92386d9
commit b3741ca106
14 changed files with 920 additions and 112 deletions
+21 -17
View File
@@ -2,10 +2,12 @@
import io
from docx import Document
from docx.shared import Pt, Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH
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.common import MONEY_TEXT, QTY_TEXT, format_period, project_requisite_lines
from app.exporters.ks2_dto import KS2ExportDocument
@@ -13,6 +15,10 @@ def render_ks2_word(doc: KS2ExportDocument) -> bytes:
d = Document()
h = doc.header
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
@@ -22,17 +28,15 @@ def render_ks2_word(doc: KS2ExportDocument) -> bytes:
d.add_paragraph()
# Header block
header_lines = [
("Проект:", h.project_name),
("Заказчик:", h.customer),
("Договор:", h.contract_number),
]
if h.period_from and h.period_to:
header_lines.append(("Период:", f"{h.period_from.strftime('%d.%m.%Y')} — {h.period_to.strftime('%d.%m.%Y')}"))
if h.comment:
header_lines.append(("Комментарий:", h.comment))
header_lines = project_requisite_lines(
project_name=h.project_name,
customer=h.customer,
contract_number=h.contract_number,
contract_date=h.contract_date,
currency=h.currency,
period_text=format_period(h.period_from, h.period_to),
extra=[("Комментарий:", h.comment)],
)
for label, value in header_lines:
p = d.add_paragraph()
run = p.add_run(label + " ")
@@ -61,9 +65,9 @@ def render_ks2_word(doc: KS2ExportDocument) -> bytes:
row.cells[1].text = item.name
row.cells[2].text = item.source_number
row.cells[3].text = item.unit
row.cells[4].text = f"{item.quantity:.4f}"
row.cells[5].text = f"{item.unit_price:.2f}"
row.cells[6].text = f"{item.amount:.2f}"
row.cells[4].text = QTY_TEXT.format(item.quantity)
row.cells[5].text = MONEY_TEXT.format(item.unit_price)
row.cells[6].text = MONEY_TEXT.format(item.amount)
# Total row
total_row = table.add_row()
@@ -73,7 +77,7 @@ def render_ks2_word(doc: KS2ExportDocument) -> bytes:
paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT
for run in paragraph.runs:
run.bold = True
total_row.cells[6].text = f"{doc.total:.2f}"
total_row.cells[6].text = MONEY_TEXT.format(doc.total)
for paragraph in total_row.cells[6].paragraphs:
for run in paragraph.runs:
run.bold = True
+22 -21
View File
@@ -2,10 +2,12 @@
import io
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
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.common import MONEY_TEXT, format_date, format_period, project_requisite_lines
from app.exporters.ks3_dto import KS3ExportDocument
@@ -13,26 +15,28 @@ def render_ks3_word(doc: KS3ExportDocument) -> bytes:
d = Document()
h = doc.header
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(f"КС-3 №{h.ks3_number} от {h.ks3_date.strftime('%d.%m.%Y')}")
run = title.add_run(f"Акт КС-3 №{h.ks3_number} от {h.ks3_date.strftime('%d.%m.%Y')}")
run.bold = True
run.font.size = Pt(14)
d.add_paragraph()
# Header block
header_lines = [
("Проект:", h.project_name),
("Заказчик:", h.customer),
("Договор:", h.contract_number),
]
if h.period_from and h.period_to:
header_lines.append(("Период:", f"{h.period_from.strftime('%d.%m.%Y')} — {h.period_to.strftime('%d.%m.%Y')}"))
if h.comment:
header_lines.append(("Комментарий:", h.comment))
header_lines = project_requisite_lines(
project_name=h.project_name,
customer=h.customer,
contract_number=h.contract_number,
contract_date=h.contract_date,
currency=h.currency,
period_text=format_period(h.period_from, h.period_to),
extra=[("Комментарий:", h.comment)],
)
for label, value in header_lines:
p = d.add_paragraph()
run = p.add_run(label + " ")
@@ -59,12 +63,9 @@ def render_ks3_word(doc: KS3ExportDocument) -> bytes:
row = table.add_row()
row.cells[0].text = str(item.row_number)
row.cells[1].text = f"№{item.ks2_number}"
row.cells[2].text = item.ks2_date.strftime('%d.%m.%Y') if item.ks2_date else ""
period_text = ""
if item.period_from and item.period_to:
period_text = f"{item.period_from.strftime('%d.%m.%Y')} — {item.period_to.strftime('%d.%m.%Y')}"
row.cells[3].text = period_text
row.cells[4].text = f"{item.amount:.2f}"
row.cells[2].text = format_date(item.ks2_date)
row.cells[3].text = format_period(item.period_from, item.period_to)
row.cells[4].text = MONEY_TEXT.format(item.amount)
# Total row
total_row = table.add_row()
@@ -74,7 +75,7 @@ def render_ks3_word(doc: KS3ExportDocument) -> bytes:
paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT
for run in paragraph.runs:
run.bold = True
total_row.cells[4].text = f"{doc.total:.2f}"
total_row.cells[4].text = MONEY_TEXT.format(doc.total)
for paragraph in total_row.cells[4].paragraphs:
for run in paragraph.runs:
run.bold = True
+15 -14
View File
@@ -7,6 +7,7 @@ from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Pt
from app.exporters.common import QTY_TEXT, format_date, format_period, project_requisite_lines
from app.exporters.ks6a_dto import KS6aExportDocument
@@ -28,21 +29,21 @@ def render_ks6a_word(doc: KS6aExportDocument) -> bytes:
d.add_paragraph()
# Header block
header_lines = [
("Проект:", h.project_name),
("Заказчик:", h.customer),
]
extra = []
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')}",
))
extra.append(("Подрядчик:", h.contractor))
if h.system_name:
header_lines.append(("Система:", h.system_name))
extra.append(("Система:", h.system_name))
header_lines = project_requisite_lines(
project_name=h.project_name,
customer=h.customer,
contract_number=h.contract_number,
contract_date=h.contract_date,
currency=h.currency,
period_text=format_period(h.period_from, h.period_to),
extra=extra,
)
for label, value in header_lines:
p = d.add_paragraph()
run = p.add_run(label + " ")
@@ -71,13 +72,13 @@ def render_ks6a_word(doc: KS6aExportDocument) -> bytes:
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[1].text = format_date(item.report_date)
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}"
row.cells[7].text = QTY_TEXT.format(item.quantity)
buf = io.BytesIO()
d.save(buf)