| 290 | doc.save(output, True) |
| 291 | |
| 292 | def write_xlsx(self, output: str) -> None: |
| 293 | workbook = Workbook() |
| 294 | |
| 295 | cell_formats: dict[str, PatternFill] = {} |
| 296 | for key, value in self.config.get("colours", {}).items(): |
| 297 | fill = PatternFill(start_color=value, end_color=value, fill_type="solid") |
| 298 | cell_formats[key] = fill |
| 299 | |
| 300 | for category, data in self.categories.items(): |
| 301 | category_data = self.config.get("categories", {}).get(category, {}) |
| 302 | colours = category_data.get("colours", ()) |
| 303 | |
| 304 | if category in workbook.sheetnames: |
| 305 | worksheet = workbook[category] |
| 306 | else: |
| 307 | worksheet = workbook.create_sheet(category) |
| 308 | |
| 309 | if category_colour := category_data.get("colour", None): |
| 310 | worksheet.sheet_properties.tabColor = cell_formats[category_colour].start_color.rgb |
| 311 | |
| 312 | r = 1 # Openpyxl uses 1-based indexing |
| 313 | c = 1 |
| 314 | for header in data["headers"]: |
| 315 | cell = worksheet.cell(row=r, column=c, value=header) |
| 316 | cell.fill = cell_formats["h"] |
| 317 | c += 1 |
| 318 | |
| 319 | r += 1 |
| 320 | for row in data["rows"]: |
| 321 | c = 1 |
| 322 | for col in row: |
| 323 | if c > len(colours): # Adjusted the comparison |
| 324 | cell_format = "n" |
| 325 | else: |
| 326 | cell_format = colours[c - 1] # Adjusted the indexing |
| 327 | cell = worksheet.cell(row=r, column=c, value=str(col)) |
| 328 | cell.fill = cell_formats[cell_format] |
| 329 | c += 1 |
| 330 | r += 1 |
| 331 | |
| 332 | if "Sheet" in workbook.sheetnames and len(workbook.sheetnames) > 1: |
| 333 | workbook.remove(workbook["Sheet"]) |
| 334 | |
| 335 | workbook.save(output) |
| 336 | |
| 337 | def write_pd(self) -> dict[str, pd.DataFrame]: |
| 338 | results = {} |