Write Single Array Dimension to Excel Sheet

  • 1 year ago
Comment

ChatGPTChatGPT
1 / 1

To write a single array dimension to an Excel sheet, you can use the openpyxl library in Python. This library allows you to work with Excel files (.xlsx) and provides a simple way to write data to Excel sheets. If you don't have openpyxl installed, you can install it using pip:

bash
pip install openpyxl

Here's an example of how to write a single array to an Excel sheet:

python
import openpyxl

def write_array_to_excel(data_array, output_file):
# Create a new workbook and sheet
workbook = openpyxl.Workbook()
sheet = workbook.active

# Write the data to the first row of the sheet
for col, value in enumerate(data_array, start=1):
sheet.cell(row=1, column=col, value=value)

# Save the workbook to the specified output file
workbook.save(output_file)

# Example usage:
data = [1, 2, 3, 4, 5]
output_file = "output.xlsx"
write_array_to_excel(data, output_file)

In this example, the write_array_to_excel function takes two parameters: data_array (the single-dimensional array to write) and output_file (the path of the output Excel file). The function creates a new workbook, writes the array data to the first row of the sheet, and saves the workbook to the output file.

Make sure to adjust the data variable to contain the array that you want to write to the Excel sheet, and customize the output_file variable with the desired path and filename for the output Excel file.

With this code, you can easily write a single array dimension to an Excel sheet using Python.