Python uses OpenPyXL to process Excel tables

OpenPyXL library-cell style settings

The control of the cell depends on the openpyxl.style package, which defines the required objects and introduces related:

from openpyxl.styles import PatternFill, Font, Alignment, Border, SideBorder Side Border PatternFill Fill Font Font Aignment Alignment

The above can basically meet the needs

The basic usage is to assign the set properties of the cell object to a new corresponding object that is different from the default.

Import excel



from openpyxl import load_workbook


from openpyxl.styles import Border,Side


wb = load_workbook("template.xlsx")#Use openpyxl to read xlsx files and create workbook


ws = wb.active


ws


<Worksheet "sheet1">

1. Border Side border



from openpyxl.styles import Border, Side


border_type=Side(border_style=None, color='FF000000')


border = Border(left=border_type,


right=border_type,


top=border_type,


bottom=border_type,


diagonal=border_type,


diagonal_direction=0,


outline=border_type,


vertical=border_type,


horizontal=border_type


)


The border_style are:

‘dashDot',‘dashDotDot',‘dashed',‘dotted',‘double', ‘hair',‘medium',‘mediumDashDot',‘mediumDashDotDot', ‘mediumDashed',‘slantDashDot',‘thick',‘thin'

For example, the original excel





# 1-Narrow border, black


thin = Side(border_style="thin", color="000000")#边框,


border = Border(left=thin, right=thin, top=thin, bottom=thin)#The position of the border



ws['A3'].border = border #A3 cell set border



for row in ws['A5:D6']:


for cell in row:


cell.border = border#A5:D6 area cell set border


wb.save("test.xlsx")


effect:





# 2- wide border, blue


thin = Side(border_style="thick", color="0000FF")#边框


border = Border(left=thin, right=thin, top=thin, bottom=thin)#The position of the border



ws['A3'].border = border #A3 cell set border



for row in ws['A5:D6']:


for cell in row:


cell.border = border#A5:D6 area cell set border


wb.save("test.xlsx")


effect:



2. Font settings



from openpyxl.styles import Font


font = Font(name='Calibri',


size=11,


color='FF000000',


bold=False,


italic=False,


vertAlign=None,


underline='none',


strike=False)


Font name, font size, font color, bold, italic, vertical alignment (there are three: baseline, superscript, subscript), underline, strikethrough, font color can be RGB or aRGB,



font = Font(size=14, bold=True, name='Microsoft Yahei', color="FF0000")#font size, bold, font name, font name


ws['A3']="Welcome to pay attention: Eternal King's Treasure Box"


ws['A3'].font = font


wb.save("test.xlsx")




3. Fill



from openpyxl.styles import PatternFill


# fill_type is None or solid


fill = PatternFill(fill_type = None,start_color='FFFFFF',end_color='000000')


fill_type type

有:'none'、'solid'、'darkDown'、'darkGray'、'darkGrid'、'darkHorizontal'、'darkTrellis'、'darkUp'、'darkVertical'、'gray0625'、 'gray125'、'lightDown'、'lightGray'、'lightGrid'、'lightHorizontal'、 'lightTrellis'、'lightUp'、'lightVertical'、'mediumGray'

The official document states that if fill_type does not specify the type, the subsequent parameters are invalid

So the above code will have a problem, start_color represents the foreground color, end_color is the background color, the reason for setting two parameters is to facilitate the filling and the display of the gradient color (personally think)

If you want to fill with a solid color, you can use'solid', and then make the foreground color what you need, namely:



fill = PatternFill(fill_type = None,start_color='FF0000')


fill = PatternFill(patternType="solid", start_color="33CCFF")#Pure color fill


ws['A3']="Welcome to pay attention: Eternal King's Treasure Box"


ws['A3'].fill = fill


wb.save("test.xlsx")




4. Align




from openpyxl.styles import Alignment


align = Alignment(horizontal='left',vertical='center',wrap_text=True)


Horizontal represents the horizontal direction, can be left aligned, there are center center and right aligned right, distributed alignment, center continuous across columns, justify justify, fill, regular general

vertical stands for the vertical direction, it can be centered, top, bottom, justify, and distributed

Automatic wrapping: wrap_text, this is a boolean parameter, this parameter can also be written as wrapText



align = Alignment(horizontal='right',vertical='center',wrap_text=True)#Pure color filling


ws['A3']="Eternal King's Treasure Box"


ws['A3'].alignment = align


wb.save("test.xlsx")




The above is the whole content of this article, I hope it will be helpful to everyone's study, and I hope you can support it.


Guess you like

Origin blog.51cto.com/14825302/2540058