使用 Python 在 Excel 中更改具有多个索引和标题的数据框的标题颜色

问题描述 投票:0回答:0

我创建了一个有多个索引的数据框,(3个标题,两个索引)我使用

to.excel()
方法将它导出到excel,第一个截图: before

我想改变颜色看起来像第二个截图: after

我怎样才能实现我想要的东西?感谢您提供的任何帮助! 这是我的代码:

import xlsxwriter
from xlsxwriter import Workbook
import pandas as pd
import numpy as np
from numpy import *
import ctypes
from openpyxl.workbook import Workbook
from docxtpl import DocxTemplate

laenge=['50','75','100','125','150','175','200','225','250','275','300']
TM=["S", "2TM", "3TM", "4TM", "5TM", "6TM", "8TM", "10TM", "12TM"]

spannung=['400V','200V']
MotorenList=["200HX","200UHX" ,"240HX", "310HX", "360UHX","564HX"]
DataToCalculat=['M_N','M_0','n_MAX','n_N','I_N','I_p','I_0']

columns=[np.array(spannung),np.array(MotorenList),np.array(DataToCalculat)]
columnsList=pd.MultiIndex.from_product(columns,names=['Voltage:','Motor:',''])

rows=[np.array(laenge),np.array(TM)]
indexList=pd.MultiIndex.from_product(rows,names=['h','TM'])

df=pd.DataFrame(np.random.randn(indexList.shape[0],columnsList.shape[0]),index=indexList, columns=columnsList)

writer= pd.ExcelWriter('test2.xlsx', engine='xlsxwriter')

for motor in MotorenList:
    
    if motor=='200HX' or motor=='200UHX':
        sheetName='RM 200'
        df_400V=pd.DataFrame(df[['400V']].xs(motor,level='Motor:',axis=1,drop_level=False))
        df_200V=pd.DataFrame(df[['200V']].xs(motor,level='Motor:',axis=1,drop_level=False))
        if motor=='200UHX':
            df_400V.to_excel(writer,sheet_name=sheetName,startrow=0,startcol=df_400V.shape[1]+3)
            df_200V.to_excel(writer,sheet_name=sheetName,startrow=df_400V.shape[0]+5,startcol=df_400V.shape[1]+3)
        else:
            df_400V.to_excel(writer,sheet_name=sheetName,startrow=0,startcol=0)
            df_200V.to_excel(writer,sheet_name=sheetName,startrow=df_400V.shape[0]+5,startcol=0)

    elif motor=='240HX' or motor=='240UHX':
        sheetName='RM 240'
        df_400V=pd.DataFrame(df[['400V']].xs(motor,level='Motor:',axis=1,drop_level=False))
        df_200V=pd.DataFrame(df[['200V']].xs(motor,level='Motor:',axis=1,drop_level=False))
        if motor=='240UHX':
            df_400V.to_excel(writer,sheet_name=sheetName,startrow=0,startcol=df_400V.shape[1]+3)
            df_200V.to_excel(writer,sheet_name=sheetName,startrow=df_400V.shape[0]+5,startcol=df_400V.shape[1]+3)
        else:
            df_400V.to_excel(writer,sheet_name=sheetName,startrow=0,startcol=0)
            df_200V.to_excel(writer,sheet_name=sheetName,startrow=df_400V.shape[0]+5,startcol=0)
    
    elif motor=='310HX' or motor=='310UHX':
        sheetName='RM 310'
        df_400V=pd.DataFrame(df[['400V']].xs(motor,level='Motor:',axis=1,drop_level=False))
        df_200V=pd.DataFrame(df[['200V']].xs(motor,level='Motor:',axis=1,drop_level=False))
        if motor=='310UHX':
            df_400V.to_excel(writer,sheet_name=sheetName,startrow=0,startcol=df_400V.shape[1]+3)
            df_200V.to_excel(writer,sheet_name=sheetName,startrow=df_400V.shape[0]+5,startcol=df_400V.shape[1]+3)
        else:
            df_400V.to_excel(writer,sheet_name=sheetName,startrow=0,startcol=0)
            df_200V.to_excel(writer,sheet_name=sheetName,startrow=df_400V.shape[0]+5,startcol=0)

    elif motor=='360HX' or motor=='360UHX':
        sheetName='RM 360'
        df_400V=pd.DataFrame(df[['400V']].xs(motor,level='Motor:',axis=1,drop_level=False))
        df_200V=pd.DataFrame(df[['200V']].xs(motor,level='Motor:',axis=1,drop_level=False))
        if motor=='360UHX':
            df_400V.to_excel(writer,sheet_name=sheetName,startrow=0,startcol=df_400V.shape[1]+3)
            df_200V.to_excel(writer,sheet_name=sheetName,startrow=df_400V.shape[0]+5,startcol=df_400V.shape[1]+3)
        else:
            df_400V.to_excel(writer,sheet_name=sheetName,startrow=0,startcol=0)
            df_200V.to_excel(writer,sheet_name=sheetName,startrow=df_400V.shape[0]+5,startcol=0)

    elif motor=='564HX' or motor=='564UHX':
        sheetName='RM 564'
        df_200V=pd.DataFrame(df[['200V']].xs(motor,level='Motor:',axis=1,drop_level=False))
        df_200V=pd.DataFrame(df[['200V']].xs(motor,level='Motor:',axis=1,drop_level=False))
        if motor=='564UHX':
            df_400V.to_excel(writer,sheet_name=sheetName,startrow=0,startcol=df_400V.shape[1]+3)
            df_200V.to_excel(writer,sheet_name=sheetName,startrow=df_400V.shape[0]+5,startcol=df_400V.shape[1]+3)
        else:
            df_400V.to_excel(writer,sheet_name=sheetName,startrow=0,startcol=0)
            df_200V.to_excel(writer,sheet_name=sheetName,startrow=df_400V.shape[0]+5,startcol=0)

writer.close()
python excel pandas dataframe openpyxl
© www.soinside.com 2019 - 2024. All rights reserved.