Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -332,42 +332,90 @@ def drop_missing_rows(df, columns):
|
|
| 332 |
|
| 333 |
# workbook.save('Log.xlsx')
|
| 334 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
def write_log(sheet_data_dict):
|
| 336 |
workbook = openpyxl.Workbook()
|
| 337 |
|
| 338 |
for sheet_name, data_dict in sheet_data_dict.items():
|
| 339 |
sheet = workbook.create_sheet(title=sheet_name)
|
| 340 |
|
|
|
|
|
|
|
|
|
|
| 341 |
for title, data in data_dict.items():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
# Write the title
|
| 343 |
-
sheet.
|
| 344 |
-
title_cell
|
| 345 |
title_cell.font = Font(size=14, bold=True) # Set the font size and bold
|
| 346 |
|
| 347 |
-
# Extract the list values and color
|
| 348 |
-
lst = data[0] # Extract the list of items to write
|
| 349 |
-
color = data[1] if len(data) > 1 else None # Extract the color if it exists
|
| 350 |
-
|
| 351 |
# Write each item of the list in a new row and apply the color if provided
|
| 352 |
-
for item in lst:
|
| 353 |
-
sheet.
|
| 354 |
-
|
|
|
|
| 355 |
fill = PatternFill(start_color=color, end_color=color, fill_type="solid")
|
| 356 |
-
cell = sheet.cell(row=sheet.max_row, column=1)
|
| 357 |
cell.fill = fill
|
| 358 |
|
| 359 |
-
|
| 360 |
-
for column in sheet.columns:
|
| 361 |
max_length = 0
|
| 362 |
-
|
| 363 |
-
for cell in column:
|
| 364 |
try: # Necessary to avoid error on empty cells
|
| 365 |
if len(str(cell.value)) > max_length:
|
| 366 |
max_length = len(cell.value)
|
| 367 |
except:
|
| 368 |
pass
|
| 369 |
adjusted_width = (max_length + 2)
|
| 370 |
-
sheet.column_dimensions[
|
| 371 |
|
| 372 |
# Remove the default sheet created and save the workbook
|
| 373 |
if "Sheet" in workbook.sheetnames:
|
|
@@ -375,6 +423,14 @@ def write_log(sheet_data_dict):
|
|
| 375 |
|
| 376 |
workbook.save('Log.xlsx')
|
| 377 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
warnings = []
|
| 379 |
def is_found(c,text):
|
| 380 |
global warnings
|
|
@@ -615,9 +671,9 @@ def process(files,button):
|
|
| 615 |
sheet_data[name] = {
|
| 616 |
"Before columns": [old_olds],
|
| 617 |
"After columns": [columns, '00FF00'],
|
| 618 |
-
'Filled quarters':[filled_warn],
|
| 619 |
"Warnings": sheetwarnings
|
| 620 |
-
}
|
| 621 |
|
| 622 |
c_name = name.split('.')[0]+'_cleaned.csv'
|
| 623 |
df.to_csv(c_name,index=False)
|
|
|
|
| 332 |
|
| 333 |
# workbook.save('Log.xlsx')
|
| 334 |
|
| 335 |
+
# def write_log(sheet_data_dict):
|
| 336 |
+
# workbook = openpyxl.Workbook()
|
| 337 |
+
|
| 338 |
+
# for sheet_name, data_dict in sheet_data_dict.items():
|
| 339 |
+
# sheet = workbook.create_sheet(title=sheet_name)
|
| 340 |
+
|
| 341 |
+
# for title, data in data_dict.items():
|
| 342 |
+
# # Write the title
|
| 343 |
+
# sheet.append([title])
|
| 344 |
+
# title_cell = sheet.cell(row=sheet.max_row, column=1)
|
| 345 |
+
# title_cell.font = Font(size=14, bold=True) # Set the font size and bold
|
| 346 |
+
|
| 347 |
+
# # Extract the list values and color
|
| 348 |
+
# lst = data[0] # Extract the list of items to write
|
| 349 |
+
# color = data[1] if len(data) > 1 else None # Extract the color if it exists
|
| 350 |
+
|
| 351 |
+
# # Write each item of the list in a new row and apply the color if provided
|
| 352 |
+
# for item in lst:
|
| 353 |
+
# sheet.append([item]) # Write item in a new row
|
| 354 |
+
# if color: # If color is provided, apply it to the row
|
| 355 |
+
# fill = PatternFill(start_color=color, end_color=color, fill_type="solid")
|
| 356 |
+
# cell = sheet.cell(row=sheet.max_row, column=1)
|
| 357 |
+
# cell.fill = fill
|
| 358 |
+
|
| 359 |
+
# # Adjust the column width
|
| 360 |
+
# for column in sheet.columns:
|
| 361 |
+
# max_length = 0
|
| 362 |
+
# column = [cell for cell in column]
|
| 363 |
+
# for cell in column:
|
| 364 |
+
# try: # Necessary to avoid error on empty cells
|
| 365 |
+
# if len(str(cell.value)) > max_length:
|
| 366 |
+
# max_length = len(cell.value)
|
| 367 |
+
# except:
|
| 368 |
+
# pass
|
| 369 |
+
# adjusted_width = (max_length + 2)
|
| 370 |
+
# sheet.column_dimensions[column[0].column_letter].width = adjusted_width
|
| 371 |
+
|
| 372 |
+
# # Remove the default sheet created and save the workbook
|
| 373 |
+
# if "Sheet" in workbook.sheetnames:
|
| 374 |
+
# workbook.remove(workbook["Sheet"])
|
| 375 |
+
|
| 376 |
+
# workbook.save('Log.xlsx')
|
| 377 |
+
|
| 378 |
def write_log(sheet_data_dict):
|
| 379 |
workbook = openpyxl.Workbook()
|
| 380 |
|
| 381 |
for sheet_name, data_dict in sheet_data_dict.items():
|
| 382 |
sheet = workbook.create_sheet(title=sheet_name)
|
| 383 |
|
| 384 |
+
col_index = 1 # Initialize column index to 1 to start writing from the first column
|
| 385 |
+
row_index = 1 # Initialize row index
|
| 386 |
+
|
| 387 |
for title, data in data_dict.items():
|
| 388 |
+
lst, color, adjacent = data[0], (data[1] if len(data) > 1 else None), data[2]
|
| 389 |
+
|
| 390 |
+
if adjacent: # Check if the key should be written adjacent to the previous key
|
| 391 |
+
row_index = max(row_index, sheet.max_row + 1) # Get the maximum row index used so far in the sheet + 1
|
| 392 |
+
else:
|
| 393 |
+
col_index += 1 # Move to the next column for the next title
|
| 394 |
+
row_index = 1 # Reset row index
|
| 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[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[column_letter(col_index)].width = adjusted_width
|
| 419 |
|
| 420 |
# Remove the default sheet created and save the workbook
|
| 421 |
if "Sheet" in workbook.sheetnames:
|
|
|
|
| 423 |
|
| 424 |
workbook.save('Log.xlsx')
|
| 425 |
|
| 426 |
+
def column_letter(index):
|
| 427 |
+
"""Convert a column index into a column letter"""
|
| 428 |
+
letters = ""
|
| 429 |
+
while index > 0:
|
| 430 |
+
index, remainder = divmod(index - 1, 26)
|
| 431 |
+
letters = chr(65 + remainder) + letters
|
| 432 |
+
return letters
|
| 433 |
+
|
| 434 |
warnings = []
|
| 435 |
def is_found(c,text):
|
| 436 |
global warnings
|
|
|
|
| 671 |
sheet_data[name] = {
|
| 672 |
"Before columns": [old_olds],
|
| 673 |
"After columns": [columns, '00FF00'],
|
| 674 |
+
'Filled quarters':[filled_warn,None,True],
|
| 675 |
"Warnings": sheetwarnings
|
| 676 |
+
}
|
| 677 |
|
| 678 |
c_name = name.split('.')[0]+'_cleaned.csv'
|
| 679 |
df.to_csv(c_name,index=False)
|