pythoncsvcsvkit

Csvkit : use "in2csv" in python code


I'd like to use in2csv to convert excel files into csv in my python code, but without running it as a separate process using exec. Instead, I'd like to directly import the function and use it, like in2csv("input.xls", "output.csv"), or still better, get directly the structure of the file in python without using the csv reader out the output file.

Is it possible?

Thank you!


Solution

  • I also had the same issue. I looked into the source code of in2csv and found it internally uses agate and agateexcel. I directly used agate and implemented the following way

    For xls file

    import agate
    import agateexcel
    
    table = agate.Table.from_xls('input.xls', sheet='sheet')
    
    table.to_csv('output.csv')
    

    For xlsx file

    import agate
    import agateexcel
    
    table = agate.Table.from_xlsx('input.xlsx', sheet='sheet')
    
    table.to_csv('output.csv')
    

    https://github.com/wireservice/csvkit/blob/master/csvkit/utilities/in2csv.py