We can read ods file in python pandas using pandas-ods-reader python package. The read_ods() function helps in reading ods file. In this article, We will see the complete implementation step by step.
Read ods file in python pandas :
Reading ods file is a piece of cake if we use pandas-ods-reader package. But before anything, We need to install it into the system.
Step 1: Installation –
pip install pandas_ods_reader
This will install the latest version of pandas_ods_reader. We can also specify the version for pandas_ods_reader.
Step 2: Implementation –
When we need to implement the above-required functionality. We need to import the package first.
from pandas_ods_reader import read_ods
Lets invoke the read_ods() function. It will take the path of the ods to file with a few other parameters. It will return the data frame after it.
2.1 Using Sheet Index –
If we need to read the ods file using sheet index. You can do it in the same way.
base_path = "path/to/file.ods" sheet_index = 1 df = read_ods(base_path , sheet_index )
2.2 Using the Sheet name-
If we need to read the ods file using the sheet name, you need to tweak the above code.
sheet = "sheet_name" df = read_ods(base_path , sheet)
2.3 Load the odf file without header :
Here is the code snippet for trading ordinary odf file without a header.
df = read_ods(base_path, 1, headers=False)
the return object will be a dataframe.
2.4 Load the odf file with header ( pandas read ods ) :
If we have the odf file with some column name. Now if we have to pick a few columns out of them. Please refer to the below code. It uses ezodf to read the odf file. If the columns are duplicates then it will make them one. Also, it will add the numbers in the column name.
df = read_ods(base_path, 1, columns=["col1", "col2", "col3"])
I hope you must like this article. I think now you may easily read the odf file. If you are stuck somewhere in any of the steps, please let us know.
Additional Packages ( Alternatives) :
1. ezodf python module –
This package will help in reading and manipulating open document format files. You can install this package with the following command.
pip install ezodf
2. Pandas extension odfpyr –
This is an alternative package for pandas_ods_reader. All you need to install –
conda install -c conda-forge odfpyr
This will install odfpyr module.
It is compatible with pandas module (ods reader) .
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.