from openpyxl import Workbook from openpyxl.styles import Alignment from openpyxl.utils…
- User
from openpyxl import Workbook from openpyxl.styles import Alignment from openpyxl.utils import get_column_letter
def fill_sheet(sheet, sites): """ Fill the excel sheet with missed cells.
Args: sheet: openpyxl sheet object sites: list """ if len(sites) == 0: return columns = [ 'Region', 'ENM', 'ERBS_Name', 'ID', 'eNBId', 'Vlan', 'VlanRef', 'ExtEnodeB VlanRef', 'EranLink', 'PTP Time Sync', 'Elastic Ran LKF', 'Elastic Ran Feature', 'BBU', 'Port Bitrate', '10G LKF', '10G Feature', 'EUtranCells', 'Eran EUtranCells', ] row = 1 for col in columns: cell = sheet.cell(row=row, column=columns.index(col) + 1) cell.value = col row = 2 for site in sites: for col, cell_val in site.items(): col_index = columns.index(col) +1 cell = sheet.cell(row=row, column=col_index) cell.value = str(cell_val) if col in ['ExtEnodeB VlanRef', 'EranLink', 'EUtranCells', 'Eran EUtranCells']: cell.alignment = Alignment(wrap_text=True) row += 1Autofit columns except for specific columns
for column_cells in sheet.columns: column_letter = get_column_letter(column_cells[0].column) # Exclude the specific columns by checking the column letter if column_letter == 'Q': sheet.column_dimensions[column_letter].width = 15 elif column_letter == 'R': sheet.column_dimensions[column_letter].width = 17 elif column_letter == 'H': sheet.column_dimensions[column_letter].width = 31 elif column_letter == 'I': sheet.column_dimensions[column_letter].width = 21 else: max_length = 0 adjusted_width = 0 for cell in column_cells: if cell.coordinate in sheet.merged_cells: continue if cell.value: max_length = max(max_length, len(str(cell.value))) adjusted_width = (max_length + 1) sheet.column_dimensions[column_letter].width = adjusted_widthdef fill_excel(sites, path): """ Fill excel file with missed in atoll cells, each technology to separate sheet.
Args: selected_data: dict path: string """ work_book = Workbook() work_sheet = work_book.create_sheet('ElasticRan') fill_sheet(work_sheet, sites) del work_book['Sheet'] work_book.save(path) - Assistant
The code provided is a function that fills an Excel sheet with missed cells. The function takes two arguments: "sheet", which is the openpyxl sheet object, and "sites", which is a list of data to be filled in the sheet.
The function first checks if the "sites" list is empty. If it is, the function returns without doing anything.
Next, the function defines a list of column names that will be used as headers in the sheet. It then loops through the list of column names and sets the corresponding cell in the first row of the sheet to the column name.
After that, the function loops through each site in the "sites" list. For each site, it loops through the key-value pairs of the site dictionary. It gets the index of the column name in the list of column names and sets the corresponding cell in the current row to the cell value. If the column name is one of the specified columns, it also applies a text wrapping alignment to the cell.
- User
Decrease font size in columns Q and R and Fill column I with green color
- Assistant
To decrease the font size in columns Q and R and fill column I with green color, you can modify the code as follows:
First, import the necessary module and class:
from openpyxl.styles import Font, Color, PatternFill from openpyxl.styles.colors import GREENInside the
fill_sheetfunction, update the section where you autofit the columns:for column_cells in sheet.columns: column_letter = get_column_letter(column_cells[0].column) if column_letter == 'Q' or column_letter == 'R': sheet.column_dimensions[column_letter].width = 10 # Decrease font size by decreasing width elif column_letter == 'I': sheet.column_dimensions[column_letter].width = 21 ...Add the following before the
forloop in thefill_sheetfunction to fill column I with green color:green_fill = PatternFill(start_color=GREEN, end_color=GREEN, fill_type="solid") sheet.column_dimensions['I'].fill = green_fillWith these modifications, the font size will be decreased in columns Q and R, and column I will be filled with a green color.
- User
Fill I only if Enabled str is in cell
- Assistant
To fill column I with a green color only if the value "Enabled" is present in a cell, you can modify the code as follows:
Inside the
fill_sheetfunction, update the section where you autofit the columns. Also, add a check to fill column I only if the cell value contains "Enabled":green_fill = PatternFill(start_color=GREEN, end_color=GREEN, fill_type="solid") for column_cells in sheet.columns: column_letter = get_column_letter(column_cells[0].column) if column_letter == 'Q' or column_letter == 'R': sheet.column_dimensions[column_letter].width = 10 # Decrease font size by decreasing width elif column_letter == 'I': sheet.column_dimensions[column_letter].width = 21 for cell in column_cells: if cell.value == "Enabled": cell.fill = green_fillWith this modification, column I will be filled with a green color only if the cell value contains "Enabled".
- User
Sen font size to 1o for Q column
- Assistant
To decrease the font size in column Q to 10, you can modify the code as follows:
Inside the
fill_sheetfunction, update the section where you autofit the columns:from openpyxl.styles import Font for column_cells in sheet.columns: column_letter = get_column_letter(column_cells[0].column) if column_letter == 'Q': sheet.column_dimensions[column_letter].width = 10 # Decrease width of column Q for cell in column_cells: cell.font = Font(size=10) # Decrease font size to 10 elif column_letter == 'R': sheet.column_dimensions[column_letter].width = 17 elif column_letter == 'H': sheet.column_dimensions[column_letter].width = 31 elif column_letter == 'I': sheet.column_dimensions[column_letter].width = 21 else: max_length = 0 adjusted_width = 0 for cell in column_cells: if cell.coordinate in sheet.merged_cells: continue if cell.value: max_length = max(max_length, len(str(cell.value))) adjusted_width = (max_length + 1) sheet.column_dimensions[column_letter].width = adjusted_width
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.