Having trouble with pandas to_excel writing only some files (for loop)

46 views Asked by At

I would like to save several .xlsx files with names coming from a pandas dataset.

import pandas as pd
from pandas import ExcelWriter # tried this because of the error, didn't solve anything

df_1 = pd.read_excel("an_excel_file.xlsx")
df_2 = pd.read_excel("another_excel_file.xlsx")

df_holder = df_1
table_number = 1


for i in range(1, 124):
    df_1.iat[0,2] = df_2.iat[i,4] # Name
    df_1.iat[0,5] = df_2.iat[i,5] # Direction
    df_1.iat[1,2] = df_2.iat[i,6] # Municipality
    df_1.iat[1,5] = df_2.iat[i,2] # Number
    df_1.iat[2,3] = table_number

    table_number = table_number + int(df_2.iat[i,7]) - 1
    df_1.iat[2,5] = table_number
    table_number += 1

    file_name = str(df_1.iat[0,2]) + ".xlsx"
    #file_name = file_name + ".xlsx"
    with ExcelWriter(r'Folder/'+file_name) as writer:
        df_plantilla.to_excel(writer, index = False) 
    
    df_1 = df_holder

As you can guess, this is supposed to create 123 .xlsx files. But no, it works for the fisrt three and the I get the following error:

OSError: Cannot save file into a non-existent directory: 'Folder\ESCUELA EP N°26'

I put the real name in case it has anything to do with that. Also, one of the first three files it wrote contains the '°' symbol, so it's not that.

Thanks in advance.

0

There are 0 answers