Search code examples
pythonexcelcsvpyexcel

Converting .xls to .csv before recombining multiple files into a .xls


I am working on a webscraper tool which downloads excel files from a website. Of course, those .xls files are actually just renamed .csv files, which prevents me from just combining the .xls files together. Instead, I need to convert them all to .csv, them use pyexcel's pyexcel.merge_csv_to_a_book(filelist, outfilename='merged.xls') function to create a excel book from these .csv files.

Here is what I tried:

        def concatenate_excel_files():
        indexer = 0
        excel_file_list = []
        for file in glob.glob(os.getcwd()+'\Reports\*.'):
            pyexcel.save_as(file_name=file, dest_file_name=str(indexer)+'.csv')
            excel_file_list[indexer] = file
            indexer += 1
        pyexcel.merge_csv_to_a_book(excel_file_list, outfilename='merged.xls')

This fails to even convert the files to .csv (IndexError: list index out of range error.)

Any help rewriting this would be appreciated.


Solution

  • Answer by chfw:

    for pyexcel to work properly, it needs to know file extension but in your case, the file extension is missing. And it will more helpful if the full stack trace is shown.