Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -375,52 +375,50 @@ def write_log(sheet_data_dict):
|
|
| 375 |
workbook = openpyxl.Workbook()
|
| 376 |
max_sheet_name_length = 31
|
| 377 |
for sheet_name, data_dict in sheet_data_dict.items():
|
| 378 |
-
sheet_name = sheet_name[:max_sheet_name_length]
|
| 379 |
sheet = workbook.create_sheet(title=sheet_name)
|
| 380 |
-
|
| 381 |
-
col_index = 1 #
|
| 382 |
-
|
|
|
|
| 383 |
|
| 384 |
for title, data in data_dict.items():
|
| 385 |
-
lst = data[0]
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
row_index = sheet.max_row + 1 # Update row index to write data adjacent to the previous column
|
| 392 |
else:
|
| 393 |
-
|
| 394 |
-
row_index = 1
|
| 395 |
|
| 396 |
-
# Write
|
| 397 |
-
title_cell = sheet.cell(row=row_index, column=
|
| 398 |
title_cell.value = title
|
| 399 |
-
title_cell.font = Font(size=14, bold=True)
|
| 400 |
-
|
| 401 |
-
# Write
|
| 402 |
for item_index, item in enumerate(lst, start=row_index + 1):
|
| 403 |
-
cell = sheet.cell(row=item_index, column=
|
| 404 |
-
cell.value = item
|
| 405 |
-
if color:
|
| 406 |
fill = PatternFill(start_color=color, end_color=color, fill_type="solid")
|
| 407 |
cell.fill = fill
|
| 408 |
|
| 409 |
-
# Adjust
|
| 410 |
max_length = 0
|
| 411 |
-
for cell in sheet[get_column_letter(
|
| 412 |
-
try:
|
| 413 |
if len(str(cell.value)) > max_length:
|
| 414 |
max_length = len(cell.value)
|
| 415 |
except:
|
| 416 |
pass
|
| 417 |
adjusted_width = (max_length + 2)
|
| 418 |
-
sheet.column_dimensions[get_column_letter(
|
| 419 |
-
|
| 420 |
-
# Remove the default sheet created and save the workbook
|
| 421 |
if "Sheet" in workbook.sheetnames:
|
| 422 |
workbook.remove(workbook["Sheet"])
|
| 423 |
-
|
| 424 |
workbook.save('Log.xlsx')
|
| 425 |
|
| 426 |
|
|
|
|
| 375 |
workbook = openpyxl.Workbook()
|
| 376 |
max_sheet_name_length = 31
|
| 377 |
for sheet_name, data_dict in sheet_data_dict.items():
|
| 378 |
+
sheet_name = sheet_name[:max_sheet_name_length]
|
| 379 |
sheet = workbook.create_sheet(title=sheet_name)
|
| 380 |
+
|
| 381 |
+
col_index = 1 # Start from column 1 (A), column 0 does not exist in Excel
|
| 382 |
+
adjacent_col_index = 2 # Initialize adjacent column index to 2 (B)
|
| 383 |
+
row_index = 1 # Initialize row index to 1 to start writing from the first row
|
| 384 |
|
| 385 |
for title, data in data_dict.items():
|
| 386 |
+
lst, color = data[0], (data[1] if len(data) > 1 else None)
|
| 387 |
+
adjacent = data[2] if len(data) > 2 else False
|
| 388 |
+
|
| 389 |
+
if adjacent:
|
| 390 |
+
write_col_index = adjacent_col_index # Use adjacent column
|
| 391 |
+
adjacent_col_index += 1 # Increment adjacent column index for next adjacent data
|
|
|
|
| 392 |
else:
|
| 393 |
+
write_col_index = col_index # Use column 1 (A) for non-adjacent data
|
| 394 |
+
row_index = sheet.max_row + 1 if sheet.max_row > 0 else 1 # Start from next available row in column 1
|
| 395 |
|
| 396 |
+
# Write title
|
| 397 |
+
title_cell = sheet.cell(row=row_index, column=write_col_index)
|
| 398 |
title_cell.value = title
|
| 399 |
+
title_cell.font = Font(size=14, bold=True)
|
| 400 |
+
|
| 401 |
+
# Write list items and apply color
|
| 402 |
for item_index, item in enumerate(lst, start=row_index + 1):
|
| 403 |
+
cell = sheet.cell(row=item_index, column=write_col_index)
|
| 404 |
+
cell.value = item
|
| 405 |
+
if color:
|
| 406 |
fill = PatternFill(start_color=color, end_color=color, fill_type="solid")
|
| 407 |
cell.fill = fill
|
| 408 |
|
| 409 |
+
# Adjust column width
|
| 410 |
max_length = 0
|
| 411 |
+
for cell in sheet[get_column_letter(write_col_index)]:
|
| 412 |
+
try:
|
| 413 |
if len(str(cell.value)) > max_length:
|
| 414 |
max_length = len(cell.value)
|
| 415 |
except:
|
| 416 |
pass
|
| 417 |
adjusted_width = (max_length + 2)
|
| 418 |
+
sheet.column_dimensions[get_column_letter(write_col_index)].width = adjusted_width
|
| 419 |
+
|
|
|
|
| 420 |
if "Sheet" in workbook.sheetnames:
|
| 421 |
workbook.remove(workbook["Sheet"])
|
|
|
|
| 422 |
workbook.save('Log.xlsx')
|
| 423 |
|
| 424 |
|