XPMaster commited on
Commit
44dead3
·
1 Parent(s): 6ada886

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -28
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] # Truncate the sheet name to a maximum of 31 characters
379
  sheet = workbook.create_sheet(title=sheet_name)
380
-
381
- col_index = 1 # Initialize column index to 1 to start writing from the first column
382
- row_index = 1 # Initialize row index
 
383
 
384
  for title, data in data_dict.items():
385
- lst = data[0] # Extract the list of items to write
386
- color = data[1] if len(data) > 1 else None # Extract the color if it exists
387
- adjacent = data[2] if len(data) > 2 else False # Extract the adjacent flag if it exists, else it's False
388
-
389
- if adjacent:
390
- col_index = col_index # Keep the column index the same for adjacent
391
- row_index = sheet.max_row + 1 # Update row index to write data adjacent to the previous column
392
  else:
393
- col_index += 1 # Move to the next column for the next title
394
- row_index = 1 # Reset row index to start from the first row
395
 
396
- # Write the title
397
- title_cell = sheet.cell(row=row_index, column=col_index)
398
  title_cell.value = title
399
- title_cell.font = Font(size=14, bold=True) # Set the font size and bold
400
-
401
- # Write each item of the list in a new row and apply the color if provided
402
  for item_index, item in enumerate(lst, start=row_index + 1):
403
- cell = sheet.cell(row=item_index, column=col_index)
404
- cell.value = item # Write item in the cell
405
- if color: # If color is provided, apply it to the cell
406
  fill = PatternFill(start_color=color, end_color=color, fill_type="solid")
407
  cell.fill = fill
408
 
409
- # Adjust the column width
410
  max_length = 0
411
- for cell in sheet[get_column_letter(col_index)]:
412
- try: # Necessary to avoid error on empty cells
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(col_index)].width = adjusted_width
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