Skip to content

pyexcel/pyexcel

Repository files navigation

pyexcel - Let you focus on data, instead of file formats

image

image

image

image

image

image

image

image

image

image

image

image

Support the project

If your company has embedded pyexcel and its components into a revenue generating product, please support me on github, patreon or bounty source 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.

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.

Known constraints

Fonts, colors and charts are not supported.

Nor to read password protected xls, xlsx and ods files.

Introduction

Feature Highlights

A list of supported file formats
file format definition
csv comma separated values
tsv tab separated values
csvz a zip file that contains one or many csv files
tsvz a zip file that contains one or many tsv files

xls

a spreadsheet file format created by MS-Excel 97-2003

xlsx

MS-Excel Extensions to the Office Open XML SpreadsheetML File Format.

xlsm an MS-Excel Macro-Enabled Workbook file
ods open document spreadsheet
fods flat open document spreadsheet
json java script object notation
html html table of the data structure
simple simple presentation
rst rStructured Text presentation of the data
mediawiki media wiki table

image

  1. One application programming interface(API) to handle multiple data sources:
    • physical file
    • memory file
    • SQLAlchemy table
    • Django Model
    • Python data structures: dictionary, records and array
  2. One API to read and write data in various excel file formats.
  3. For large data sets, data streaming are supported. A genenerator can be returned to you. Checkout iget_records, iget_array, isave_as and isave_book_as.

Installation

You can install pyexcel via pip:

or clone it and install it:

One liners

This section shows you how to get data from your excel files and how to export data to excel files in one line

Read from the excel files

Get a list of dictionaries

Suppose you want to process History of Classical Music:

History of Classical Music:

Name Period Representative Composers
Medieval c.1150-c.1400 Machaut, Landini
Renaissance c.1400-c.1600 Gibbons, Frescobaldi
Baroque c.1600-c.1750 JS Bach, Vivaldi
Classical c.1750-c.1830 Joseph Haydn, Wolfgan Amadeus Mozart
Early Romantic c.1830-c.1860 Chopin, Mendelssohn, Schumann, Liszt
Late Romantic c.1860-c.1920 Wagner,Verdi
Modernist 20th century Sergei Rachmaninoff,Calude Debussy

Let's get a list of dictionary out from the xls file:

And let's check what do we have:

Get two dimensional array

Instead, what if you have to use pyexcel.get_array to do the same:

where start_row skips the header row.

Get a dictionary

You can get a dictionary too:

And let's have a look inside:

Please note that my_dict is an OrderedDict.

Get a dictionary of two dimensional array

Suppose you have a multiple sheet book as the following:

Top Violinist:

Name Period Nationality
Antonio Vivaldi 1678-1741 Italian
Niccolo Paganini 1782-1840 Italian
Pablo de Sarasate 1852-1904 Spainish
Eugene Ysaye 1858-1931 Belgian
Fritz Kreisler 1875-1962 Astria-American
Jascha Heifetz 1901-1987 Russian-American
David Oistrakh 1908-1974 Russian
Yehundi Menuhin 1916-1999 American
Itzhak Perlman 1945- Israeli-American
Hilary Hahn 1979- American

Noteable Violin Makers:

Maker Period Country
Antonio Stradivari 1644-1737 Cremona, Italy
Giovanni Paolo Maggini 1580-1630 Botticino, Italy
Amati Family 1500-1740 Cremona, Italy
Guarneri Family 1626-1744 Cremona, Italy
Rugeri Family 1628-1719 Cremona, Italy
Carlo Bergonzi 1683-1747 Cremona, Italy
Jacob Stainer 1617-1683 Austria

Most Expensive Violins:

Name Estimated Value Location
Messiah Stradivarious $ 20,000,000 Ashmolean Museum in Oxford, England
Vieuxtemps Guarneri $ 16,000,000 On loan to Anne Akiko Meyers
Lady Blunt $ 15,900,000 Anonymous bidder

Here is the code to obtain those sheets as a single dictionary:

And check:

Write data

Export an array

Suppose you have the following array:

And here is the code to save it as an excel file :

Let's verify it:

And here is the code to save it as a csv file :

Let's verify it:

Export a list of dictionaries

Export a dictionary of single key value pair

Export a dictionary of single dimensonal array

Export a dictionary of two dimensional array as a book

Suppose you want to save the below dictionary to an excel file :

Here is the code:

If you want to preserve the order of sheets in your dictionary, you have to pass on an ordered dictionary to the function itself. For example:

Let's verify its order:

Please notice that "Sheet 2" is the first item in the book_dict, meaning the order of sheets are preserved.

Transcoding

Note

Please note that pyexcel-cli can perform file transcoding at command line. No need to open your editor, save the problem, then python run.

The following code does a simple file format transcoding from xls to csv:

Again it is really simple. Let's verify what we have gotten:

Note

Please note that csv(comma separate value) file is pure text file. Formula, charts, images and formatting in xls file will disappear no matter which transcoding tool you use. Hence, pyexcel is a quick alternative for this transcoding job.

Let use previous example and save it as xlsx instead

Again let's verify what we have gotten:

Excel book merge and split operation in one line

Merge all excel files in directory into a book where each file become a sheet

The following code will merge every excel files into one file, say "output.xls":

You can mix and match with other excel formats: xls, xlsm and ods. For example, if you are sure you have only xls, xlsm, xlsx, ods and csv files in your_excel_file_directory, you can do the following:

Split a book into single sheet files

Suppose you have many sheets in a work book and you would like to separate each into a single sheet excel file. You can easily do this:

for the output file, you can specify any of the supported formats

Extract just one sheet from a book

Suppose you just want to extract one sheet from many sheets that exists in a work book and you would like to separate it into a single sheet excel file. You can easily do this:

for the output file, you can specify any of the supported formats

Hidden feature: partial read

Most pyexcel users do not know, but other library users were requesting partial read

When you are dealing with huge amount of data, e.g. 64GB, obviously you would not like to fill up your memory with those data. What you may want to do is, record data from Nth line, take M records and stop. And you only want to use your memory for the M records, not for beginning part nor for the tail part.

Hence partial read feature is developed to read partial data into memory for processing.

You can paginate by row, by column and by both, hence you dictate what portion of the data to read back. But remember only row limit features help you save memory. Let's you use this feature to record data from Nth column, take M number of columns and skip the rest. You are not going to reduce your memory footprint.

Why did not I see above benefit?

This feature depends heavily on the implementation details.

pyexcel-xls (xlrd), pyexcel-xlsx (openpyxl), pyexcel-ods (odfpy) and pyexcel-ods3 (pyexcel-ezodf) will read all data into memory. Because xls, xlsx and ods file are effective a zipped folder, all four will unzip the folder and read the content in xml format in full, so as to make sense of all details.

Hence, during the partial data is been returned, the memory consumption won't differ from reading the whole data back. Only after the partial data is returned, the memory comsumption curve shall jump the cliff. So pagination code here only limits the data returned to your program.

With that said, pyexcel-xlsxr, pyexcel-odsr and pyexcel-htmlr DOES read partial data into memory. Those three are implemented in such a way that they consume the xml(html) when needed. When they have read designated portion of the data, they stop, even if they are half way through.

In addition, pyexcel's csv readers can read partial data into memory too.

Let's assume the following file is a huge csv file:

And let's pretend to read partial data:

And you could as well do the same for columns:

Obvious, you could do both at the same time:

The pagination support is available across all pyexcel plugins.

Note

No column pagination support for query sets as data source.

Formatting while transcoding a big data file

If you are transcoding a big data set, conventional formatting method would not help unless a on-demand free RAM is available. However, there is a way to minimize the memory footprint of pyexcel while the formatting is performed.

Let's continue from previous example. Suppose we want to transcode "your_file.csv" to "your_file.xls" but increase each element by 1.

What we can do is to define a row renderer function as the following:

Then pass it onto save_as function using row_renderer:

Note

If the data content is from a generator, isave_as has to be used.

We can verify if it was done correctly:

Stream APIs for big file : A set of two liners

When you are dealing with BIG excel files, you will want pyexcel to use constant memory.

This section shows you how to get data from your BIG excel files and how to export data to excel files in two lines at most, without eating all your computer memory.

Two liners for get data from big excel files

Get a list of dictionaries

Suppose you want to process the following coffee data again:

Top 5 coffeine drinks:

Coffees Serving Size Caffeine (mg)
Starbucks Coffee Blonde Roast venti(20 oz) 475
Dunkin' Donuts Coffee with Turbo Shot large(20 oz.) 398
Starbucks Coffee Pike Place Roast grande(16 oz.) 310
Panera Coffee Light Roast regular(16 oz.) 300

Let's get a list of dictionary out from the xls file:

And let's check what do we have:

Please do not forgot the second line to close the opened file handle:

Get two dimensional array

Instead, what if you have to use pyexcel.get_array to do the same:

Again, do not forgot the second line:

where start_row skips the header row.

Data export in one liners

Export an array

Suppose you have the following array:

And here is the code to save it as an excel file :

But the following line is not required because the data source are not file sources:

Let's verify it:

And here is the code to save it as a csv file :

Let's verify it:

Export a list of dictionaries

Export a dictionary of single key value pair

Export a dictionary of single dimensonal array

Export a dictionary of two dimensional array as a book

Suppose you want to save the below dictionary to an excel file :

Here is the code:

If you want to preserve the order of sheets in your dictionary, you have to pass on an ordered dictionary to the function itself. For example:

Let's verify its order:

Please notice that "Sheet 2" is the first item in the book_dict, meaning the order of sheets are preserved.

File format transcoding on one line

Note

Please note that the following file transcoding could be with zero line. Please install pyexcel-cli and you will do the transcode in one command. No need to open your editor, save the problem, then python run.

The following code does a simple file format transcoding from xls to csv:

Again it is really simple. Let's verify what we have gotten:

Note

Please note that csv(comma separate value) file is pure text file. Formula, charts, images and formatting in xls file will disappear no matter which transcoding tool you use. Hence, pyexcel is a quick alternative for this transcoding job.

Let use previous example and save it as xlsx instead

Again let's verify what we have gotten:

Available Plugins

A list of file formats supported by external plugins
Package name Supported file formats Dependencies

pyexcel-io

csv, csvz1, tsv, tsvz2

pyexcel-xls

xls, xlsx(read only), xlsm(read only)

xlrd, xlwt

pyexcel-xlsx xlsx openpyxl

pyexcel-ods3

ods

pyexcel-ezodf, lxml

pyexcel-ods ods odfpy
Dedicated file reader and writers
Package name Supported file formats Dependencies
pyexcel-xlsxw xlsx(write only) XlsxWriter
pyexcel-libxlsxw xlsx(write only) libxlsxwriter
pyexcel-xlsxr xlsx(read only) lxml
pyexcel-xlsbr xlsb(read only) pyxlsb
pyexcel-odsr read only for ods, fods lxml
pyexcel-odsw write only for ods loxun
pyexcel-htmlr html(read only) lxml,html5lib
pyexcel-pdfr pdf(read only) camelot

Plugin shopping guide

Since 2020, all pyexcel-io plugins have dropped the support for python versions which are lower than 3.6. If you want to use any of those Python versions, please use pyexcel-io and its plugins versions that are lower than 0.6.0.

Except csv files, xls, xlsx and ods files are a zip of a folder containing a lot of xml files

The dedicated readers for excel files can stream read

In order to manage the list of plugins installed, you need to use pip to add or remove a plugin. When you use virtualenv, you can have different plugins per virtual environment. In the situation where you have multiple plugins that does the same thing in your environment, you need to tell pyexcel which plugin to use per function call. For example, pyexcel-ods and pyexcel-odsr, and you want to get_array to use pyexcel-odsr. You need to append get_array(..., library='pyexcel-odsr').

Other data renderers
Package name Supported file formats Dependencies Python versions

pyexcel-text

write only:rst, mediawiki, html, latex, grid, pipe, orgtbl, plain simple read only: ndjson r/w: json

tabulate

2.6, 2.7, 3.3, 3.4 3.5, 3.6, pypy

pyexcel-handsontable handsontable in html handsontable same as above

pyexcel-pygal

svg chart

pygal

2.7, 3.3, 3.4, 3.5 3.6, pypy

pyexcel-sortable sortable table in html csvtotable same as above

pyexcel-gantt

gantt chart in html

frappe-gantt

except pypy, same as above

Footnotes

Acknowledgement

All great work have been done by odf, ezodf, xlrd, xlwt, tabulate and other individual developers. This library unites only the data access code.

License

New BSD License


  1. zipped csv file

  2. zipped tsv file