Comparing Excel to learn the cell selection and font setting of the openpyxl series

select cell

# 单元格选择
from openpyxl import Workbook
wb=Workbook()#声明新建一个工作簿,默认创建一个sheet
ws=wb.active#激活sheet

#选择具体单元格
ws['A2']=1#具体单元格
ws.cell(row=1,column=2).value=2#通过row和colum形式指定

#选择多个单元格
ws[10]=3#第十行的单元格
ws[C]=4#第C列的单元格

ws[5:10]=5
ws['C:E']=6
wa['A1':'C5']=7

font settings

Basic font related settings

Set the excel font. The basic font-related settings mainly include font type, font size, whether to bold, whether to italic, alignment, underline, strikethrough, font color, etc. Need to use the Font () function.

excel implementation: insert image description here
python implementation:
insert image description here

  • name is used to describe the font type to be set, and the optional parameter values ​​are all the values ​​in the drop-down list in the Excel "Font" group.

  • size indicates the size of the font, and the optional value is a specific value.

  • Bold indicates whether to bold, when the parameter value is False, it means no bold, and when it is True, it means bold.

  • italic indicates whether italic, when the parameter value is False, it means that the font is not italic, and when it is True, it means that the font is italic.

  • vertAlign indicates the vertical alignment of the font, optional values ​​and corresponding alignmentsinsert image description here

  • underline indicates the type of underline, the optional value and the corresponding type of underline are as followsinsert image description here

  • strike indicates whether to add a strikethrough. When the parameter value is False, it means not to add a strikethrough, and when it is True, it means to add a strikethrough.

  • color indicates the specific font color, and the optional value is the color value in ARGB format. A color will have different formats, and there will be tools for converting between different formats on the Internet.

If you want to set a certain cell, you only need to make the font property of this cell equal to the Font() function, and specify the specific setting parameters in the Font() function. The format is as follows.

a1.font=Font()

The following is the processing of cell fonts

from openpyxl import Workbook
from openpyxl.styles import colors
from openpyxl.styles import Font
wb=Workbook()
ws=wb.active
rows=[["字体"],["字体大小"],["是否加粗"],["是否斜体"],["垂直对齐"],["下划线"],["删除线"],["字体颜色"]]
for row in rows:
    ws.append(row)
a1=ws['A1']
a1.font=Font(name='arial')
a2=ws['A2']
a2.font=Font(size=16)
a3=ws['A3']
a3.font=Font(bold=True)
a4=ws['A4']
a4.font=Font(italic=True)
a5=ws['A5']
a5.font=Font(vertAlign='superscript')
a6=ws['A6']
a6.font=Font(underline='doubleAccounting')
a7=ws['A7']
a7.font=Font(strike=True)

a8=ws['A8']
a8.font=Font(color='FFEE0000')

wb.save(r'C:\Users\mac\Desktop\test4.xlsx')#保存工作簿

insert image description here

cell padding

Cell filling is mainly divided into two types, one is background color filling, which is pure color filling, and the other is pattern filling, which is to fill cells with a certain pattern.
The operation of excel is as follows: insert image description here
In Python, when you want to fill the cells, you need to use the PatternFill() function. The specific parameters of this function are as follows
insert image description here

  • start_color represents the foreground color fill, which is the color of the specific pattern.
  • end_color indicates the background color, because the pattern is covered above the cell, so there will be another color at the bottom of the pattern, which is the background color. This color value also needs to be in ARGB format.
from openpyxl import Workbook
from openpyxl.styles import colors
from openpyxl.styles import PatternFill
wb=Workbook()#声明新建一个工作簿,默认创建一个sheet
ws=wb.active#激活sheet
ws['A1']='我是纯色填充'#给单元格a1赋值1
a1=ws['A1']
a1.fill=PatternFill(fill_type='solid',start_color='FFFFFF00')
wb.save(r'C:\Users\mac\Desktop\test5.xlsx')

insert image description here

border settings

The border line setting is to set the cell border, mainly including two aspects of line type and color.
In order to set the border line of the cell in Python, you need to use the Border() function. The specific parameters of this function are as follows.
insert image description here

  • left, right, top, and bottom represent setting the border lines on the left, right, top, and bottom sides of a cell respectively, and diagonal represents setting the diagonal line of the cell.
  • border_style represents the line type, optional parameter values ​​and corresponding line types
    insert image description here
  • color indicates the color of the line, and the optional parameter value also needs to be in ARGB format.
from openpyxl import Workbook
from openpyxl.styles import colors
from openpyxl.styles import Border,Side
wb=Workbook()
ws=wb.active
ws['B3']='边框'#给单元格a1赋值1
a1=ws['B3']
a1.border=Border(left=Side(border_style='hair',color='FFFFFF00'),
                right=Side(border_style='thick',color='FFFFFF00'),
                top=Side(border_style='dotted',color='FFFFFF00'),
                bottom=Side(border_style='medium',color='FFFFFF00'))
wb.save(r'C:\Users\mac\Desktop\test6.xlsx')

insert image description here

insert image description here
In addition to setting the diagonal parameters: diagonal
usage is as follows

from openpyxl import Workbook
from openpyxl.styles import colors
from openpyxl.styles import Border,Side
wb=Workbook()
ws=wb.active
ws['B3']='边框'#给单元格a1赋值1
a1=ws['B3']
a1.border=Border(left=Side(border_style='hair',color='FFFFFF00'),
                right=Side(border_style='thick',color='FFFFFF00'),
                top=Side(border_style='dotted',color='FFFFFF00'),
                bottom=Side(border_style='medium',color='FFFFFF00'),
                diagonal=Side(border_style='thick',color='FFFFFF00'),
                diagonalDown=True)

ws['E3']='斜杠'#给单元格a1赋值1
a2=ws['E3']
a2.border=Border(left=Side(border_style='hair',color='FFFFFF00'),
                right=Side(border_style='thick',color='FFFFFF00'),
                diagonal=Side(border_style='thick',color='FFFFFF00'),
                diagonalUp=True,
                 diagonalDown=True)


wb.save(r'C:\Users\mac\Desktop\test6.xlsx')

insert image description here

Batch setting cells

#遍历每一列
for col in wa['A':'C']:
    for i in col:
        r.font=Font()
        r.fill=PatternFill()
        r.border=Border()


#遍历每一行
for col in wa[1:7]:
    for i in col:
        r.font=Font()
        r.fill=PatternFill()
        r.border=Border()


#指定区域的每一行
for col in wa["A1":"D5"]:
    for i in col:
        r.font=Font()
        r.fill=PatternFill()
        r.border=Border()

Guess you like

Origin blog.csdn.net/weixin_41867184/article/details/125543891