How to convert OpenDocument spreadsheets to a pand

2020-02-17 00:41发布

The Python library can read Excel spreadsheets and convert them to a pandas.DataFrame with pandas.read_excel(file) command. Under the hood, it uses xlrd library which does not support ods files.

Is there an equivalent of pandas.read_excel for ods files? If not, how can I do the same for an Open Document Formatted spreadsheet (ods file)? ODF is used by LibreOffice and OpenOffice.

10条回答
趁早两清
2楼-- · 2020-02-17 01:13

I've had good luck with pandas read_clipboard. Selecting cells and then copy from excel or opendocument. In python run the following.

import pandas as pd
data = pd.read_clipboard()

Pandas will do a good job based on the cells copied.

查看更多
Melony?
3楼-- · 2020-02-17 01:15

Another option: read-ods-with-odfpy. This module takes an OpenDocument Spreadsheet as input, and returns a list, out of which a DataFrame can be created.

查看更多
虎瘦雄心在
4楼-- · 2020-02-17 01:15

If you only have a few .ods files to read, I would just open it in openoffice and save it as an excel file. If you have a lot of files, you could use the unoconv command in Linux to convert the .ods files to .xls programmatically (with bash)

Then it's really easy to read it in with pd.read_excel('filename.xls')

查看更多
干净又极端
5楼-- · 2020-02-17 01:17

Based heavily on the answer by davidovitch (thank you), I have put together a package that reads in a .ods file and returns a DataFrame. It's not a full implementation in pandas itself, such as his PR, but it provides a simple read_ods function that does the job.

You can install it with pip install pandas_ods_reader. It's also possible to specify whether the file contains a header row or not, and to specify custom column names.

查看更多
登录 后发表回答