Apply borders to all cells in a range with openpyxl

后端 未结 8 796
面向向阳花
面向向阳花 2021-01-03 01:28

I have a script that takes a pandas dataframe and chops it up into several hundred chunks and saves each chunk as a separate excel file. Each chunk will have the same number

8条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-03 01:52

    if you need styling (borders...) for pandas excel dataframe my fork just got merged into master https://github.com/pydata/pandas/pull/2370#issuecomment-10898427

    as for you borders problems. setting all borders at once does not seam to work in openpyxl.

    In [34]: c.style.borders.all_borders.border_style = openpyxl.style.Border.BORDER_THIN
    
    In [36]: c.style
    'Calibri':11:False:False:False:False:'none':False:'FF000000':'none':0:'FFFFFFFF':'FF000000':'none':'FF000000':'none':'FF000000':'none':'FF000000':'none':'FF000000':'none':'FF000000':0:'thin':'FF000000':'none':'FF000000':'none':'FF000000':'none':'FF000000':'none':'FF000000':'general':'bottom':0:False:False:0:'General':0:'inherit':'inherit'
    

    setting individually works ('thin':'FF000000')

    In [37]: c.style.borders.top.border_style = openpyxl.style.Border.BORDER_THIN
    
    In [38]: c.style
    Out[38]: 'Calibri':11:False:False:False:False:'none':False:'FF000000':'none':0:'FFFFFFFF':'FF000000':'none':'FF000000':'none':'FF000000':'thin':'FF000000':'none':'FF000000':'none':'FF000000':0:'thin':'FF000000':'none':'FF000000':'none':'FF000000':'none':'FF000000':'none':'FF000000':'general':'bottom':0:False:False:0:'General':0:'inherit':'inherit'
    

    maybe a bug in openpyxl. but no big deal just wrap setting bottom , top, left, right in function

提交回复
热议问题