Skip to content

Commit

Permalink
address #69: a bit more info on save_book_as
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Jan 16, 2017
1 parent f7560c7 commit 45a24d0
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 113 deletions.
3 changes: 1 addition & 2 deletions Makefile
Expand Up @@ -4,5 +4,4 @@ test:
bash test.sh

document:
sphinx-autogen -o doc/source/generated/ doc/source/*.rst
sphinx-build -b html doc/source/ doc/build/
bash document.sh
102 changes: 1 addition & 101 deletions docs/source/tutorial02.rst
Expand Up @@ -20,6 +20,7 @@ Column 1 Column 2 Column 3
.. testcode::
:hide:

>>> import os
>>> data = [
... ["Column 1", "Column 2", "Column 3"],
... [1, 2, 3],
Expand Down Expand Up @@ -128,108 +129,7 @@ And then apply the filter on the sheet:
Work with multi-sheet file
--------------------------

How do I read a book, process it and save to a new book
******************************************************

Yes, you can do that. The code looks like this::

import pyexcel

book = pyexcel.get_book(file_name="yourfile.xls")
for sheet in book:
# do you processing with sheet
# do filtering?
pass
book.save_as("output.xls")
What would happen if I save a multi sheet book into "csv" file
**************************************************************

Well, you will get one csv file per each sheet. Suppose you have these code:

.. code-block:: python
>>> content = {
... 'Sheet 1':
... [
... [1.0, 2.0, 3.0],
... [4.0, 5.0, 6.0],
... [7.0, 8.0, 9.0]
... ],
... 'Sheet 2':
... [
... ['X', 'Y', 'Z'],
... [1.0, 2.0, 3.0],
... [4.0, 5.0, 6.0]
... ],
... 'Sheet 3':
... [
... ['O', 'P', 'Q'],
... [3.0, 2.0, 1.0],
... [4.0, 3.0, 2.0]
... ]
... }
>>> book = pyexcel.Book(content)
>>> book.save_as("myfile.csv")
You will end up with three csv files:

.. code-block:: python
>>> import glob
>>> outputfiles = glob.glob("myfile_*.csv")
>>> for file in sorted(outputfiles):
... print(file)
...
myfile__Sheet 1__0.csv
myfile__Sheet 2__1.csv
myfile__Sheet 3__2.csv
and their content is the value of the dictionary at the corresponding key


After I have saved my multiple sheet book in csv format, how do I get them back in pyexcel
*******************************************************************************************

First of all, you can read them back individual as csv file using `meth:~pyexcel.get_sheet` method. Secondly, the pyexcel can do
the magic to load all of them back into a book. You will just need to provide the common name before the separator "__":

.. code-block:: python
>>> book2 = pyexcel.get_book(file_name="myfile.csv")
>>> book2
Sheet 1:
+-----+-----+-----+
| 1.0 | 2.0 | 3.0 |
+-----+-----+-----+
| 4.0 | 5.0 | 6.0 |
+-----+-----+-----+
| 7.0 | 8.0 | 9.0 |
+-----+-----+-----+
Sheet 2:
+-----+-----+-----+
| X | Y | Z |
+-----+-----+-----+
| 1.0 | 2.0 | 3.0 |
+-----+-----+-----+
| 4.0 | 5.0 | 6.0 |
+-----+-----+-----+
Sheet 3:
+-----+-----+-----+
| O | P | Q |
+-----+-----+-----+
| 3.0 | 2.0 | 1.0 |
+-----+-----+-----+
| 4.0 | 3.0 | 2.0 |
+-----+-----+-----+
.. testcode::
:hide:

>>> os.unlink("myfile__Sheet 1__0.csv")
>>> os.unlink("myfile__Sheet 2__1.csv")
>>> os.unlink("myfile__Sheet 3__2.csv")
>>> os.unlink("example_series.xls")
117 changes: 114 additions & 3 deletions docs/source/tutorial04.rst
@@ -1,4 +1,3 @@

Book: Sheet operations
=========================

Expand Down Expand Up @@ -107,17 +106,129 @@ merge sheets into a single sheet

Suppose you want to merge many csv files row by row into a new sheet.

>>> import pyexcel as pe
>>> import glob
>>> merged = pyexcel.Sheet()
>>> for file in glob.glob("*.csv"):
... merged.row += pe.get_sheet(file_name=file)
... merged.row += pyexcel.get_sheet(file_name=file)
>>> merged.save_as("merged.csv")

How do I read a book, process it and save to a new book
--------------------------------------------------------------------------------

Yes, you can do that. The code looks like this::

import pyexcel

book = pyexcel.get_book(file_name="yourfile.xls")
for sheet in book:
# do you processing with sheet
# do filtering?
pass
book.save_as("output.xls")
What would happen if I save a multi sheet book into "csv" file
--------------------------------------------------------------------------------

Well, you will get one csv file per each sheet. Suppose you have these code:

.. code-block:: python
>>> content = {
... 'Sheet 1':
... [
... [1.0, 2.0, 3.0],
... [4.0, 5.0, 6.0],
... [7.0, 8.0, 9.0]
... ],
... 'Sheet 2':
... [
... ['X', 'Y', 'Z'],
... [1.0, 2.0, 3.0],
... [4.0, 5.0, 6.0]
... ],
... 'Sheet 3':
... [
... ['O', 'P', 'Q'],
... [3.0, 2.0, 1.0],
... [4.0, 3.0, 2.0]
... ]
... }
>>> book = pyexcel.Book(content)
>>> book.save_as("myfile.csv")
You will end up with three csv files:

.. code-block:: python
>>> import glob
>>> outputfiles = glob.glob("myfile_*.csv")
>>> for file in sorted(outputfiles):
... print(file)
...
myfile__Sheet 1__0.csv
myfile__Sheet 2__1.csv
myfile__Sheet 3__2.csv
and their content is the value of the dictionary at the corresponding key


.. testcode::
:hide:

>>> import os
>>> os.unlink("myfile__Sheet 1__0.csv")
>>> os.unlink("myfile__Sheet 2__1.csv")
>>> os.unlink("myfile__Sheet 3__2.csv")

Alternatively, you could use :meth:`~pyexcel.save_book_as` function

.. code-block:: python
>>> pyexcel.save_book_as(bookdict=content, dest_file_name="myfile.csv")
After I have saved my multiple sheet book in csv format, how do I get them back
--------------------------------------------------------------------------------

First of all, you can read them back individual as csv file using `meth:~pyexcel.get_sheet` method. Secondly, the pyexcel can do
the magic to load all of them back into a book. You will just need to provide the common name before the separator "__":

.. code-block:: python
>>> book2 = pyexcel.get_book(file_name="myfile.csv")
>>> book2
Sheet 1:
+-----+-----+-----+
| 1.0 | 2.0 | 3.0 |
+-----+-----+-----+
| 4.0 | 5.0 | 6.0 |
+-----+-----+-----+
| 7.0 | 8.0 | 9.0 |
+-----+-----+-----+
Sheet 2:
+-----+-----+-----+
| X | Y | Z |
+-----+-----+-----+
| 1.0 | 2.0 | 3.0 |
+-----+-----+-----+
| 4.0 | 5.0 | 6.0 |
+-----+-----+-----+
Sheet 3:
+-----+-----+-----+
| O | P | Q |
+-----+-----+-----+
| 3.0 | 2.0 | 1.0 |
+-----+-----+-----+
| 4.0 | 3.0 | 2.0 |
+-----+-----+-----+
.. testcode::
:hide:

>>> import os
>>> os.unlink("myfile__Sheet 1__0.csv")
>>> os.unlink("myfile__Sheet 2__1.csv")
>>> os.unlink("myfile__Sheet 3__2.csv")
>>> os.unlink("book.xls")
>>> os.unlink("book1.xls")
>>> os.unlink("book2.xlsx")
Expand Down
19 changes: 12 additions & 7 deletions pyexcel/book.py
Expand Up @@ -45,7 +45,7 @@ def load_from_sheets(self, sheets):
Load content from existing sheets
:param dict sheets: a dictionary of sheets. Each sheet is
a list of lists
a list of lists
"""
self.__sheets = compact.OrderedDict()
if sheets is None:
Expand Down Expand Up @@ -166,7 +166,8 @@ def __add__(self, other):
return output

def __iadd__(self, other):
"""Operator overloading +=
"""
Operator overloading +=
example::
Expand Down Expand Up @@ -216,14 +217,16 @@ def __str__(self):
return self.__repr__()

def save_as(self, filename):
"""Save the content to a new file
"""
Save the content to a new file
:param str filename: a file path
:param filename: a file path
"""
return save_book(self, file_name=filename)

def save_to_memory(self, file_type, stream=None, **keywords):
"""Save the content to a memory stream
"""
Save the content to a memory stream
:param file_type: what format the stream is in
:param stream: a memory stream. Note in Python 3, for csv and tsv
Expand All @@ -237,7 +240,8 @@ def save_to_memory(self, file_type, stream=None, **keywords):
def save_to_django_models(self, models,
initializers=None, mapdicts=None,
batch_size=None):
"""Save to database table through django model
"""
Save to database table through django model
:param models: a list of database models, that is accepted by
:meth:`Sheet.save_to_django_model`. The sequence
Expand All @@ -259,7 +263,8 @@ def save_to_django_models(self, models,
def save_to_database(self, session, tables,
initializers=None, mapdicts=None,
auto_commit=True):
"""Save data in sheets to database tables
"""
Save data in sheets to database tables
:param session: database session
:param tables: a list of database tables, that is accepted by
Expand Down

0 comments on commit 45a24d0

Please sign in to comment.