简体   繁体   中英

reformat dataframe using openpyxl

I have a unformatted dataframe, that I would like to re-format using Openpyxl.

Data

id  consumed_q122   available_q122  consumed_q122   available_q222
aa  727.2           272.8           975.7           24.3
bb  0               1000            117.3           882.7

Desired

在此处输入图像描述

Doing

from openpyxl.styles import colors
from openpyxl.styles import Font, Color
from openpyxl import Workbook
wb = Workbook()
ws = wb.active

from openpyxl.styles import PatternFill
    
sheet['A1'].fill = PatternFill(bgColor="808080", fill_type = "solid") #grey
sheet['B1'].fill = PatternFill(bgColor="FFC0CB", fill_type = "solid") #pink
sheet['C1'].fill = PatternFill(bgColor="FFC0CB", fill_type = "solid") #pink
sheet['D1'].fill = PatternFill(bgColor="add8e6", fill_type = "solid") #blue
sheet['E1'].fill = PatternFill(bgColor="add8e6", fill_type = "solid") #blue



I am researching how to:
1. make the header fonts bold and black
2. make the headers 'wrap text'

Any suggestion is appreciated.

Sorry I fail to laod your desired image. Refer to what you are working on:

  1. headers fonts bold and black can be set by font

  2. headers 'wrap text' can be set by alignment

     from openpyxl.styles import Alignment, Font font1 = Font(color=colors.BLACK,b=True) algn1 = Alignment(wrapText=True) sh.cell(row=1,column=1).font = font1 sh.cell(row=1,column=2).alignment = algn1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM