使用pandas style 制作表格樣式,并保存為圖片

# -*- coding: utf-8 -*-
import logging
import os,sys
sys.path.append(os.path.abspath(os.path.join(os.getcwd(), "..")))
import pandas as pd
logging.basicConfig(filename='myprogramlog.txt', level=logging.DEBUG,
                    format='%(asctime)s-%(levelname)s - %(message)s')  #導(dǎo)入日志模塊
# logging.disable(logging.CRITICAL) #禁止日志輸出
from moudlescript.dataserver import DataServer

mServer = DataServer(server='127.0.0.1',user='sa',password='',database='')

sql = "select * from ywdj"
df = mServer.readSql(sql)
# delsql = "delete  from ywdj where CLFS=''"
# mServer.delSql(delsql)
df.CLFS = df.CLFS.str.replace(' ','')
df['CLFS']['**']='**'
df.CJZ = df.CJZ.str.replace(' ','')

tables = pd.crosstab(df.CJZ,df.CLFS,margins=False)
tables.index.name='處理人'
tables.reset_index(inplace=True)
tables.columns.name='處理人'
styles = [
    dict(selector="th", props=[("font-size", "150%"),
                               ("text-align", "center"),
                               ("border", "1px solid black"),
                               ('background-color','gray'),
                               ('width','100px')
                               ]),
    dict(selector="tr", props=[("font-size", "100%"),
                               ("text-align", "center"),
                               ("border", "1px solid black"),
                               ('width','100px')
                               ]),
    dict(selector="td", props=[("font-size", "100%"),
                               ("text-align", "center"),
                               ("border", "1px solid black"),
                               ('width','100px')
                               ]),
    dict(selector="caption", props=[("font-size", "80%"),
                                    ("color", "blue"),
                                    ("text-align", "left"),
                                    ("caption-side", "bottom")])
]

tables.columns=[['表格標(biāo)題','表格標(biāo)題','表格標(biāo)題','表格標(biāo)題','表格標(biāo)題'],['處理人', '辦理', '辦結(jié)', '轉(zhuǎn)派', '退單']]
tables = tables.style.hide_index().set_table_styles(styles)\
                     .bar(subset=[('表格標(biāo)題','辦結(jié)')],color='green',vmin=40,vmax=200)\
                     .set_caption("表格下標(biāo)").set_properties()
import dataframe_image as dfi

dfi.export(tables,'aa.png')

最后一個單元格格式

dict(selector="tbody tr:last-child td:last-child", props=[  
                               ('background','#ffffff'),
                               ])
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容