Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An intuitive and type-safe Python library for converting lists to fancy ASCII tables for displaying in the terminal or code-blocks

License

NotificationsYou must be signed in to change notification settings

DenverCoder1/table2ascii

Repository files navigation

buildversiondownloadslicensediscord

An intuitive and type-safe library for converting 2D Python lists to fancy ASCII/Unicode tables

Documentation and examples are available attable2ascii.rtfd.io

📥 Installation

pip install -U table2ascii

Requirements:Python 3.7+

🧑‍💻 Usage

🚀 Convert lists to ASCII tables

fromtable2asciiimporttable2asciioutput=table2ascii(header=["#","G","H","R","S"],body=[["1","30","40","35","30"], ["2","30","40","35","30"]],footer=["SUM","130","140","135","130"],)print(output)"""╔═════════════════════════════╗║  #     G     H     R     S  ║╟─────────────────────────────╢║  1    30    40    35    30  ║║  2    30    40    35    30  ║╟─────────────────────────────╢║ SUM   130   140   135   130 ║╚═════════════════════════════╝"""

🏆 Set first or last column headings

fromtable2asciiimporttable2asciioutput=table2ascii(body=[["Assignment","30","40","35","30"], ["Bonus","10","20","5","10"]],first_col_heading=True,)print(output)"""╔════════════╦═══════════════════╗║ Assignment ║ 30   40   35   30 ║║    Bonus   ║ 10   20    5   10 ║╚════════════╩═══════════════════╝"""

📰 Set column widths and alignments

fromtable2asciiimporttable2ascii,Alignmentoutput=table2ascii(header=["Product","Category","Price","Rating"],body=[        ["Milk","Dairy","$2.99","6.283"],        ["Cheese","Dairy","$10.99","8.2"],        ["Apples","Produce","$0.99","10.00"],    ],column_widths=[12,12,12,12],alignments=[Alignment.LEFT,Alignment.CENTER,Alignment.RIGHT,Alignment.DECIMAL],)print(output)"""╔═══════════════════════════════════════════════════╗║ Product       Category         Price     Rating   ║╟───────────────────────────────────────────────────╢║ Milk           Dairy           $2.99      6.283   ║║ Cheese         Dairy          $10.99      8.2     ║║ Apples        Produce          $0.99     10.00    ║╚═══════════════════════════════════════════════════╝"""

🎨 Use a preset style

See a list of 30+ preset styleshere.

fromtable2asciiimporttable2ascii,Alignment,PresetStyleoutput=table2ascii(header=["First","Second","Third","Fourth"],body=[["10","30","40","35"], ["20","10","20","5"]],column_widths=[10,10,10,10],style=PresetStyle.ascii_box)print(output)"""+----------+----------+----------+----------+|  First   |  Second  |  Third   |  Fourth  |+----------+----------+----------+----------+|    10    |    30    |    40    |    35    |+----------+----------+----------+----------+|    20    |    10    |    20    |    5     |+----------+----------+----------+----------+"""output=table2ascii(header=["First","Second","Third","Fourth"],body=[["10","30","40","35"], ["20","10","20","5"]],style=PresetStyle.plain,cell_padding=0,alignments=Alignment.LEFT,)print(output)"""First Second Third Fourth10    30     40    3520    10     20    5"""

🎲 Define a custom style

CheckTableStyle for more info andPresetStyle for examples.

fromtable2asciiimporttable2ascii,TableStylemy_style=TableStyle.from_string("*-..*||:+-+:+     *''*")output=table2ascii(header=["First","Second","Third"],body=[["10","30","40"], ["20","10","20"], ["30","20","30"]],style=my_style)print(output)"""*-------.--------.-------*| First : Second : Third |+-------:--------:-------+|  10   :   30   :  40   ||  20   :   10   :  20   ||  30   :   20   :  30   |*-------'--------'-------*"""

🪄 Merge adjacent cells

fromtable2asciiimporttable2ascii,Merge,PresetStyleoutput=table2ascii(header=["#","G","Merge",Merge.LEFT,"S"],body=[        [1,5,6,200,Merge.LEFT],        [2,"E","Long cell",Merge.LEFT,Merge.LEFT],        ["Bonus",Merge.LEFT,Merge.LEFT,"F","G"],    ],footer=["SUM","100","200",Merge.LEFT,"300"],style=PresetStyle.double_thin_box,first_col_heading=True,)print(output)"""╔═════╦═════╤═══════╤═════╗║  #  ║  G  │ Merge │  S  ║╠═════╬═════╪═══╤═══╧═════╣║  1  ║  5  │ 6 │   200   ║╟─────╫─────┼───┴─────────╢║  2  ║  E  │  Long cell  ║╟─────╨─────┴───┬───┬─────╢║     Bonus     │ F │  G  ║╠═════╦═════╤═══╧═══╪═════╣║ SUM ║ 100 │  200  │ 300 ║╚═════╩═════╧═══════╧═════╝"""

⚙️ Options

All parameters are optional. At least one ofheader,body, andfooter must be provided.

Refer to thedocumentation for more information.

OptionSupported TypesDescription
headerSequence[SupportsStr],None
(Default:None)
First table row seperated by header row separator. Values should supportstr()
bodySequence[Sequence[SupportsStr]],None
(Default:None)
2D List of rows for the main section of the table. Values should supportstr()
footerSequence[SupportsStr],None
(Default:None)
Last table row seperated by header row separator. Values should supportstr()
column_widthsSequence[Optional[int]],None
(Default:None / automatic)
List of column widths in characters for each column
alignmentsSequence[Alignment],Alignment,None
(Default:None / all centered)
Column alignments
(ex.[Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT, Alignment.DECIMAL])
number_alignmentsSequence[Alignment],Alignment,None
(Default:None)
Column alignments for numeric values.alignments will be used if not specified.
styleTableStyle
(Default:double_thin_compact)
Table style to use for the table*
first_col_headingbool
(Default:False)
Whether to add a heading column separator after the first column
last_col_headingbool
(Default:False)
Whether to add a heading column separator before the last column
cell_paddingint
(Default:1)
The minimum number of spaces to add between the cell content and the cell border
use_wcwidthbool
(Default:True)
Whether to usewcwidth instead oflen() to calculate cell width

*See a list of all preset styleshere.

See theAPI Reference for more info.

👨‍🎨 Use cases

🗨️ Discord messages and embeds

  • Display tables nicely inside markdown code blocks on Discord
  • Useful for making Discord bots withDiscord.py

image

💻 Terminal outputs

  • Tables display nicely whenever monospace fonts are fully supported
  • Tables make terminal outputs look more professional

image

🤗 Contributing

Contributions are welcome!

SeeCONTRIBUTING.md for more details on how to get involved.

About

An intuitive and type-safe Python library for converting lists to fancy ASCII tables for displaying in the terminal or code-blocks

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors8

Languages


[8]ページ先頭

©2009-2025 Movatter.jp