Skip to content
Advertisement

How to convert multiple excel files to CSV utf-8 encoding using python

I have 30+ xlsx files in same directory and using python I would like to convert all files to csv with utf-8 encoding, regardless of whatever encoding is present in the file. I am using python’s magic library to get the file names (below code).For conversion, I tried the code mention by SO user Julian here (I used the code posted here), but the code is throwing an error saying "InvalidFileException: openpyxl does not support file format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,.xltx,.xltm. Below is the code that is throwing an error.The second issue is based on my limited python knowledge I believe code will work for one excel file. How should I make it work for multiple files ?

Thanks in advance for your help!

JavaScript

Code throwing error from SO User github link mentioned here

JavaScript

Advertisement

Answer

In first, the first error is obvious: InvalidFileException: openpyxl does not support file format, please check you can open it with Excel first.

Does Excel successfully open this file? If yes, we need the workbook (or small part of it).

The answer to the second question:

JavaScript

Also you can explicitly specify the dialect of csv as csv.writer parameter.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement