### Install PyTableWriter with apt Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/introduction/installation.md Install PyTableWriter on Debian-based systems using apt. This involves adding a PPA, updating the package list, and then installing the package. ```bash sudo add-apt-repository ppa:thombashi/ppa sudo apt update sudo apt install python3-pytablewriter ``` -------------------------------- ### MediaWiki Table Writer Example Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/markup/mediawiki.md Demonstrates how to use the MediaWikiTableWriter to format a table in MediaWiki markup. This example is linked from the main documentation page. ```python import pytablewriter with pytablewriter.MediaWikiTableWriter() as writer: writer.from_pytablewriter_data( [ ["Header 1", "Header 2", "Header 3"], [1, 2, 3], ["A", "B", "C"], ] ) writer.write_table() ``` -------------------------------- ### YAML Table Writer Example Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/yaml.md This example demonstrates how to use the YamlTableWriter to write data in YAML format. It shows basic instantiation and usage. ```python from pytablewriter import YamlTableWriter writer = YamlTableWriter() writer.write_table([ ["header1", "header2"], [1, 2], [3, 4] ]) ``` -------------------------------- ### TOML Table Writer Example Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/toml.md Demonstrates the usage of the TomlTableWriter to format data into TOML. This example shows how to create a writer instance and potentially write data, though the specific output is linked externally. ```python import pytablewriter with pytablewriter.TomlTableWriter() as writer: writer.from_pytablewriter_data( [ [1, 2, 3], [4, 5, 6], ] ) writer.write_table() ``` -------------------------------- ### Install PyTableWriter optional dependencies with pip Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/introduction/installation.md Install optional dependencies for specific formats like Elasticsearch, Excel, HTML, SQLite, TOML, themes, or all available dependencies. ```bash pip install pytablewriter[es] ``` ```bash pip install pytablewriter[excel] ``` ```bash pip install pytablewriter[html] ``` ```bash pip install pytablewriter[sqlite] ``` ```bash pip install pytablewriter[toml] ``` ```bash pip install pytablewriter[theme] ``` ```bash pip install pytablewriter[all] ``` -------------------------------- ### Space Aligned Table Writer Example Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/spacealigned.md Demonstrates the usage of the SpaceAlignedTableWriter to create a table with space-aligned formatting. This example is linked from the class and write_table() method documentation. ```python import pytablewriter writer = pytablewriter.SpaceAlignedTableWriter() writer.from_columns([ ["col1", "col2", "col3"], [1, 2, 3], [4, 5, 6], ]) writer.write_table() ``` -------------------------------- ### Write Table with Margin Source: https://github.com/thombashi/pytablewriter/blob/master/examples/ipynb/pytablewriter_examples.ipynb Example of writing a table with specified margins. Ensure the pytablewriter library is installed and imported. ```python import pytablewriter from textwrap import dedent writer = pytablewriter.MarkdownTableWriter() writer.table_name = "ps" writer.from_csv( dedent( """ USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.4 77664 8784 ? Ss May11 0:02 /sbin/init root 2 0.0 0.0 0 0 ? S May11 0:00 [kthreadd] root 4 0.0 0.0 0 0 ? I< May11 0:00 [kworker/0:0H] root 6 0.0 0.0 0 0 ? I< May11 0:00 [mm_percpu_wq] root 7 0.0 0.0 0 0 ? S May11 0:01 [ksoftirqd/0] """ ), delimiter=" ", ) writer.write_table() ``` -------------------------------- ### LatexMatrixWriter Example Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/latex.md Demonstrates how to use the LatexMatrixWriter to write a table in LaTeX array format. This example is linked from the class and method documentation. ```python import pytablewriter writer = pytablewriter.LatexMatrixWriter() writer.write_table([ [1, 2, 3], [4, 5, 6] ]) # Example of writing to a file: # writer.dump("matrix.tex") # Example of getting the string representation: # print(writer.dumps()) ``` -------------------------------- ### Install PyTableWriter with pip Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/introduction/installation.md Use this command to install the base PyTableWriter package. For formats requiring extra dependencies, append the format name in square brackets (e.g., `[excel]`). ```bash pip install pytablewriter ``` -------------------------------- ### Install PyTableWriter with conda Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/introduction/installation.md Install PyTableWriter using the conda package manager from the conda-forge channel. ```bash conda install -c conda-forge pytablewriter ``` -------------------------------- ### Apply predefined style themes Source: https://context7.com/thombashi/pytablewriter/llms.txt Use `set_theme()` to apply predefined style themes like `altrow` or `altcol`. Themes are available as installable plugins (`pip install pytablewriter[theme]`). `clear_theme()` removes all applied style filters. ```python import pytablewriter as ptw # Install first: pip install pytablewriter[theme] writer = ptw.TableWriterFactory.create_from_format_name( "markdown", table_name="inventory", headers=["Product", "Stock", "Price"], value_matrix=[ ["Widget A", 150, 9.99], ["Widget B", 80, 14.99], ["Widget C", 300, 4.99], ], margin=1, theme="altrow", # alternating row colors ) writer.write_table() # Remove all style filters writer.clear_theme() writer.write_table() ``` -------------------------------- ### Write Markdown Table from DataFrame Source: https://github.com/thombashi/pytablewriter/blob/master/README.rst Demonstrates how to write a Markdown table from a pandas DataFrame. Ensure pandas is installed. ```python import pandas as pd import pytablewriter as ptw def main(): csv_data = """i,f,c,if,ifc,bool,inf,nan,mix_num,time 1,1.1,aa,1.0,1,True,Infinity,NaN,1,2017-01-01 00:00:00+09:00 2,2.2,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,,2017-01-01 00:00:00+09:00 """ df = pd.read_csv(csv_data, sep=',') writer = ptw.MarkdownTableWriter(dataframe=df) writer.write_table() if __name__ == "__main__": main() ``` -------------------------------- ### set_theme Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/code.md Applies a theme to the writer, which sets predefined styles. Requires the corresponding theme plugin to be installed. ```APIDOC ## set_theme(theme: str, **kwargs: Any) ### Description Set style filters for a theme. ### Parameters #### Path Parameters - **theme** (str) - Required - Name of the theme. pytablewriter theme plugin must be installed corresponding to the theme name. ### Raises - **RuntimeError** - Raised when a theme plugin does not install. ``` -------------------------------- ### Write Table Data with PyTableWriter Source: https://github.com/thombashi/pytablewriter/blob/master/examples/ipynb/pytablewriter_examples.ipynb Basic example of writing table data using PyTableWriter. Set headers and value matrix, then call write_table(). ```python writer.table_name = "Timezone" writer.headers = headers writer.value_matrix = data writer.write_table() ``` -------------------------------- ### JavaScript Type Hint Example Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/examples/typehint/javascript.md This example shows the difference in generated JavaScript when type hints are applied versus when they are not. Without type hints, data types are inferred. With type hints like 'int', 'datetime', and 'str', values are converted to match the specified types, potentially altering their representation (e.g., numbers becoming integers, strings becoming Date objects, and vice-versa). ```javascript // without type hints: column data types are detected automatically by default const without_type_hint = [ ["header_a", "header_b", "header_c"], [-1.1, "2017-01-02 03:04:05", new Date("2017-01-02T03:04:05")], [0.12, "2017-02-03 04:05:06", new Date("2017-02-03T04:05:06")] ]; // with type hints: values will be converted with type hints if possible const with_type_hint = [ ["header_a", "header_b", "header_c"], [-1, new Date("2017-01-02T03:04:05"), "2017-01-02 03:04:05"], [0, new Date("2017-02-03T04:05:06"), "2017-02-03 04:05:06"] ]; ``` -------------------------------- ### Write table from pandas DataFrame Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/examples/datasource/from_pandas_dataframe.md Use the `from_dataframe` method to set up tabular data from a pandas DataFrame. Ensure pandas is installed and imported. ```python from textwrap import dedent import pandas as pd import io from pytablewriter import MarkdownTableWriter def main(): csv_data = io.StringIO(dedent(""" "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 = MarkdownTableWriter(dataframe=df) writer.write_table() if __name__ == "__main__": main() ``` -------------------------------- ### set_theme Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/yaml.md Applies a theme to set style filters for the writer. Requires the corresponding theme plugin to be installed. Raises RuntimeError if the theme plugin is not found. ```APIDOC ## set_theme(theme: str, **kwargs: Any) ### Description Set style filters for a theme. ### Parameters #### Path Parameters - **theme** (str) - Required - Name of the theme. pytablewriter theme plugin must be installed corresponding to the theme name. ### Raises **RuntimeError** – Raised when a theme plugin does not install. ``` -------------------------------- ### Render Markdown Table in Jupyter Notebook Source: https://github.com/thombashi/pytablewriter/blob/master/examples/ipynb/jupyter_notebook_example.ipynb Use pytablewriter to create a Markdown table. Ensure pytablewriter[html] or pytablewriter[all] is installed for notebook rendering. ```python import pytablewriter writer = pytablewriter.MarkdownTableWriter() writer.table_name = "Table render for Jupyter Notebook" writer.headers = ["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"], ] # All table writer class instances in pytablewriter can render in Jupyter Notebook. # To render writers at notebook cells, you will require the dependency packages to be installed either by # `pip install pytablewriter[html]` or `pip install pytablewriter[all]` writer ``` -------------------------------- ### LaTeX Table Output Example Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/examples/table_format/text/latex/latex_table.md The generated LaTeX code for a table written using LatexTableWriter. This output is in the LaTeX array environment format. ```tex \begin{array}{r | r | l | l | l | l} \hline \verb|int| & \verb|float| & \verb|str | & \verb|bool | & \verb| mix | & \verb| time | \\ \hline \hline 0 & 0.10 & hoge & True & 0 & \verb|2017-01-01 03:04:05+0900| \\ \hline 2 & -2.23 & foo & False & & \verb|2017-12-23 12:34:51+0900| \\ \hline 3 & 0.00 & bar & True & \infty & \verb|2017-03-03 22:44:55+0900| \\ \hline -10 & -9.90 & & False & NaN & \verb|2017-01-01 00:00:00+0900| \\ \hline \end{array} ``` -------------------------------- ### Write MediaWiki, AsciiDoc, TOML, and YAML Tables Source: https://context7.com/thombashi/pytablewriter/llms.txt Generate tables in MediaWiki markup, AsciiDoc format, TOML, and YAML. Use dumps() for string output. ```python import pytablewriter as ptw # MediaWiki mw = ptw.MediaWikiTableWriter( headers=["Name", "Age"], value_matrix=[["Alice", 30], ["Bob", 25]], ) mw.write_table() # YAML yaml_writer = ptw.YamlTableWriter( headers=["name", "score"], value_matrix=[["Alice", 95], ["Bob", 82]], ) print(yaml_writer.dumps()) # - name: Alice # score: 95 ``` -------------------------------- ### open Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/binary/pandas_pickle.md Opens a file for output stream. ```APIDOC ## open(file_path: str) ### Description Open a file for output stream. ### Parameters * **file_path** (*str*) – path to the file. ``` -------------------------------- ### open Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/binary/sqlite.md Open a SQLite database file. ```APIDOC ## open(file_path: str) ### Description Open a SQLite database file. ### Parameters * **file_path** (*str*) – SQLite database file path to open. ``` -------------------------------- ### Styling and Configuration Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/ltsv.md Methods for configuring the appearance and behavior of the LTSV writer. ```APIDOC ## headers ### Description Headers of a table to be outputted. ### Type Sequence[str] ``` ```APIDOC ## set_style(column: str | int, style: [Style](../../style.md#pytablewriter.style.Style)) ### Description Set [`Style`](../../style.md#pytablewriter.style.Style) for a specific column. ### Parameters * **column** (`int` or `str`) – Column specifier. Column index or header name correlated with the column. * **style** ([`Style`](../../style.md#pytablewriter.style.Style)) – Style value to be set to the column. ### Raises **ValueError** – Raised when the column specifier is invalid. ``` ```APIDOC ## set_theme(theme: str, **kwargs: Any) ### Description Set style filters for a theme. ### Parameters **theme** (*str*) – Name of the theme. pytablewriter theme plugin must be installed corresponding to the theme name. ### Raises **RuntimeError** – Raised when a theme plugin does not install. ``` ```APIDOC ## support_split_write ### Description Indicates whether the writer class supports iterative table writing (`write_table_iter`) method. ### Returns `True` if the writer supported iterative table writing. ### Return type bool ``` -------------------------------- ### Basic Markdown Table Creation Source: https://github.com/thombashi/pytablewriter/blob/master/examples/ipynb/pytablewriter_examples.ipynb Create a basic Markdown table with headers and a matrix of values. Imports are required. ```python import pytablewriter writer = pytablewriter.MarkdownTableWriter() writer.headers = ["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"], ] print(writer.dumps()) ``` -------------------------------- ### MediaWikiTableWriter Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/markup/mediawiki.md Initializes the MediaWikiTableWriter. This writer is used for creating tables in MediaWiki format. ```APIDOC ## class pytablewriter.MediaWikiTableWriter(**kwargs: Any) Bases: [`TextTableWriter`](../../base/text.md#pytablewriter.writer.text._text_writer.TextTableWriter) A table writer class for [MediaWiki](https://www.mediawiki.org/wiki/MediaWiki) format. ``` -------------------------------- ### format_name Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/markup/mediawiki.md Gets the name of the table format. ```APIDOC ## property format_name *: str Format name for the writer. * **Returns:** `str` ``` -------------------------------- ### value_matrix Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/base/abstract.md Get or set the data of a table to be outputted. ```APIDOC ## property value_matrix ### Description Data of a table to be outputted. ### Type Sequence ``` -------------------------------- ### format_name Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/binary/sqlite.md Property to get the format name of the writer. ```APIDOC #### *property* format_name *: str* Format name for the writer. * **Returns:** `str` ``` -------------------------------- ### GitHub Flavored Markdown (GFM) Table with Styles Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/examples/table_format/text/markdown/markdown.md Demonstrates how to create a GitHub Flavored Markdown table with custom text styles applied to columns using the 'flavor' and 'column_styles' arguments. Imports are required. ```python from pytablewriter import MarkdownTableWriter from pytablewriter.style import Style writer = MarkdownTableWriter( column_styles=[ Style(fg_color="red"), Style(fg_color="green", decoration_line="underline"), ], headers=["A", "B"], value_matrix=[ ["abc", 1], ["efg", 2], ], margin=1, flavor="github", enable_ansi_escape=False, ) writer.write_table() ``` -------------------------------- ### Initialize MarkdownTableWriter Source: https://github.com/thombashi/pytablewriter/blob/master/examples/ipynb/pytablewriter_examples.ipynb Initialize a MarkdownTableWriter to format data into a Markdown table. Further configuration and writing would follow. ```python from pytablewriter import MarkdownTableWriter writer = MarkdownTableWriter() writer.table_name = table_name ``` -------------------------------- ### column_styles Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/binary/sqlite.md Property to get or set the style for each column. ```APIDOC #### *property* column_styles *: list[[Style](../../style.md#pytablewriter.style.Style) | None]* [`Style`](../../style.md#pytablewriter.style.Style) for each column. * **Type:** List[Optional[[Style](../../style.md#pytablewriter.style.Style)]] ``` -------------------------------- ### table_name Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/application/es.md Property to get or set the name of the table. ```APIDOC ## table_name ### Description Name of a table. ### Type str ``` -------------------------------- ### Write MediaWiki Table Source: https://github.com/thombashi/pytablewriter/blob/master/examples/ipynb/pytablewriter_examples.ipynb Use MediaWikiTableWriter to format data as a MediaWiki table. Requires setting table name, headers, and value matrix. ```python writer = pytablewriter.MediaWikiTableWriter() writer.table_name = table_name writer.headers = headers writer.value_matrix = data writer.write_table() ``` -------------------------------- ### headers Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/application/es.md Property to get or set the headers of the table. ```APIDOC ## headers ### Description Headers of a table to be outputted. ### Type Sequence[str] ``` -------------------------------- ### Format Name Property Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/borderless.md Get the format name for the writer. ```python property format_name *: str* ``` -------------------------------- ### tabledata Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/application/es.md Property to get the tabular data stored in the writer. ```APIDOC ## tabledata ### Description Get tabular data of the writer. ### Type TableData ``` -------------------------------- ### LaTeX Table with Font Size Styles Source: https://github.com/thombashi/pytablewriter/blob/master/examples/ipynb/pytablewriter_examples.ipynb Demonstrates applying different font sizes (tiny, small, medium, large) to columns in a LaTeX table. Requires importing Style and FontSize. ```python from pytablewriter import LatexTableWriter from pytablewriter.style import Style, FontSize writer = LatexTableWriter() writer.table_name = "style test: font size" writer.headers = ["none", "empty_style", "tiny", "small", "medium", "large"] writer.value_matrix = [[111, 111, 111, 111, 111, 111], [1234, 1234, 1234, 1234, 1234, 1234]] writer.column_styles = [ None, Style(), Style(font_size=FontSize.TINY), Style(font_size=FontSize.SMALL), Style(font_size=FontSize.MEDIUM), Style(font_size=FontSize.LARGE), ] writer.write_table() ``` -------------------------------- ### table_format Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/application/es.md Property to get the table format supported by the writer. ```APIDOC ## table_format ### Description Get the format of the writer. ### Type [TableFormat](../../table_format.md#pytablewriter.TableFormat) ``` -------------------------------- ### dumps Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/json.md Get the JSON formatted string representation of the table data. ```APIDOC ## dumps(**kwargs: Any) -> str Get rendered tabular text from the table data. Only available for text format table writers. * **Parameters:** ****kwargs** – Optional arguments that the writer takes. * **Returns:** Rendered tabular text. * **Return type:** str ``` -------------------------------- ### Write Markdown Tables with Pytablewriter Source: https://context7.com/thombashi/pytablewriter/llms.txt Demonstrates how to use MarkdownTableWriter to create CommonMark, GFM, or kramdown tables. Configure table name, headers, data, column styles, and margins. Output can be written to stdout, captured as a string, or dumped to a file. ```python import pytablewriter as ptw from pytablewriter.style import Style writer = ptw.MarkdownTableWriter( table_name="sales_report", headers=["Product", "Units", "Revenue", "Growth"], value_matrix=[ ["Widget A", 1500, 45000.50, 0.12], ["Widget B", 800, 32000.00, -0.03], ["Widget C", 3200, 128000.75, 0.27], ], column_styles=[ Style(), Style(thousand_separator=","), Style(thousand_separator=",", font_weight="bold"), Style(align="center"), ], margin=1, ) # Write to stdout writer.write_table() # Output: # # sales_report # | Product | Units | Revenue | Growth | # | -------- | ----: | --------: | :------: | # | Widget A | 1,500 | **45,001** | 0.12 | # | Widget B | 800 | **32,000** | -0.03 | # | Widget C | 3,200 | **128,001** | 0.27 | # Capture as string result = writer.dumps() print(type(result)) # # GFM flavor with ANSI color disabled (for rendered output) writer.write_table(flavor="gfm") # Dump to file writer.dump("report.md") ``` -------------------------------- ### type_hints Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/base/abstract.md Get or set type hints for each column of the tabular data. ```APIDOC ## property type_hints ### Description Type hints for each column of the tabular data. Writers convert data for each column using the type hints information before writing tables when you call `write_xxx` methods. Acceptable values are as follows: > - `None` (automatically detect column type from values in the column) > - `pytablewriter.typehint.Bool` or `"bool"` > - `pytablewriter.typehint.DateTime` or `"datetime"` > - `pytablewriter.typehint.Dictionary` or `"dict"` > - `pytablewriter.typehint.Infinity` or `"inf"` > - `pytablewriter.typehint.Integer` or `"int"` > - `pytablewriter.typehint.IpAddress` or `"ipaddr"` > - `pytablewriter.typehint.List` or `"list"` > - `pytablewriter.typehint.Nan` or `"nan"` > - `pytablewriter.typehint.NoneType` or `"none"` > - `pytablewriter.typehint.NullString` or `"nullstr"` > - `pytablewriter.typehint.RealNumber` or `"realnumber"` or `"float"` > - `pytablewriter.typehint.String` or `"str"` If a type-hint value is not `None`, the writer tries to convert data for each data in a column to type-hint class. If the type-hint value is `None` or failed to convert data, the writer automatically detects column data type from the column data. If `type_hints` is `None`, the writer automatically detects data types for all of the columns and writes a table using detected column types. Defaults to `None`. ### Type list[type[AbstractType] | None] ``` -------------------------------- ### Column Styles Property Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/borderless.md Get or set the styles for each column in the table. ```python property column_styles *: list[[Style](../../style.md#pytablewriter.style.Style) | None]* ``` -------------------------------- ### Configure Markdown Table Column Styles Source: https://github.com/thombashi/pytablewriter/blob/master/README.rst Demonstrates how to apply specific styles to columns in a Markdown table using the `column_styles` attribute and the `Style` class. ```python import pytablewriter as ptw from pytablewriter.style import Style def main(): writer = ptw.MarkdownTableWriter( table_name="set style by column_styles", headers=[ "auto align", "left align", "center align", "bold", "italic", "bold italic ts", ], ``` -------------------------------- ### default_style Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/binary/sqlite.md Property to get or set the default style for table cells. ```APIDOC #### *property* default_style *: [Style](../../style.md#pytablewriter.style.Style)* Default [`Style`](../../style.md#pytablewriter.style.Style) of table cells. * **Type:** [Style](../../style.md#pytablewriter.style.Style) ``` -------------------------------- ### Configure Output Stream Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/examples/output/stream/index.md Write tables to stdout, capture output as a string using io.StringIO, or write to a file by changing the writer's stream attribute. ```python import io import pytablewriter as ptw def main(): writer = ptw.MarkdownTableWriter() writer.table_name = "zone" writer.headers = ["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 instance writes a table to stdout by default writer.write_table() # change the stream to a string buffer to get the output as a string # you can also get the tabular text by using dumps method writer.stream = io.StringIO() writer.write_table() print(writer.stream.getvalue()) # change the output stream to a file with open("sample.md", "w") as f: writer.stream = f writer.write_table() if __name__ == "__main__": main() ``` -------------------------------- ### column_styles Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/markup/md.md Get or set the style for each column. This is a list of optional Style objects. ```APIDOC ## property column_styles *: list[[Style](../../../style.md#pytablewriter.style.Style) | None]* ### Description [`Style`](../../../style.md#pytablewriter.style.Style) for each column. * **Type:** List[Optional[[Style](../../../style.md#pytablewriter.style.Style)]] ``` -------------------------------- ### open Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/binary/excel.md Open an Excel workbook file. ```APIDOC ## open(file_path: str) ### Description Open an Excel workbook file. ### Parameters #### Path Parameters - **file_path** (str) - Required - Excel workbook file path to open. ``` -------------------------------- ### Default Style Property Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/borderless.md Get or set the default style for table cells. ```python property default_style *: [Style](../../style.md#pytablewriter.style.Style)* ``` -------------------------------- ### Pandas DataFrame Initialization Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/examples/table_format/text/sourcecode/pandas_dataframe.md This code shows how a Pandas DataFrame can be initialized with the data provided to the writer. ```python example_table = pd.DataFrame([ [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, np.inf, "2017-03-03 22:44:55+0900"], [-10, -9.9, "", False, np.nan, "2017-01-01 00:00:00+0900"], ], columns=["int", "float", "str", "bool", "mix", "time"]) ``` -------------------------------- ### Styling and Configuration Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/markup/html.md Methods for configuring the appearance and behavior of the HTML table writer. ```APIDOC ## *property* headers *: Sequence[str]* Headers of a table to be outputted. * **Type:** Sequence[str] ## set_style(column: str | int, style: [Style](../../../style.md#pytablewriter.style.Style)) -> None Set [`Style`](../../../style.md#pytablewriter.style.Style) for a specific column. * **Parameters:** * **column** (`int` or `str`) – Column specifier. Column index or header name correlated with the column. * **style** ([`Style`](../../../style.md#pytablewriter.style.Style)) – Style value to be set to the column. * **Raises:** **ValueError** – Raised when the column specifier is invalid. ## set_theme(theme: str, **kwargs: Any) -> None Set style filters for a theme. * **Parameters:** **theme** (*str*) – Name of the theme. pytablewriter theme plugin must be installed corresponding to the theme name. * **Raises:** **RuntimeError** – Raised when a theme plugin does not install. ``` -------------------------------- ### Get Format Name Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/markup/asciidoc.md Retrieve the format name for the current writer, which is 'asciidoc' for this class. ```python format_name = writer.format_name ``` -------------------------------- ### set_theme Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/binary/sqlite.md Set style filters for a theme. Requires the corresponding pytablewriter theme plugin to be installed. ```APIDOC ## set_theme(theme: str, **kwargs: Any) ### Description Set style filters for a theme. ### Parameters * **theme** (*str*) – Name of the theme. pytablewriter theme plugin must be installed corresponding to the theme name. ### Raises **RuntimeError** – Raised when a theme plugin does not install. ``` -------------------------------- ### LTSV Table Writer Example Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/ltsv.md Demonstrates the basic usage of the LtsvTableWriter to create an LTSV formatted table. This writer is suitable for data that needs to be structured with key-value pairs. ```python import sys from pytablewriter import LtsvTableWriter writer = LtsvTableWriter( stream=sys.stdout, margin=1, header_list=["key1", "key2", "key3"], value_matrix=[ ["value1_1", "value1_2", "value1_3"], ["value2_1", "value2_2", "value2_3"], ["value3_1", "value3_2", "value3_3"], ], ) writer.write_table() ``` -------------------------------- ### MediaWikiTableWriter Methods Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/markup/index.md This section lists the available methods and properties for the MediaWikiTableWriter class, which allows for writing tables in MediaWiki markup format. ```APIDOC ## MediaWikiTableWriter ### Methods and Properties - `add_col_separator_style_filter()` - `add_style_filter()` - `clear_theme()` - `close()` ``` -------------------------------- ### set_theme Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/application/es.md Applies style filters for a given theme. Requires the corresponding theme plugin to be installed. ```APIDOC ## set_theme(theme: str, **kwargs: Any) ### Description Set style filters for a theme. ### Parameters #### Path Parameters * **theme** (*str*) – Name of the theme. pytablewriter theme plugin must be installed corresponding to the theme name. ### Raises **RuntimeError** – Raised when a theme plugin does not install. ``` -------------------------------- ### Write Markdown Table with GitHub Flavor Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/examples/table_format/text/markdown/md_example_with_flavor.txt Use the 'github' flavor with MarkdownTableWriter to apply GFM styles. This example shows how to set column-specific styles for text color and underlines. ```python from pytablewriter import MarkdownTableWriter from pytablewriter.style import Style writer = MarkdownTableWriter( column_styles=[ Style(fg_color="red"), Style(fg_color="green", decoration_line="underline"), ], headers=["A", "B"], value_matrix=[ ["abc", 1], ["efg", 2], ], margin=1, flavor="github", enable_ansi_escape=False, ) writer.write_table() ``` -------------------------------- ### MediaWiki Table Output Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/examples/table_format/text/mediawiki_example.txt The generated MediaWiki table syntax, ready to be used in wikis. ```mediawiki {| class="wikitable" |+example_table ! int ! float ! str ! bool ! mix ! time |- | style="text-align:right"| 0 | style="text-align:right"| 0.10 | hoge | True | style="text-align:right"| 0 | 2017-01-01 03:04:05+0900 |- | style="text-align:right"| 2 | style="text-align:right"| -2.23 | foo | False | | 2017-12-23 12:34:51+0900 |- | style="text-align:right"| 3 | style="text-align:right"| 0.00 | bar | True | Infinity | 2017-03-03 22:44:55+0900 |- | style="text-align:right"| -10 | style="text-align:right"| -9.90 | | False | NaN | 2017-01-01 00:00:00+0900 |} ``` -------------------------------- ### NumPy Array Output Example Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/examples/table_format/text/sourcecode/numpy.md The output format generated by NumpyTableWriter, representing a NumPy array definition. ```python example_table = np.array([ ["int", "float", "str", "bool", "mix", "time"], [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, np.inf, "2017-03-03 22:44:55+0900"], [-10, -9.9, "", False, np.nan, "2017-01-01 00:00:00+0900"], ]) ``` -------------------------------- ### dumps Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/code.md Get rendered tabular text from the table data. Only available for text format table writers. ```APIDOC ## dumps(**kwargs: Any) -> str Get rendered tabular text from the table data. Only available for text format table writers. * **Parameters:** **kwargs** – Optional arguments that the writer takes. * **Returns:** Rendered tabular text. * **Return type:** str ``` -------------------------------- ### Column Styles Property Source: https://github.com/thombashi/pytablewriter/blob/master/docs/pages/reference/writers/text/json.md Get or set the style for each column. The type is a list of optional Style objects. ```python column_styles: list[Style | None] ``` -------------------------------- ### Instantiate Writer by Format Name with TableWriterFactory Source: https://context7.com/thombashi/pytablewriter/llms.txt Use `TableWriterFactory.create_from_format_name()` to instantiate a writer by its format name. Handles potential `WriterNotFoundError` if the format is not supported. ```python import pytablewriter as ptw from pytablewriter.error import WriterNotFoundError try: writer = ptw.TableWriterFactory.create_from_format_name( "markdown", table_name="demo", headers=["col_a", "col_b"], value_matrix=[[1, "foo"], [2, "bar"]], margin=1, ) writer.write_table() except WriterNotFoundError as e: print(f"Unknown format: {e}") # List all supported format names for name in ptw.TableWriterFactory.get_format_names(): print(name) ```