'Conversion xlsx to csv python with saving the xlsx file name and adding the sheet number (python)

I am converting xlsx format, documents have several sheets, to csv. When I do this, the files start using the name that is specified in the nested sheet. I need to make sure that the csv files that I get as output have the names of the original file with the addition of a serial number.

That is, I have the following file:

Assessment of the population in places of traditional residence of indigenous peoples of the Russian Federation.xlsx

And I get the following output:

List_1.csv
List_2.csv
List_3.csv

What I need is that I get this:

Assessment of the population in places of traditional residence of indigenous peoples of the Russian Federation_1.csv
Assessment of the population in places of traditional residence of indigenous peoples of the Russian Federation_2.csv
Assessment of the population in places of traditional residence of indigenous peoples of the Russian Federation_3.csv

Here is the code that I am using, probably something like a counter needs to be done here:

def xlsx_files_list(directory):

    xlsx_files = [f for f in os.listdir(directory) if f.endswith('.xlsx')]
    if xlsx_files == []:
        return False
    else:
        return xlsx_files

def xlsx_to_csv(xlsx_files, directory):

    try:
        df = pd.read_excel(xlsx_files, sheet_name=None)
        sheets = df.keys()

        for sheets, value in df.items():
            df[sheets].to_csv(directory + '%s.csv' % sheets)

    except Exception as e:
        logging.error('%s => error convert xlsx to csv', sheets)
        print(e)
    else:
        print('%s => convert xlsx to csv', sheets)


xlsx_files = xlsx_files_list(directory)
if xlsx_files != False:
   for list_xlsx in xlsx_files:
      xlsx_to_csv(xlsx_files=directory + list_xlsx, directory=directory)

it is probably necessary to add something like a sheet counter to the function xlsx_to_csv, but I got confused out of the blue.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source