Change this Python code to: 1. set fixed width and height for every cell 2. enable word…
- User
Change this Python code to:
- set fixed width and height for every cell
- enable word wrap in cells
import openpyxl
def array_to_excel(array, filename='output.xlsx', sheet_name='Sheet1'): # Create a workbook and select the active worksheet workbook = openpyxl.Workbook() sheet = workbook.active sheet.title = sheet_name
# Iterate through the array and write each value to the Excel sheet for row_index, row in enumerate(array, start=1): for column_index, value in enumerate(row, start=1): sheet.cell(row=row_index, column=column_index, value=value) # Save the workbook to a file workbook.save(filename)Example usage
array = [ ['Header1', 'Header2', 'Header3'], [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
array_to_excel(array, 'example.xlsx')
- Assistant
To set fixed width and height for every cell and enable word wrap in cells using
openpyxl, you can modify your code as follows:import openpyxl from openpyxl.styles import Alignment def array_to_excel(array, filename='output.xlsx', sheet_name='Sheet1'): # Create a workbook and select the active worksheet workbook = openpyxl.Workbook() sheet = workbook.active sheet.title = sheet_name # Set fixed column width. Adjust '10' to the desired width. fixed_width = 15 for column in range(1, len(array[0]) + 1): column_letter = openpyxl.utils.get_column_letter(column) sheet.column_dimensions[column_letter].width = fixed_width # Set fixed row height. Adjust '20' to the desired height. fixed_height = 30 for row in range(1, len(array) + 1): sheet.row_dimensions[row].height = fixed_height # Iterate through the array and write each value to the Excel sheet for row_index, row in enumerate(array, start=1): for column_index, value in enumerate(row, start=1): cell = sheet.cell(row=row_index, column=column_index, value=value) # Enable word wrap in cells cell.alignment = Alignment(wrap_text=True) # Save the workbook to a file workbook.save(filename) # Example usage array = [ ['Header1', 'Header2', 'Header3'], [1, 2, 3], [4, 5, 6], [7, 8, 9] ] array_to_excel(array, 'example.xlsx') - User
cell.alignment = Alignment(wrap_text=True) ^^^^^^^^^ NameError: name 'Alignment' is not defined
- Assistant
If you're encountering a
NameErrorforAlignment, it suggests that the import statement forAlignmentmight be missing or incorrect in your script. To fix this, ensure that you have importedAlignmentcorrectly fromopenpyxl.styles. Here's the updated code with the correct import statement:import openpyxl from openpyxl.styles import Alignment # Ensure this import is present def array_to_excel(array, filename='output.xlsx', sheet_name='Sheet1'): # Create a workbook and select the active worksheet workbook = openpyxl.Workbook() sheet = workbook.active sheet.title = sheet_name # Set fixed column width. Adjust '15' to the desired width. fixed_width = 15 for column in range(1, len(array[0]) + 1): column_letter = openpyxl.utils.get_column_letter(column) sheet.column_dimensions[column_letter].width = fixed_width # Set fixed row height. Adjust '30' to the desired height. fixed_height = 30 for row in range(1, len(array) + 1): sheet.row_dimensions[row].height = fixed_height # Iterate through the array and write each value to the Excel sheet for row_index, row in enumerate(array, start=1): for column_index, value in enumerate(row, start=1): cell = sheet.cell(row=row_index, column=column_index, value=value) # Enable word wrap in cells cell.alignment = Alignment(wrap_text=True) # Save the workbook to a file workbook.save(filename) # Example usage array = [ ['Header1', 'Header2', 'Header3'], [1, 2, 3], [4, 5, 6], [7, 8, 9] ] array_to_excel(array, 'example.xlsx')
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.