1

Always the best answers here from the community and that the image is perfectly formatted, that perfectly fits the columns according to the data, without loss of quality, the use of dataframe-image is indicated:
https://stackoverflow.com/a/65954710/11462274
https://stackoverflow.com/a/70550426/11462274

The use is simplified and takes up almost no space in the code, a single line to call the function does all the work.

Official module page:
https://github.com/dexplo/dataframe_image

https://pypi.org/project/dataframe-image/

When using this module, several future alert messages about changes are issued, the code has become old and no longer update according to my research. This may cause problems in the future if a module is updated and no longer accepts the use of some function.

Like for example:

FutureWarning: this method is deprecated in favour of `Styler.to_html()`
  html = '<div>' + obj.render() + '</div>'

I use it to save a stylized DataFrame in image:

import pandas as pd
import dataframe_image as dfi

def csv_to_image(csv_file,name_file):
    df = pd.read_csv(csv_file)

    df = df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center'),('background-color', '#40466e'),('color', 'white')])])
    df.set_properties(**{'text-align': 'center'}).hide(axis='index')
    pd.set_option('colheader_justify', 'center')

    dfi.export(df, name_file + ".png")

I would like to know if there is any module that does the same service and that has not been left out by the created.

My CSV data for example to tests:

DATA,HORA,CAMPEONATO,JOGO,CANAIS
21/04/2022,"08:00 Ao vivo 77'",Liga dos Campeões Asiática,0 Shandong Taishan x 0 Lion City Sailors FC,STAR+
21/04/2022,"08:00 Ao vivo 78'",Liga dos Campeões Asiática,2 BG Pathum United x 0 United City,STAR+
21/04/2022,11:00,Liga dos Campeões Asiática,Kawasaki Frontale x Johor Darul Takzim,STAR+
21/04/2022,11:00,Liga dos Campeões Asiática,Melbourne City x Jeonnam Dragons,STAR+
21/04/2022,11:00,Liga dos Campeões Asiática,Daegu FC x Urawa Red Diamonds,STAR+
21/04/2022,14:00,LaLiga,Espanhol x Rayo Vallecano,STAR+
21/04/2022,14:00,LaLiga,Levante x Sevilla,"ESPN 4, STAR+"
21/04/2022,15:00,LaLiga,Cadiz x Athletic Bilbao,"ESPN 3, STAR+"
21/04/2022,15:00,Campeonato Equatoriano,Mushuc Runa x Macara,STAR+
21/04/2022,15:45,Campeonato Inglês,Burnley x Southampton,"ESPN 2, STAR+"
21/04/2022,16:00,Copa da Liga Argentina,Sarmiento x Defensa y Justicia,STAR+
21/04/2022,16:15,Taça de Portugal,FC Porto x Sporting,STAR+
21/04/2022,16:30,LaLiga,Real Sociedad x Barcelona,"ESPN, STAR+"
21/04/2022,16:30,Brasileirão Série B,Grêmio x Guarani,PREMIERE FC
21/04/2022,16:30,Campeonato Pernambucano,Náutico x Retrô FC Brasil,"GLOBO(PE), PREMIERE 2"
21/04/2022,17:00,Campeonato Piauiense,Parnahyba x Fluminense PI,ELEVENSPORTS.COM
21/04/2022,17:30,Campeonato Equatoriano,SD Aucas x U Católica,STAR+
21/04/2022,19:00,Brasileirão Série B,Londrina x Grêmio Novorizontino,"PREMIERE FC, SPORTV"
21/04/2022,19:00,Campeonato Acreano,Rio Branco AC x Galvez AC,ELEVENSPORTS.COM
21/04/2022,19:00,Campeonato Capixaba,Serra x Real Noroeste Capixaba,"TVE(ES), YOUTUBE(TVE ES)"
21/04/2022,20:00,Campeonato Equatoriano,Cumbaya FC x Emelec,STAR+
21/04/2022,21:30,Copa do Brasil,Atlético GO x Cuiabá EC,"SPORTV, PREMIERE FC"

And the final result:

enter image description here

Digital Farmer
  • 1,705
  • 5
  • 17
  • 67
  • 1
    It's already been brought to the attention [here](https://githublab.com/repository/issues/dexplo/dataframe_image/37) so they may have an update soon. With that being said, that's the whole point of creating an environment. Then you can continue using an older version of pandas that utilizes this. – chitown88 Apr 22 '22 at 12:19
  • Hi @chitown88 , interesting. When I was doing my research, I hadn't seen his answer, as the project had several signs of abandonment, due to lack of check-in signature, due to the time without updates, it appeared that there was no more support. But the survey was more than 1 month ago and this answer is recent, it's good that he returned to look at the case. – Digital Farmer Apr 22 '22 at 12:30

1 Answers1

2

You could use pandas .to_html(), then use imgkit to write to file.

import pandas as pd
import imgkit

def csv_to_image(csv_file,name_file):
    df = pd.read_csv(csv_file)

    df = df.style.set_table_styles([dict(selector='th', props=[('text-align', 'center'),('background-color', '#40466e'),('color', 'white')])])
    df.set_properties(**{'text-align': 'center'}).hide(axis='index')
    pd.set_option('colheader_justify', 'center')

    html = df.to_html()
    imgkit.from_string(html, name_file + ".png")
chitown88
  • 27,527
  • 4
  • 30
  • 59
  • 1
    Hi mate when trying to use your indication, this error message appears indicating the use for a module that saves PDF's ```OSError: No wkhtmltoimage executable found: "command not found" If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf``` could you tell me the reason? – Digital Farmer Apr 22 '22 at 12:34
  • There is no line of code that calls ```wkhtmltoimage``` functions, would that also be a direct module problem? – Digital Farmer Apr 22 '22 at 12:36
  • Ah ya I might reference that I. The package. So ya you’d need tonistall that. – chitown88 Apr 22 '22 at 15:24
  • Instructions to install wkhtmltoimage https://computingforgeeks.com/install-wkhtmltopdf-on-ubuntu-debian-linux/ – Pranjal Sahu Oct 10 '22 at 16:52