Preface:
In this article, let's talk about how to generate table images with Python.
Choose a suitable library
Python's biggest advantage is the rich third-party libraries, basically what you want to function, you can find others to implement a good library, a few lines of code a call on the end.
Pytable
The first to find is the Japanese development of pytab library, it is based on matplotlib to draw the map, the default parameters generated by the appearance of the table in general, and can not display the Chinese characters, fonts are also very small, the effect is not ideal.
I began to hold a die-hard mentality, the library's source code to download the magic change, change the font and size, generate the table can finally look, but the layout will still be a variety of strange problems, such as text overflow cell and so on, the heart tired ......
Plotly:Official website address
Found a new one in the back.storehouse:plotly,
The official website is:
Designed specifically for machine learning and data science front-end display tools, simply used to draw the table is still too big to use ~ it is rendered with the web page, look at the example of the effect is okay, I will switch to this plotly to try, well, it really can be, then this is it.
This article will cover the use of each of these two libraries, including where I've magically altered pytab.
(but still recommend plotly, web rendering is just fine)
pytab
first install
pip install pytab
Then I wrapped the code for drawing the form into a function
import uuid from typing import List, Dict, Optional, Tuple import pytab def draw_table(data: Dict[str, list]): """ draw a table :param data: data format { 'a': [1.0, 2.1, 3.5, 4.0, 2.0, 1.0, 2.1, 3.5, 4.0, 2.0, ], 'b': [5.7, 6.1, 7.2, 8.3, 1.2, 5.7, 6.1, 7.2, 8.3, 1.2, ], } :return: """ # Set the fonts or it won't show Chinese ["-serif"] = ["SimHei"] ( data=data, data_loc='center', # th_type='dark', th_c='#aaaaee', # set the table header background color td_c='gray', # Set the data row background color table_type='striped', figsize=(len(()), int(len(()) / len(()) + 1)), # fontsize=18, ) # () temp_file = ((), f'{uuid.uuid4().hex}.jpg') print(temp_file) (temp_file) return temp_file
According to this data format in the notes, a and b are the table header column names, followed by an array of data for each column, which is very easy to understand
{ 'a': [1.0, 2.1, 3.5, 4.0, 2.0, 1.0, 2.1, 3.5, 4.0, 2.0, ], 'b': [5.7, 6.1, 7.2, 8.3, 1.2, 5.7, 6.1, 7.2, 8.3, 1.2, ], }
The table drawn looks like this
Let's try it again in Chinese.
{ 'Column 1': [1.0, 2.1, 3.5, 4.0, 2.0, 1.0, 2.1, 3.5, 4.0, 2.0, ], 'Column 2': [5.7, 6.1, 7.2, 8.3, 1.2, 5.7, 6.1, 7.2, 8.3, 1.2, ], }
It's drawn like this.
It's kind of ugly, I guess. It's barely watchable.
OK~ I won't toss more about pytab, after all, the upper limit is right here
Let's take a look at plotly.
plotly
first install
pip install plotly
Without further ado, the usual rules apply, and I'm writing it as a function again
import uuid from typing import List, Dict, Optional, Tuple import plotly.graph_objects as go import as pio def draw_table(headers: List[str], cells: List[list]): """ picture (e.g. of life in the city) :param headers: header=dict(values=['A Scores', 'B Scores']) :param cells: cells=dict(values=[[100, 90, 80, 90], [95, 85, 75, 95]]) :return: """ .default_width = len(','.join(headers)) * 20 .default_height = 250 + len(cells[0]) * 20 fig = (data=[(header=dict(values=headers), cells=dict(values=cells))]) # () image_file = ((), f'{uuid.uuid4().hex}.jpg') print('write image to', image_file) fig.write_image(image_file) return image_file
This time, the parameter format is different from the previous pytab, and you have to pass two parameters, both of which are of type array
The first is the table header and the second array is the cells
Let's try an example first
draw_table(['Column A', 'Column B'], [[100, 90, 80, 90], [95, 85, 75, 95]])
Then put the()
Comments removed, you can see the effect of the generated form image
After running it you can notice that a browser is automatically opened, because this library uses web pages to render tables.
The effect is as follows:
It works better than the previous pytab.
to this article on the Python data display to generate a table picture of the article is introduced to this, more related Python generate a table picture content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!