[英]pandas change column color of different sheets
我有一個腳本可以讀取多個 excel 文件並將它們作為表格放入最終的 excel 中。
我還有一個 function 為工作簿中的所有工作表填充藍色的列名背景,但我希望某些列的顏色為綠色,而其他特定工作表的顏色為藍色,可以這樣做嗎?
這是我的腳本:
def gitanalysis():
dest = createdir()
dfGitUsers = pd.read_excel(os.path.join(dest, "GitUsers.xlsx"))
dfGitUsers.fillna("N/A", inplace=True)
dfGitGroupMembership = pd.read_excel(os.path.join(dest, "GitGroupMembership.xlsx"))
dfGitRepoGroupAccess= pd.read_excel(os.path.join(dest,"GitRepoGroupAccess.xlsx"))
dfGitReposSize=pd.read_excel(os.path.join(dest,"GitReposSize.xlsx"))
dfGitRepoLastChangeDate=pd.read_excel(os.path.join(dest,"GitRepoLastChangeDate.xlsx"))
pathdest = path_dir()
# below its the path from where reads "CM_UserDetails.xlsx" file to add it in the excel sheet
dfUserDetails = pd.read_excel(rf"{pathdest}\CM_UsersDetails.xlsx")
dfUserDetails.fillna("N/A", inplace=True)
timestr = time.strftime("%Y-%m-%d-")
xlwriter = pd.ExcelWriter(os.path.join(dest, f'{timestr}Usage-GitAnalysis.xlsx'))
dfUserDetails.to_excel(xlwriter, sheet_name='UserDetails', index=False)
dfGitUsers.to_excel(xlwriter, sheet_name='GitUsers', index=False)
dfGitGroupMembership.to_excel(xlwriter, sheet_name='GitGroupMembership', index=False)
dfGitRepoGroupAccess.to_excel(xlwriter,sheet_name='GitRepoGroupAccess',index=False)
dfGitReposSize.to_excel(xlwriter,sheet_name='GitReposSize',index=False)
dfGitRepoLastChangeDate.to_excel(xlwriter,sheet_name='GitRepoLastChangeDate',index=False)
for column in dfGitUsers:
column_width = max(dfGitUsers[column].astype(str).map(len).max(), len(column))
col_idx = dfGitUsers.columns.get_loc(column)
xlwriter.sheets['GitUsers'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['UserDetails'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['GitGroupMembership'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['GitRepoGroupAccess'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['GitReposSize'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['GitRepoLastChangeDate'].set_column(col_idx,col_idx,column_width)
#THIS IS WHERE I CHANGE THE BG Color to Blue
workbook = xlwriter.book
cell_format = workbook.add_format({'bg_color': 'blue'})
cell_format.set_bold()
cell_format.set_font_color('black')
cell_format.set_border(1)
for sheet_name in xlwriter.sheets:
ws = xlwriter.sheets[sheet_name]
ws.freeze_panes(1, 0)
ws.conditional_format('A1:{}1'.format(chr(65 + ws.dim_colmax)), {'type': 'no_blanks', 'format': cell_format})
xlwriter.close()
print("GitSvnAnalysis.xlsx was exported with succes!")
這是它的外觀:
這是此表的預期 output:
IIUC ,對於您的所有工作表,您希望前兩列為綠色,其他列為 header 行的藍色。
您可以嘗試使用以下代碼為 header 行着色,其中代碼#92D050
對應於color green
, #00B0F0
對應於light blue
。
def gitanalysis():
dest = createdir()
dfGitUsers = pd.read_excel(os.path.join(dest, "GitUsers.xlsx"))
dfGitUsers.fillna("N/A", inplace=True)
dfGitGroupMembership = pd.read_excel(os.path.join(dest, "GitGroupMembership.xlsx"))
dfGitRepoGroupAccess= pd.read_excel(os.path.join(dest,"GitRepoGroupAccess.xlsx"))
dfGitReposSize=pd.read_excel(os.path.join(dest,"GitReposSize.xlsx"))
dfGitRepoLastChangeDate=pd.read_excel(os.path.join(dest,"GitRepoLastChangeDate.xlsx"))
pathdest = path_dir()
# below its the path from where reads "CM_UserDetails.xlsx" file to add it in the excel sheet
dfUserDetails = pd.read_excel(rf"{pathdest}\CM_UsersDetails.xlsx")
dfUserDetails.fillna("N/A", inplace=True)
timestr = time.strftime("%Y-%m-%d-")
xlwriter = pd.ExcelWriter(os.path.join(dest, f'{timestr}Usage-GitAnalysis.xlsx'))
dfUserDetails.to_excel(xlwriter, sheet_name='UserDetails', index=False)
dfGitUsers.to_excel(xlwriter, sheet_name='GitUsers', index=False)
dfGitGroupMembership.to_excel(xlwriter, sheet_name='GitGroupMembership', index=False)
dfGitRepoGroupAccess.to_excel(xlwriter,sheet_name='GitRepoGroupAccess',index=False)
dfGitReposSize.to_excel(xlwriter,sheet_name='GitReposSize',index=False)
dfGitRepoLastChangeDate.to_excel(xlwriter,sheet_name='GitRepoLastChangeDate',index=False)
for column in dfGitUsers:
column_width = max(dfGitUsers[column].astype(str).map(len).max(), len(column))
col_idx = dfGitUsers.columns.get_loc(column)
xlwriter.sheets['GitUsers'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['UserDetails'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['GitGroupMembership'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['GitRepoGroupAccess'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['GitReposSize'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['GitRepoLastChangeDate'].set_column(col_idx,col_idx,column_width)
workbook = xlwriter.book
# Green color for the first two cells
cell_format_green = workbook.add_format({'bg_color': '#92D050'})
cell_format_green.set_bold()
cell_format_green.set_font_color('black')
cell_format_green.set_border(1)
# Blue color for the next cells
cell_format_blue = workbook.add_format({'bg_color': '#00B0F0'})
cell_format_blue.set_bold()
cell_format_blue.set_font_color('black')
cell_format_blue.set_border(1)
for sheet_name in xlwriter.sheets:
ws = xlwriter.sheets[sheet_name]
ws.freeze_panes(1, 0)
ws.conditional_format('A1:B1', {'type': 'no_blanks', 'format': cell_format_green})
ws.conditional_format('C1:{}1'.format(chr(65 + ws.dim_colmax)), {'type': 'no_blanks', 'format': cell_format_blue})
xlwriter.close()
print("GitSvnAnalysis.xlsx was exported with success!")
這給了我們預期的 output
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.