我有一個熊貓數據框,需要將它寫入Excel中,然後在同一張表上繪製顏色格式並繪製圖表。Excel樣式和來自熊貓數據框的圖表在單個Excel中
我已經使用StyleFrame做Coloring &邊框到我的Dataframe,但是這個StyleFrame不適用於Pandas XlsxWriter對象。反過來,從這種風格的數據框中繪製圖表也不起作用。
任何一個可以請您分享此解決方案!?
import pandas as pd
import xlfunc
import genfunc
from StyleFrame import StyleFrame, Styler, utils
def applystyle(df):
sf=StyleFrame(df)
sf.apply_column_style(cols_to_style=df.columns,
styler_obj=Styler(bg_color=utils.colors.white, bold=True, font=utils.fonts.calibri,
font_size=8), style_header=True)
sf.apply_headers_style(
styler_obj=Styler(bg_color=utils.colors.blue, bold=True, font_size=8, font_color=utils.colors.white,
number_format=utils.number_formats.general, protection=False))
sf.set_column_width(columns=sf.columns, width=15)
sf.set_row_height(rows=sf.row_indexes, height=12)
return sf
def createchart(regionname,workbook,outxl,sheetname,cellid,charttitle,startrow,startcol,endrow,endcol):
worksheet = outxl.sheets[sheetname]
kpichart = workbook.add_chart({'type': 'line'})
bsckpi = workbook.add_chart({'type': 'column'})
for col_num in range(startcol+1, endcol-1):
kpichart.set_title(
{'name': charttitle, 'name_font': {'name': 'Cambria', 'size': 18, 'color': '#000000'}})
kpichart.add_series({
'name': [sheetname, startrow, col_num],
'categories': [sheetname, startrow+1, 1, endrow, 1],
'values': [sheetname, startrow+1, col_num, endrow, col_num],
})
nodekpi.add_series({
'name': [sheetname, startrow, endcol-1],
'categories': [sheetname,startrow+1,1,endrow,1],
'values': [sheetname,startrow+1,endcol-1,endrow,endcol-1],
'y2_axis': True,
})
kpichart.combine(nodekpi)
kpichart.set_x_axis({'name': 'Date'})
kpichart.set_x_axis({'num_font': {'name': 'Cambria', 'size': 10, 'color': '#000000'}})
kpichart.set_y_axis({'name': charttitle, 'minor_gridlines': {'visible': True, 'color': '#FFFFFF'}})
kpichart.set_y_axis({'num_font': {'name': 'Cambria', 'size': 10, 'color': '#000000'}})
worksheet.insert_chart(cellid, kpichart, {'x_scale': 1.75, 'y_scale': 1.25})
def df_to_xl(regionname,hostlist,timerdf,hostData):
outxl = pd.ExcelWriter("timer_audit_data.xlsx", engine='xlsxwriter')
stylexl = StyleFrame.ExcelWriter("timer_audit_data_styled.xlsx")
outxl.sheets.clear()
workbook = outxl.book
stylebook = stylexl.book
style_timerdf = applystyle(timerdf)
style_timerdf.to_excel(stylexl, sheet_name='Parameters', index=False)
timerdfnew = timerdf
# Pivot
timer_summary = timerdfnew.pivot_table(values='NODE', index='HOST', columns='timer', aggfunc="count")
timer_summary = applystyle(timer_summary)
timer_summary.to_excel(stylexl, sheet_name='Parameters', startrow=0, startcol=15)
for hostname in hostlist:
hostname = genfunc.toString(hostname)
hostData.to_excel(outxl,sheet_name=hostname,startrow=1,startcol=1,index=False)
createchart(regionname,workbook,outxl,hostname,'G2',"Timer Performance"+hostname,1,1,tr+1,tc+1)
#In this Section I am pasting the dataframe to excel workbook & I am preparing a chart in the same sheet
#But here i would like to apply some colors and borders to hostData pasted in excel and then I want to prepare chart in the same sheet
outxl.save()
stylexl.save()
「StyleFrame不適用於熊貓XlsxWriter對象」它絕對應該,甚至有它自己的'ExcelWriter'方法來創建它。請提供您使用的代碼。 – DeepSpace
@DeepSpace我已經在Question中粘貼了我的代碼,在這裏我傳遞了一些來自主代碼的數據框,並希望該數據框可以在圖表和樣式數據表的Excel中給出 –