Skip to main content

pytablewriter is a python library to write a table in various formats: CSV/HTML/JavaScript/JSON/Markdown/Excel/Pandas/Python/reStructuredText

Project description

pytablewriter
=============

.. image:: https://img.shields.io/pypi/pyversions/pytablewriter.svg
:target: https://pypi.python.org/pypi/pytablewriter
.. image:: https://travis-ci.org/thombashi/pytablewriter.svg?branch=master
:target: https://travis-ci.org/thombashi/pytablewriter
.. image:: https://ci.appveyor.com/api/projects/status/2w0611ajvw21vho5?svg=true
:target: https://ci.appveyor.com/project/thombashi/pytablewriter
.. image:: https://coveralls.io/repos/github/thombashi/pytablewriter/badge.svg?branch=master
:target: https://coveralls.io/github/thombashi/pytablewriter?branch=master

Summary
-------

pytablewriter is a python library to write a table in various formats: CSV/HTML/JavaScript/JSON/Markdown/Excel/Pandas/Python/reStructuredText

Features
--------

- Write a table in various formats:
- CSV
- Microsoft Excel :superscript:`TM`
- HTML
- JavaScript (Definition of a nested list variable)
- JSON
- Markdown
- Pandas (Definition of a DataFrame variable)
- Python code (Definition of a nested list variable)
- reStructuredText
- Grid tables
- Simple tables
- CSV table
- Automatic value formatting
- Alignment
- Padding
- Decimal places of numbers
- Output to a stream such as a file or the standard output

Examples
========

Write a Markdown table
----------------------

.. code:: python

import pytablewriter

writer = pytablewriter.MarkdownTableWriter()
writer.table_name = "zone"
writer.header_list = ["zone_id", "country_code", "zone_name"]
writer.value_matrix = [
["1", "AD", "Europe/Andorra"],
["2", "AE", "Asia/Dubai"],
["3", "AF", "Asia/Kabul"],
["4", "AG", "America/Antigua"],
["5", "AI", "America/Anguilla"],
]

writer.write_table()

.. code::

# zone
zone_id|country_code| zone_name
------:|------------|----------------
1|AD |Europe/Andorra
2|AE |Asia/Dubai
3|AF |Asia/Kabul
4|AG |America/Antigua
5|AI |America/Anguilla


Rendering result
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. figure:: ss/markdown.png
:scale: 80%
:alt: markdown_ss

Rendered markdown at GitHub

Write a reStructuredText table (grid tables)
--------------------------------------------


.. code:: python

import pytablewriter

writer = pytablewriter.RstGridTableWriter()
writer.table_name = "zone"
writer.header_list = ["zone_id", "country_code", "zone_name"]
writer.value_matrix = [
["1", "AD", "Europe/Andorra"],
["2", "AE", "Asia/Dubai"],
["3", "AF", "Asia/Kabul"],
["4", "AG", "America/Antigua"],
["5", "AI", "America/Anguilla"],
]

writer.write_table()


.. code::

.. table:: zone

+-------+------------+----------------+
|zone_id|country_code| zone_name |
+=======+============+================+
| 1|AD |Europe/Andorra |
+-------+------------+----------------+
| 2|AE |Asia/Dubai |
+-------+------------+----------------+
| 3|AF |Asia/Kabul |
+-------+------------+----------------+
| 4|AG |America/Antigua |
+-------+------------+----------------+
| 5|AI |America/Anguilla|
+-------+------------+----------------+


Rendering result
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. table:: zone

+-------+------------+----------------+
|zone_id|country_code| zone_name |
+=======+============+================+
| 1|AD |Europe/Andorra |
+-------+------------+----------------+
| 2|AE |Asia/Dubai |
+-------+------------+----------------+
| 3|AF |Asia/Kabul |
+-------+------------+----------------+
| 4|AG |America/Antigua |
+-------+------------+----------------+
| 5|AI |America/Anguilla|
+-------+------------+----------------+

Write a JavaScript table (variable definition of nested list)
-------------------------------------------------------------

.. code:: python

import pytablewriter

writer = pytablewriter.JavaScriptTableWriter()
writer.table_name = "zone"
writer.header_list = ["zone_id", "country_code", "zone_name"]
writer.value_matrix = [
["1", "AD", "Europe/Andorra"],
["2", "AE", "Asia/Dubai"],
["3", "AF", "Asia/Kabul"],
["4", "AG", "America/Antigua"],
["5", "AI", "America/Anguilla"],
]

writer.write_table()

.. code:: js

var zone = [
["zone_id", "country_code", "zone_name"],
[1, "AD", "Europe/Andorra"],
[2, "AE", "Asia/Dubai"],
[3, "AF", "Asia/Kabul"],
[4, "AG", "America/Antigua"],
[5, "AI", "America/Anguilla"]
];

Write an Excel table
--------------------

.. code:: python

import pytablewriter

writer = pytablewriter.ExcelTableWriter()
writer.open_workbook("sample_single.xlsx")

writer.make_worksheet("zone")
writer.header_list = ["zone_id", "country_code", "zone_name"]
writer.value_matrix = [
["1", "AD", "Europe/Andorra"],
["2", "AE", "Asia/Dubai"],
["3", "AF", "Asia/Kabul"],
["4", "AG", "America/Antigua"],
["5", "AI", "America/Anguilla"],
]
writer.write_table()

writer.close()

Output
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. figure:: ss/excel_single.png
:scale: 100%
:alt: excel_single

Output excel file (``sample_single.xlsx``)

For more information
--------------------

More examples are available at
http://pytablewriter.readthedocs.org/en/latest/pages/examples/index.html

Installation
============

::

pip install pytablewriter


Dependencies
============

Python 2.7+ or 3.3+

- `DataPropery <https://github.com/thombashi/DataProperty>`__
- `dominate <http://github.com/Knio/dominate/>`__
- `pathvalidate <https://github.com/thombashi/pathvalidate>`__
- `six <https://pypi.python.org/pypi/six/>`__
- `XlsxWriter <http://xlsxwriter.readthedocs.io/>`__


Test dependencies
-----------------

- `pytest <http://pytest.org/latest/>`__
- `pytest-runner <https://pypi.python.org/pypi/pytest-runner>`__
- `SimpleSQLite <https://github.com/thombashi/SimpleSQLite>`__
- `tox <https://testrun.org/tox/latest/>`__

Documentation
=============

http://pytablewriter.readthedocs.org/en/latest/

Project details


Release history Release notifications | RSS feed

This version

0.4.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytablewriter-0.4.0.tar.gz (36.3 kB view details)

Uploaded Source

Built Distribution

pytablewriter-0.4.0-py2.py3-none-any.whl (21.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pytablewriter-0.4.0.tar.gz.

File metadata

File hashes

Hashes for pytablewriter-0.4.0.tar.gz
Algorithm Hash digest
SHA256 4f321814f0f6d762f00c679c8e9940a82ffb35e151bc2913a7fab486f3bcb1b0
MD5 b8cb67b78755f7da8807a365a0b0fc2c
BLAKE2b-256 13c564871b1da83ef74c460e81ccfc5c0abf6394a5874dfdfabe4a1178d7f123

See more details on using hashes here.

File details

Details for the file pytablewriter-0.4.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pytablewriter-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0a0e08f8463328c043b2c8496a067190d0d6e01d950ff6fec0323deca74dce1b
MD5 0d5cdedaa4f71c8a0677deb9239cd1b7
BLAKE2b-256 2da0e52943e50f010ad18631d4363e469615bad6d5c6310ed9e02f00b3185307

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page