ODT table support

This commit is contained in:
Artem Kokos
2026-01-20 23:25:08 +07:00
committed by Артём Кокос
parent 6fcc834617
commit bca24189c7
4 changed files with 175 additions and 9 deletions

View File

@@ -32,6 +32,10 @@ def main(argv: Optional[List[str]] = None) -> int:
action="store_true",
help="Use compact plain-text output instead of table"
)
parser.add_argument(
"--output",
help="Path to output .odt file (e.g., report.odt). If omitted, prints to stdout."
)
args = parser.parse_args(argv)
try:
@@ -58,15 +62,31 @@ def main(argv: Optional[List[str]] = None) -> int:
print(f"✅ Total issues: {len(issue_hours)} [{args.date}]")
try:
if args.compact:
output = format_compact(issue_hours)
else:
output = format_table(issue_hours)
print(output)
except Exception as e:
print(f"❌ Formatting error: {e}", file=sys.stderr)
return 1
if args.output:
if not args.output.endswith(".odt"):
print("❌ Output file must end with .odt", file=sys.stderr)
return 1
try:
from .formatter_odt import format_odt
doc = format_odt(issue_hours)
doc.save(args.output)
print(f"✅ Report saved to {args.output}")
except ImportError:
print("❌ odfpy is not installed. Install with: pip install odfpy", file=sys.stderr)
return 1
except Exception as e:
print(f"❌ ODT export error: {e}", file=sys.stderr)
return 1
else:
try:
if args.compact:
output = format_compact(issue_hours)
else:
output = format_table(issue_hours)
print(output)
except Exception as e:
print(f"❌ Formatting error: {e}", file=sys.stderr)
return 1
return 0

View File

@@ -0,0 +1,72 @@
from typing import List, Tuple
from redminelib.resources import Issue
from odf.opendocument import OpenDocumentText
from odf.style import Style, TableProperties, TableCellProperties, ParagraphProperties
from odf.text import P
from odf.table import Table, TableColumn, TableRow, TableCell
from .formatter import get_version, hours_to_human, STATUS_TRANSLATION
def format_odt(issue_hours: List[Tuple[Issue, float]]) -> OpenDocumentText:
doc = OpenDocumentText()
# Стили
table_style = Style(name="Table", family="table")
table_style.addElement(TableProperties(width="17cm", align="center"))
doc.styles.addElement(table_style)
cell_style = Style(name="Cell", family="table-cell")
cell_style.addElement(TableCellProperties(border="0.5pt solid #000000"))
doc.styles.addElement(cell_style)
para_style = Style(name="Para", family="paragraph")
para_style.addElement(ParagraphProperties(textalign="left"))
doc.styles.addElement(para_style)
# Таблица
table = Table(name="Report", stylename=table_style)
for _ in range(5): # 5 колонок
table.addElement(TableColumn())
# Заголовок
header_row = TableRow()
headers = ["Проект", "Версия", "Задача", "Статус", "Затрачено"]
for text in headers:
cell = TableCell(stylename=cell_style)
p = P(stylename=para_style, text=text)
cell.addElement(p)
header_row.addElement(cell)
table.addElement(header_row)
# Данные
prev_project = None
prev_version = None
for issue, hours in issue_hours:
project = str(issue.project)
version = get_version(issue)
status_en = str(issue.status)
status_ru = STATUS_TRANSLATION.get(status_en, status_en)
display_project = project if project != prev_project else ""
display_version = version if (project != prev_project or version != prev_version) else ""
row = TableRow()
for col_text in [
display_project,
display_version,
f"{issue.id}. {issue.subject}",
status_ru,
hours_to_human(hours)
]:
cell = TableCell(stylename=cell_style)
p = P(stylename=para_style, text=col_text)
cell.addElement(p)
row.addElement(cell)
table.addElement(row)
prev_project = project
prev_version = version
doc.text.addElement(table)
return doc