Upload multiple files pandas

Question:

I need help, I can’t figure out what I need to do.

The task is as follows, I need to be able to select multiple xlsx files and convert them to csv.

In my code I got the ability to do this with only one file. I hope for your help and advice. Thanks


path_file = sg.popup_get_file('Choose files',

                                   title='Choose files',

                                   file_types=(('Excel Files', '*.xlsx'),),

                                   multiple_files=True)

df = pd.read_excel(path_file, dtype='str', engine='openpyxl')

new_header = []

for i in list(df.columns):

    new_header.append(i.upper())

df.columns = new_header

for col in df.columns:

    df[col] = df[col].str.replace('"', '')

 

df.to_csv(path_file[:-5] + '.csv',index=False, encoding='utf-8', sep=';', lineterminator='rn')

Asked By: Alex

||

Answers:

Without option no_window=True, it will return None or str-type filepaths with separator ';' between each file_path.

import PySimpleGUI as sg

path_files = sg.popup_get_file(
    'Choose files',
    title='Choose files',
    file_types=(('Excel Files', '*.xlsx'),),
    multiple_files=True,
)
if path_files:
    for path_file in path_files.split(';'):
        print(path_file)    # Do the conversion here
else:
    print('No file selected !')
Answered By: Jason Yang
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.