How to convert OpenDocument spreadsheets to a pandas DataFrame?

前端 未结 11 1634
日久生厌
日久生厌 2020-12-23 19:40

The Python library pandas can read Excel spreadsheets and convert them to a pandas.DataFrame with pandas.read_excel(file) command. Under the hood,

11条回答
  •  星月不相逢
    2020-12-23 19:58

    Here is a quick and dirty hack which uses ezodf module:

    import pandas as pd
    import ezodf
    
    def read_ods(filename, sheet_no=0, header=0):
        tab = ezodf.opendoc(filename=filename).sheets[sheet_no]
        return pd.DataFrame({col[header].value:[x.value for x in col[header+1:]]
                             for col in tab.columns()})
    

    Test:

    In [92]: df = read_ods(filename='fn.ods')
    
    In [93]: df
    Out[93]:
         a    b    c
    0  1.0  2.0  3.0
    1  4.0  5.0  6.0
    2  7.0  8.0  9.0
    

    NOTES:

    • all other useful parameters like header, skiprows, index_col, parse_cols are NOT implemented in this function - feel free to update this question if you want to implement them
    • ezodf depends on lxml make sure you have it installed

提交回复
热议问题