utf8 error when opening xlsx file created using pandas.excelwriter and openpyxl

47 views Asked by At

import pandas as pd
import openpyxl
wb = openpyxl.Workbook() 
wb.save(filename='Test.xlsx')

import openpyxl
# Read names from Y.csv
y_data = ['a','b','c']

# Iterate through each row in Y.csv
for i in range(len(y_data)):
    sheet_name = y_data[i]

    data = {
    'Column1': [
        2016.1, 2016.2, 2016.3, 2016.4, 2017.1, 2017.2, 2017.3, 2017.4,
        2018.1, 2018.2, 2018.3, 2018.4, 2019.1, 2019.2, 2019.3, 2019.4,
        2020.1, 2020.2, 2020.3, 2020.4, 2021.1, 2021.2, 2021.3, 2021.4,
        2022.1, 2022.2, 2022.3, 2022.4
    ]
    }
    df = pd.DataFrame(data)
    print(df)
    
    # Write the sheet to the Excel file
    with pd.ExcelWriter('Test.xlsx', mode='a',engine='openpyxl') as writer:
        df.to_excel(writer, sheet_name=y_data[i], index=False,encoding='utf8')

print(f"Created {len(y_data)} spreadsheets in output.xlsx.")
is not UTF-8 encoded

Saving disabled.

See Console for more details.

I'm trying to make n spreadsheets in a single file with their sheet_names from some array and duplicate the first column across the n-spreadsheets but I can't get the files to open.

I tried downloading Test.xlsx after running, but Excel doesn't open it. Says corrupted file.

0

There are 0 answers