27 lines
869 B
Python
27 lines
869 B
Python
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from docx import Document
|
|
from docx.shared import RGBColor
|
|
|
|
|
|
async def write_docx(filename: Path, data: dict[str, Any]) -> str:
|
|
doc = Document()
|
|
try:
|
|
for idx, table in data["tables"].items():
|
|
doc.add_paragraph(f"Table {idx}")
|
|
table = doc.add_table(rows=len(table), cols=len(table[0]))
|
|
|
|
for i, row in enumerate(data["tables"][idx]):
|
|
for j, col in enumerate(row):
|
|
table.cell(i, j).text = col if type(col) is str else str(col[0])
|
|
if isinstance(col, tuple):
|
|
table.cell(i, j).paragraphs[0].runs[0].font.color.rgb = (
|
|
RGBColor(255, 0, 0)
|
|
)
|
|
|
|
doc.save(str(filename))
|
|
return "Ok"
|
|
except Exception as e:
|
|
return str(e)
|