创建者停止更新和支持的 dataframe-image 模块选项(将样式化的 DataFrame 转换为图像)

Option for dataframe-image module (convert a styled DataFrame to image) that the creator stopped updating and supporting

这里总是来自社区的最佳答案,图像格式完美,根据数据完全适合列,没有质量损失,表明使用 dataframe-image

使用简单,几乎不占用代码space,一行调用函数搞定。

官方模块页面:
https://github.com/dexplo/dataframe_image

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

使用这个模块时,发出了几个关于更改的未来警报消息,根据我的研究,代码已经变旧并且不再更新。如果模块更新并且不再接受使用某些功能,这可能会在将来导致问题。

例如:

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

我用它来保存图像中的程式化 DataFrame:

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")

我想知道是否有任何模块提供相同的服务并且没有被创建者遗漏。

我要测试的 CSV 数据示例:

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"

最终结果:

您可以使用 pandas .to_html(),然后使用 imgkit 写入文件。

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")