pytablewriter
Summary
A python library to write a table in various formats: CSV/HTML/JavaScript/JSON/LTSV/Markdown/MediaWiki/Excel/Pandas/Python/reStructuredText/TOML/TSV.
Features
- Write a table in various formats:
CSV
Microsoft Excel TM (.xlsx/.xls file format)
HTML
JSON
Markdown
MediaWiki
- Source code
Pandas (Definition of a DataFrame variable)
Python code (Definition of a nested list variable)
JavaScript (Definition of a nested list variable)
reStructuredText: Grid Tables/Simple Tables/CSV Table
Tab-separated values (TSV)
- Automatic tabular data formatting
Alignment
Padding
Decimal places of numbers
Multibyte character support
Write table to a stream such as a file/standard-output/string-buffer
Examples
Write a Markdown table
import pytablewriter
writer = pytablewriter.MarkdownTableWriter()
writer.table_name = "example_table"
writer.header_list = ["int", "float", "str", "bool", "mix", "time"]
writer.value_matrix = [
[0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"],
[2, "-2.23", "foo", False, None, "2017-12-23 45:01:23+0900"],
[3, 0, "bar", "true", "inf", "2017-03-03 33:44:55+0900"],
[-10, -9.9, "", "FALSE", "nan", "2017-01-01 00:00:00+0900"],
]
writer.write_table()
# example_table
int|float|str |bool | mix | time
--:|----:|----|-----|-------:|------------------------
0| 0.10|hoge|True | 0|2017-01-01 03:04:05+0900
2|-2.23|foo |False| |2017-12-23 12:34:51+0900
3| 0.00|bar |True |Infinity|2017-03-03 22:44:55+0900
-10|-9.90| |False| NaN|2017-01-01 00:00:00+0900
Rendering result
Rendered markdown at GitHub
Write a reStructuredText table (Grid Tables)
import pytablewriter
writer = pytablewriter.RstGridTableWriter()
writer.table_name = "example_table"
writer.header_list = ["int", "float", "str", "bool", "mix", "time"]
writer.value_matrix = [
[0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"],
[2, "-2.23", "foo", False, None, "2017-12-23 45:01:23+0900"],
[3, 0, "bar", "true", "inf", "2017-03-03 33:44:55+0900"],
[-10, -9.9, "", "FALSE", "nan", "2017-01-01 00:00:00+0900"],
]
writer.write_table()
.. table:: example_table
+---+-----+----+-----+--------+------------------------+
|int|float|str |bool | mix | time |
+===+=====+====+=====+========+========================+
| 0| 0.10|hoge|True | 0|2017-01-01 03:04:05+0900|
+---+-----+----+-----+--------+------------------------+
| 2|-2.23|foo |False| |2017-12-23 12:34:51+0900|
+---+-----+----+-----+--------+------------------------+
| 3| 0.00|bar |True |Infinity|2017-03-03 22:44:55+0900|
+---+-----+----+-----+--------+------------------------+
|-10|-9.90| |False| NaN|2017-01-01 00:00:00+0900|
+---+-----+----+-----+--------+------------------------+
Rendering result
int |
float |
str |
bool |
mix |
time |
|---|---|---|---|---|---|
0 |
0.10 |
hoge |
True |
0 |
2017-01-01 03:04:05+0900 |
2 |
-2.23 |
foo |
False |
2017-12-23 12:34:51+0900 |
|
3 |
0.00 |
bar |
True |
Infinity |
2017-03-03 22:44:55+0900 |
-10 |
-9.90 |
False |
NaN |
2017-01-01 00:00:00+0900 |
Write a table with JavaScript format (as a nested list variable definition)
import pytablewriter
writer = pytablewriter.JavaScriptTableWriter()
writer.table_name = "example_table"
writer.header_list = ["int", "float", "str", "bool", "mix", "time"]
writer.value_matrix = [
[0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"],
[2, "-2.23", "foo", False, None, "2017-12-23 45:01:23+0900"],
[3, 0, "bar", "true", "inf", "2017-03-03 33:44:55+0900"],
[-10, -9.9, "", "FALSE", "nan", "2017-01-01 00:00:00+0900"],
]
writer.write_table()
const example_table = [
["int", "float", "str", "bool", "mix", "time"],
[0, 0.10, "hoge", true, 0, "2017-01-01 03:04:05+0900"],
[2, -2.23, "foo", false, null, "2017-12-23 12:34:51+0900"],
[3, 0.00, "bar", true, Infinity, "2017-03-03 22:44:55+0900"],
[-10, -9.90, "", false, NaN, "2017-01-01 00:00:00+0900"]
];
Write a table to an Excel sheet
import pytablewriter
writer = pytablewriter.ExcelXlsxTableWriter()
writer.open_workbook("sample.xlsx")
writer.make_worksheet("example")
writer.header_list = ["int", "float", "str", "bool", "mix", "time"]
writer.value_matrix = [
[0, 0.1, "hoge", True, 0, "2017-01-01 03:04:05+0900"],
[2, "-2.23", "foo", False, None, "2017-12-23 12:34:51+0900"],
[3, 0, "bar", "true", "inf", "2017-03-03 22:44:55+0900"],
[-10, -9.9, "", "FALSE", "nan", "2017-01-01 00:00:00+0900"],
]
writer.write_table()
writer.close()
Output of Excel book
Output excel file (sample_single.xlsx)
Write a table using multibyte character
You can use multibyte character as table data.
import pytablewriter
writer = pytablewriter.RstSimpleTableWriter()
writer.table_name = "生成に関するパターン"
writer.header_list = ["パターン名", "概要", "GoF", "Code Complete[1]"]
writer.value_matrix = [
["Abstract Factory", "関連する一連のインスタンスを状況に応じて、適切に生成する方法を提供する。", "Yes", "Yes"],
["Builder", "複合化されたインスタンスの生成過程を隠蔽する。", "Yes", "No"],
["Factory Method", "実際に生成されるインスタンスに依存しない、インスタンスの生成方法を提供する。", "Yes", "Yes"],
["Prototype", "同様のインスタンスを生成するために、原型のインスタンスを複製する。", "Yes", "No"],
["Singleton", "あるクラスについて、インスタンスが単一であることを保証する。", "Yes", "Yes"],
]
writer.write_table()
Output of multi-byte character table
Write a table from pandas.DataFrame instance
import pandas as pd
import pytablewriter
from StringIO import StringIO
csv_data = StringIO(u""""i","f","c","if","ifc","bool","inf","nan","mix_num","time"
1,1.10,"aa",1.0,"1",True,Infinity,NaN,1,"2017-01-01 00:00:00+09:00"
2,2.20,"bbb",2.2,"2.2",False,Infinity,NaN,Infinity,"2017-01-02 03:04:05+09:00"
3,3.33,"cccc",-3.0,"ccc",True,Infinity,NaN,NaN,"2017-01-01 00:00:00+09:00"
""")
df = pd.read_csv(csv_data, sep=',')
writer = pytablewriter.MarkdownTableWriter()
writer.from_dataframe(df)
writer.write_table()
i | f | c | if |ifc|bool | inf |nan|mix_num | time
--:|---:|----|---:|---|-----|--------|---|-------:|-------------------------
1|1.10|aa | 1.0|1 |True |Infinity|NaN| 1|2017-01-01 00:00:00+09:00
2|2.20|bbb | 2.2|2.2|False|Infinity|NaN|Infinity|2017-01-02 03:04:05+09:00
3|3.33|cccc|-3.0|ccc|True |Infinity|NaN| NaN|2017-01-01 00:00:00+09:00
For more information
More examples are available at http://pytablewriter.rtfd.io/en/latest/pages/examples/index.html
Installation
pip install pytablewriter
Dependencies
Python 2.7+ or 3.3+
Test dependencies
Documentation
Metadata
Release files for pytablewriter 0.17.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pytablewriter-0.17.0.tar.gz | 74.6 kB | Without provenance | Details for pytablewriter-0.17.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytablewriter-0.17.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for pytablewriter-0.17.0-py2.py3-none-any.whl |
Total release size: 112.5 kB
Release files / pytablewriter-0.17.0.tar.gz
| Download URL | pytablewriter-0.17.0.tar.gz |
|---|---|
| Size | 74.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e244b69fb570b173bd81803eb81daf08e00e9fc0198cefcb7790dea040dfeca3
|
|
BLAKE2b-256 checksum How to use checksums |
29c5cfde25c15588d180fd17e3f7e1fa7c6e4e4074360387c8fd8cce7e8fce37
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / pytablewriter-0.17.0-py2.py3-none-any.whl
| Download URL | pytablewriter-0.17.0-py2.py3-none-any.whl |
|---|---|
| Size | 38.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
0087b0a81476d9a5864c2e981af0e5483447339170008cf20b7d1290ef68ee76
|
|
BLAKE2b-256 checksum How to use checksums |
c71a7523a83d52c2ee6e396c2d97fd982710c8efb2a4bb317785244bb1ab9a54
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for pytablewriter 0.17.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.