Use right cells style in ODT table

This commit is contained in:
Кокос Артем Николаевич
2026-01-22 10:45:37 +07:00
parent a8511368ce
commit 9b260b27fd

View File

@@ -4,7 +4,7 @@ from redminelib.resources import Issue
from odf.opendocument import load from odf.opendocument import load
from odf.text import P from odf.text import P
from odf.table import Table, TableColumn, TableRow, TableCell from odf.table import Table, TableColumn, TableRow, TableCell
from odf.style import Style, TableColumnProperties from odf.style import Style, TableColumnProperties, TableCellProperties
from .formatter import get_version, hours_to_human, STATUS_TRANSLATION from .formatter import get_version, hours_to_human, STATUS_TRANSLATION
from .utils import get_month_name_from_range from .utils import get_month_name_from_range
@@ -43,28 +43,42 @@ def format_odt(
projects[project][version] = [] projects[project][version] = []
projects[project][version].append((issue, hours, status_ru)) projects[project][version].append((issue, hours, status_ru))
# Создаем стиль для каждой колонки с нужной шириной # Создаем стиль для ячеек таблицы
# Ширины из скриншота: 1.56", 1.63", 3.93", 1.56", 1.43" cell_style_name = "TableCellStyle"
cell_style = Style(name=cell_style_name, family="table-cell")
# Устанавливаем отступы (Padding)
cell_props = TableCellProperties(
padding="0.04in",
border="0.05pt solid #000000"
)
cell_style.addElement(cell_props)
doc.automaticstyles.addElement(cell_style)
# Создаем стиль для всей таблицы (опционально, но может понадобиться)
table_style_name = "ReportTableStyle"
table_style = Style(name=table_style_name, family="table")
# Создаем таблицу и применяем стиль
table = Table(name="Report", stylename=table_style_name)
# Добавляем стили для каждой колонки (ширины)
column_widths = ["1.56in", "1.63in", "3.93in", "1.56in", "1.43in"] column_widths = ["1.56in", "1.63in", "3.93in", "1.56in", "1.43in"]
# Создаем таблицу
table = Table(name="Report")
# Добавляем стили для каждой колонки
for i, width in enumerate(column_widths): for i, width in enumerate(column_widths):
col_style_name = f"col{i+1}" col_style_name = f"col{i+1}"
col_style = Style(name=col_style_name, family="table-column") col_style = Style(name=col_style_name, family="table-column")
col_props = TableColumnProperties(columnwidth=width, breakbefore="auto") col_props = TableColumnProperties(columnwidth=width, breakbefore="auto")
col_style.addElement(col_props) col_style.addElement(col_props)
doc.automaticstyles.addElement(col_style) doc.automaticstyles.addElement(col_style)
# Добавляем колонку с этим стилем
table.addElement(TableColumn(stylename=col_style)) table.addElement(TableColumn(stylename=col_style))
# Заголовки # Заголовки
header_row = TableRow() header_row = TableRow()
headers = ["Наименование Проекта", "Номер версии*", "Задача", "Статус Готовность*", "Затрачено за отчетный период"] headers = ["Наименование Проекта", "Номер версии*", "Задача", "Статус Готовность*", "Затрачено за отчетный период"]
for text in headers: for text in headers:
cell = TableCell() cell = TableCell(stylename=cell_style_name)
p = P(stylename=para_style_name, text=text) p = P(stylename=para_style_name, text=text)
cell.addElement(p) cell.addElement(p)
header_row.addElement(cell) header_row.addElement(cell)
@@ -84,7 +98,7 @@ def format_odt(
# Ячейка "Проект" - только в первой строке всего проекта # Ячейка "Проект" - только в первой строке всего проекта
if first_version_in_project and first_row_in_version: if first_version_in_project and first_row_in_version:
cell_project = TableCell() cell_project = TableCell(stylename=cell_style_name)
cell_project.setAttribute("numberrowsspanned", str(total_project_rows)) cell_project.setAttribute("numberrowsspanned", str(total_project_rows))
p = P(stylename=para_style_name, text=project) p = P(stylename=para_style_name, text=project)
cell_project.addElement(p) cell_project.addElement(p)
@@ -92,7 +106,7 @@ def format_odt(
# Ячейка "Версия" - только в первой строке каждой версии # Ячейка "Версия" - только в первой строке каждой версии
if first_row_in_version: if first_row_in_version:
cell_version = TableCell() cell_version = TableCell(stylename=cell_style_name)
cell_version.setAttribute("numberrowsspanned", str(row_span_version)) cell_version.setAttribute("numberrowsspanned", str(row_span_version))
p = P(stylename=para_style_name, text=version) p = P(stylename=para_style_name, text=version)
cell_version.addElement(p) cell_version.addElement(p)
@@ -103,17 +117,17 @@ def format_odt(
pass pass
# Остальные колонки # Остальные колонки
task_cell = TableCell() task_cell = TableCell(stylename=cell_style_name)
p = P(stylename=para_style_name, text=f"{issue.id}. {issue.subject}") p = P(stylename=para_style_name, text=f"{issue.id}. {issue.subject}")
task_cell.addElement(p) task_cell.addElement(p)
row.addElement(task_cell) row.addElement(task_cell)
status_cell = TableCell() status_cell = TableCell(stylename=cell_style_name)
p = P(stylename=para_style_name, text=status_ru) p = P(stylename=para_style_name, text=status_ru)
status_cell.addElement(p) status_cell.addElement(p)
row.addElement(status_cell) row.addElement(status_cell)
time_cell = TableCell() time_cell = TableCell(stylename=cell_style_name)
p = P(stylename=para_style_name, text=hours_to_human(hours)) p = P(stylename=para_style_name, text=hours_to_human(hours))
time_cell.addElement(p) time_cell.addElement(p)
row.addElement(time_cell) row.addElement(time_cell)