debian-python-pyexcel-ods/README.rst

388 lines
11 KiB
ReStructuredText
Raw Normal View History

2015-08-20 20:03:24 +02:00
================================================================================
2015-01-27 00:47:17 +01:00
pyexcel-ods - Let you focus on data, instead of ods format
2015-08-20 20:03:24 +02:00
================================================================================
2014-10-13 00:49:24 +02:00
2017-04-13 23:07:57 +02:00
.. image:: https://raw.githubusercontent.com/pyexcel/pyexcel.github.io/master/images/patreon.png
:target: https://www.patreon.com/chfw
.. image:: https://raw.githubusercontent.com/pyexcel/pyexcel-mobans/master/images/awesome-badge.svg
:target: https://awesome-python.com/#specific-formats-processing
.. image:: https://github.com/pyexcel/pyexcel-ods/workflows/run_tests/badge.svg
:target: http://github.com/pyexcel/pyexcel-ods/actions
2014-12-18 01:12:55 +01:00
2017-08-20 02:12:28 +02:00
.. image:: https://codecov.io/gh/pyexcel/pyexcel-ods/branch/master/graph/badge.svg
:target: https://codecov.io/gh/pyexcel/pyexcel-ods
.. image:: https://badge.fury.io/py/pyexcel-ods.svg
:target: https://pypi.org/project/pyexcel-ods
.. image:: https://pepy.tech/badge/pyexcel-ods/month
:target: https://pepy.tech/project/pyexcel-ods
2017-08-20 02:12:28 +02:00
.. image:: https://img.shields.io/gitter/room/gitterHQ/gitter.svg
:target: https://gitter.im/pyexcel/Lobby
2017-04-13 23:07:57 +02:00
2020-10-08 23:46:46 +02:00
.. image:: https://img.shields.io/static/v1?label=continuous%20templating&message=%E6%A8%A1%E7%89%88%E6%9B%B4%E6%96%B0&color=blue&style=flat-square
:target: https://moban.readthedocs.io/en/latest/#at-scale-continous-templating-for-open-source-projects
.. image:: https://img.shields.io/static/v1?label=coding%20style&message=black&color=black&style=flat-square
:target: https://github.com/psf/black
2017-05-07 19:00:50 +02:00
2015-08-20 20:03:24 +02:00
**pyexcel-ods** is a tiny wrapper library to read, manipulate and write data in
2016-02-07 19:50:11 +01:00
ods format using python 2.6 and python 2.7. You are likely to use it with
2016-01-17 17:26:39 +01:00
`pyexcel <https://github.com/pyexcel/pyexcel>`_.
`pyexcel-ods3 <https://github.com/pyexcel/pyexcel-ods3>`_ is a sister library that
2017-02-02 22:14:45 +01:00
depends on ezodf and lxml. `pyexcel-odsr <https://github.com/pyexcel/pyexcel-odsr>`_
is the other sister library that has no external dependency but do ods reading only
2014-10-15 18:28:17 +02:00
Support the project
================================================================================
If your company has embedded pyexcel and its components into a revenue generating
product, please support me on github, `patreon <https://www.patreon.com/bePatron?u=5537627>`_
or `bounty source <https://salt.bountysource.com/teams/chfw-pyexcel>`_ to maintain
the project and develop it further.
If you are an individual, you are welcome to support me too and for however long
you feel like. As my backer, you will receive
`early access to pyexcel related contents <https://www.patreon.com/pyexcel/posts>`_.
And your issues will get prioritized if you would like to become my patreon as `pyexcel pro user`.
With your financial support, I will be able to invest
a little bit more time in coding, documentation and writing interesting posts.
2015-01-27 00:47:17 +01:00
Known constraints
2016-02-07 00:56:46 +01:00
==================
2015-01-27 00:47:17 +01:00
Fonts, colors and charts are not supported.
2015-01-27 00:47:17 +01:00
Nor to read password protected xls, xlsx and ods files.
2014-10-15 18:28:17 +02:00
Installation
2016-01-23 23:30:31 +01:00
================================================================================
2014-10-15 18:28:17 +02:00
You can install pyexcel-ods via pip:
2015-09-09 00:35:47 +02:00
.. code-block:: bash
2014-10-15 18:28:17 +02:00
$ pip install pyexcel-ods
2016-02-07 00:56:46 +01:00
2015-09-09 00:35:47 +02:00
or clone it and install it:
.. code-block:: bash
2014-10-15 18:28:17 +02:00
2017-05-07 14:18:04 +02:00
$ git clone https://github.com/pyexcel/pyexcel-ods.git
2014-10-27 01:15:38 +01:00
$ cd pyexcel-ods
2014-10-15 18:28:17 +02:00
$ python setup.py install
2014-10-13 00:49:24 +02:00
Usage
2016-01-23 23:30:31 +01:00
================================================================================
2014-10-13 00:49:24 +02:00
2014-10-15 18:28:17 +02:00
As a standalone library
2016-01-23 23:30:31 +01:00
--------------------------------------------------------------------------------
.. testcode::
2015-08-19 22:02:24 +02:00
:hide:
2016-08-25 18:58:42 +02:00
>>> import os
2015-08-19 22:02:24 +02:00
>>> import sys
>>> if sys.version_info[0] < 3:
... from StringIO import StringIO
... else:
... from io import BytesIO as StringIO
>>> PY2 = sys.version_info[0] == 2
>>> if PY2 and sys.version_info[1] < 7:
... from ordereddict import OrderedDict
... else:
... from collections import OrderedDict
2015-08-19 22:02:24 +02:00
2017-02-02 22:07:42 +01:00
Write to an ods file
********************************************************************************
2015-09-09 00:35:47 +02:00
Here's the sample code to write a dictionary to an ods file:
.. code-block:: python
2014-11-20 10:09:56 +01:00
2015-05-12 15:05:29 +02:00
>>> from pyexcel_ods import save_data
2016-01-23 23:30:31 +01:00
>>> data = OrderedDict() # from collections import OrderedDict
2014-11-20 10:09:56 +01:00
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
>>> data.update({"Sheet 2": [["row 1", "row 2", "row 3"]]})
2015-05-12 15:05:29 +02:00
>>> save_data("your_file.ods", data)
2014-11-20 10:09:56 +01:00
2017-02-02 22:07:42 +01:00
2014-10-15 18:28:17 +02:00
Read from an ods file
2016-01-23 23:30:31 +01:00
********************************************************************************
2014-10-15 18:28:17 +02:00
2015-09-09 00:35:47 +02:00
Here's the sample code:
.. code-block:: python
2014-10-15 18:28:17 +02:00
2015-05-12 15:05:29 +02:00
>>> from pyexcel_ods import get_data
>>> data = get_data("your_file.ods")
2014-11-20 10:09:56 +01:00
>>> import json
>>> print(json.dumps(data))
{"Sheet 1": [[1, 2, 3], [4, 5, 6]], "Sheet 2": [["row 1", "row 2", "row 3"]]}
2014-10-15 18:28:17 +02:00
2016-08-25 18:58:42 +02:00
2016-01-23 23:30:31 +01:00
Write an ods to memory
********************************************************************************
2014-10-15 18:28:17 +02:00
2015-09-09 00:35:47 +02:00
Here's the sample code to write a dictionary to an ods file:
.. code-block:: python
2014-10-15 18:28:17 +02:00
2015-05-12 15:05:29 +02:00
>>> from pyexcel_ods import save_data
2014-11-20 10:09:56 +01:00
>>> data = OrderedDict()
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
>>> data.update({"Sheet 2": [[7, 8, 9], [10, 11, 12]]})
>>> io = StringIO()
2015-05-12 15:05:29 +02:00
>>> save_data(io, data)
>>> # do something with the io
2014-11-20 10:09:56 +01:00
>>> # In reality, you might give it to your http response
>>> # object for downloading
2014-10-15 18:28:17 +02:00
2016-01-23 23:30:31 +01:00
2017-02-02 22:07:42 +01:00
Read from an ods from memory
2016-01-23 23:30:31 +01:00
********************************************************************************
2016-01-23 23:30:31 +01:00
Continue from previous example:
2015-09-09 00:35:47 +02:00
.. code-block:: python
2014-11-20 10:09:56 +01:00
>>> # This is just an illustration
>>> # In reality, you might deal with ods file upload
2015-08-20 20:03:24 +02:00
>>> # where you will read from requests.FILES['YOUR_ODS_FILE']
2015-05-12 15:05:29 +02:00
>>> data = get_data(io)
>>> print(json.dumps(data))
{"Sheet 1": [[1, 2, 3], [4, 5, 6]], "Sheet 2": [[7, 8, 9], [10, 11, 12]]}
2016-08-23 18:53:30 +02:00
Pagination feature
********************************************************************************
Special notice 30/01/2017: due to the constraints of the underlying 3rd party
library, it will read the whole file before returning the paginated data. So
at the end of day, the only benefit is less data returned from the reading
function. No major performance improvement will be seen.
2017-02-02 22:07:42 +01:00
With that said, please install `pyexcel-odsr <https://github.com/pyexcel/pyexcel-odsr>`_
2017-02-02 22:14:45 +01:00
and it gives better performance in pagination.
2017-02-02 22:07:42 +01:00
2016-08-23 18:53:30 +02:00
Let's assume the following file is a huge ods file:
2016-08-26 00:52:55 +02:00
.. code-block:: python
2016-08-23 18:53:30 +02:00
>>> huge_data = [
... [1, 21, 31],
... [2, 22, 32],
... [3, 23, 33],
... [4, 24, 34],
... [5, 25, 35],
... [6, 26, 36]
... ]
>>> sheetx = {
... "huge": huge_data
... }
>>> save_data("huge_file.ods", sheetx)
And let's pretend to read partial data:
.. code-block:: python
>>> partial_data = get_data("huge_file.ods", start_row=2, row_limit=3)
2016-08-26 00:52:55 +02:00
>>> print(json.dumps(partial_data))
{"huge": [[3, 23, 33], [4, 24, 34], [5, 25, 35]]}
2016-08-23 18:53:30 +02:00
And you could as well do the same for columns:
.. code-block:: python
>>> partial_data = get_data("huge_file.ods", start_column=1, column_limit=2)
2016-08-26 00:52:55 +02:00
>>> print(json.dumps(partial_data))
{"huge": [[21, 31], [22, 32], [23, 33], [24, 34], [25, 35], [26, 36]]}
2016-08-23 18:53:30 +02:00
Obvious, you could do both at the same time:
.. code-block:: python
>>> partial_data = get_data("huge_file.ods",
2016-08-23 19:05:09 +02:00
... start_row=2, row_limit=3,
2016-08-23 18:53:30 +02:00
... start_column=1, column_limit=2)
2016-08-26 00:52:55 +02:00
>>> print(json.dumps(partial_data))
{"huge": [[23, 33], [24, 34], [25, 35]]}
2016-08-23 18:53:30 +02:00
2016-08-25 18:58:42 +02:00
.. testcode::
:hide:
>>> os.unlink("huge_file.ods")
2016-08-23 18:53:30 +02:00
2014-10-15 18:28:17 +02:00
As a pyexcel plugin
2016-01-23 23:30:31 +01:00
--------------------------------------------------------------------------------
2014-10-15 18:28:17 +02:00
No longer, explicit import is needed since pyexcel version 0.2.2. Instead,
this library is auto-loaded. So if you want to read data in ods format,
installing it is enough.
2016-08-25 18:58:42 +02:00
2014-10-13 00:50:35 +02:00
Reading from an ods file
2016-01-23 23:30:31 +01:00
********************************************************************************
2014-10-13 00:49:24 +02:00
2015-09-09 00:35:47 +02:00
Here is the sample code:
.. code-block:: python
2014-10-13 00:49:24 +02:00
2014-11-20 10:09:56 +01:00
>>> import pyexcel as pe
>>> sheet = pe.get_book(file_name="your_file.ods")
2014-11-20 10:09:56 +01:00
>>> sheet
2016-05-20 21:32:03 +02:00
Sheet 1:
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
Sheet 2:
2014-11-20 10:09:56 +01:00
+-------+-------+-------+
| row 1 | row 2 | row 3 |
+-------+-------+-------+
2014-10-13 00:49:24 +02:00
2016-08-25 18:58:42 +02:00
2014-10-13 00:50:35 +02:00
Writing to an ods file
2016-01-23 23:30:31 +01:00
********************************************************************************
2014-10-13 00:49:24 +02:00
2015-09-09 00:35:47 +02:00
Here is the sample code:
.. code-block:: python
2014-10-13 00:49:24 +02:00
2014-11-20 10:09:56 +01:00
>>> sheet.save_as("another_file.ods")
2014-10-13 00:49:24 +02:00
2016-08-25 18:58:42 +02:00
Reading from a IO instance
2016-08-25 18:58:42 +02:00
********************************************************************************
2016-01-23 23:30:31 +01:00
You got to wrap the binary content with stream to get ods working:
2015-09-09 00:35:47 +02:00
.. code-block:: python
2014-11-20 10:09:56 +01:00
>>> # This is just an illustration
2015-08-20 20:03:24 +02:00
>>> # In reality, you might deal with ods file upload
>>> # where you will read from requests.FILES['YOUR_ODS_FILE']
>>> odsfile = "another_file.ods"
>>> with open(odsfile, "rb") as f:
2014-11-20 10:09:56 +01:00
... content = f.read()
... r = pe.get_book(file_type="ods", file_content=content)
2014-11-20 10:09:56 +01:00
... print(r)
...
2016-05-20 21:32:03 +02:00
Sheet 1:
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
Sheet 2:
2014-11-20 10:09:56 +01:00
+-------+-------+-------+
| row 1 | row 2 | row 3 |
+-------+-------+-------+
Writing to a StringIO instance
2016-08-25 18:58:42 +02:00
********************************************************************************
2015-09-09 00:35:47 +02:00
You need to pass a StringIO instance to Writer:
.. code-block:: python
2014-11-20 10:09:56 +01:00
>>> data = [
... [1, 2, 3],
... [4, 5, 6]
... ]
>>> io = StringIO()
>>> sheet = pe.Sheet(data)
>>> io = sheet.save_to_memory("ods", io)
2014-11-20 10:09:56 +01:00
>>> # then do something with io
>>> # In reality, you might give it to your http response
>>> # object for downloading
2017-02-02 22:07:42 +01:00
2015-02-21 23:33:01 +01:00
License
2016-01-23 23:30:31 +01:00
================================================================================
2015-02-21 23:33:01 +01:00
New BSD License
2016-02-07 01:02:20 +01:00
Developer guide
==================
Development steps for code changes
#. git clone https://github.com/pyexcel/pyexcel-ods.git
#. cd pyexcel-ods
2016-08-31 09:41:51 +02:00
Upgrade your setup tools and pip. They are needed for development and testing only:
2017-04-13 23:07:57 +02:00
#. pip install --upgrade setuptools pip
2016-08-31 09:41:51 +02:00
Then install relevant development requirements:
#. pip install -r rnd_requirements.txt # if such a file exists
#. pip install -r requirements.txt
#. pip install -r tests/requirements.txt
2017-06-22 11:09:38 +02:00
Once you have finished your changes, please provide test case(s), relevant documentation
and update CHANGELOG.rst.
2017-06-22 11:09:38 +02:00
.. note::
As to rnd_requirements.txt, usually, it is created when a dependent
2017-08-20 02:12:28 +02:00
library is not released. Once the dependecy is installed
(will be released), the future
version of the dependency in the requirements.txt will be valid.
2017-06-22 11:09:38 +02:00
How to test your contribution
------------------------------
Although `nose` and `doctest` are both used in code testing, it is adviable that unit tests are put in tests. `doctest` is incorporated only to make sure the code examples in documentation remain valid across different development releases.
On Linux/Unix systems, please launch your tests like this::
$ make
On Windows systems, please issue this command::
> test.bat
Before you commit
------------------------------
Please run::
$ make format
so as to beautify your code otherwise travis-ci may fail your unit test.
2014-10-13 00:49:24 +02:00
Credits
2016-01-23 23:30:31 +01:00
================================================================================
2014-10-13 00:49:24 +02:00
2014-10-15 18:28:17 +02:00
ODSReader is originally written by `Marco Conti <https://github.com/marcoconti83/read-ods-with-odfpy>`_
2014-11-20 10:09:56 +01:00
2016-01-23 23:30:31 +01:00
.. testcode::
2015-08-19 22:02:24 +02:00
:hide:
>>> import os
>>> os.unlink("your_file.ods")
>>> os.unlink("another_file.ods")