Python - openpyxl Excel operation example and practice

I. Introduction

After using the script to process the data, it needs to be converted to excel. Since the problems of manual pasting and line breaks are very unintelligent, the openpyxl library is used instead to process the data obtained by the script and complete the annotation. By the way, we will introduce the common syntax of openpyxl. Let’s take a look at our Raw data:

A	B	C	D
0.012 0.951 0.234 -0.137
0.038 0.999 -0.225 0.139

Among them, A, B, C, D are the headers, and the following two columns are the decimal point ratio. The effect we want to achieve :

(1) Convert decimals to percentages and keep 1 decimal place

(2) Percentages greater than 0 are marked in red, and numbers less than 0 are marked in green

(3) Numbers are aligned in the table

Let's get familiar with the common operations of openpyxl and realize the above requirements~

Two. openpyxl general operation

1. Create WorkBook

The Workbook class is the basic class of openpyxl. Initializing this class can be understood as opening a new excel workspace, in which create_sheet can be regarded as a new sheet. The following example creates a sheet named Sheet1. A Workbook can initialize multiple sheets:

# 初始化
workbook = Workbook()

# 创建 sheet
sheet = workbook.create_sheet("Sheet")

2.sheet add data

A. Single point addition

Sheet corresponds to the Sheet class, in which each cell in the table corresponds to the Cell class. You can set the value of the cell through Sheet['position'] = xxx, and then obtain the value of the corresponding cell through Cell.value. The position needs to be passed through excel Similar indexes are set, such as 'A1', 'B10', 'AA5', and to get cells, you need to pass the (row, col) index, there are some differences here.

 For example, if I want to set the D7 position to 999, I can do sheet['D7'] = 999.

sheet['D7'] = 999

B. Bulk add

sheet = workbook.create_sheet("Sheet", 0)

sheet.append([1, 2, 3, 4])
sheet.append([2, 3, 4, 5])
sheet.append([3, 4, 5, 6])

This method adds the data of the array row by row:

 Arrays of unequal length can also be used:

sheet.append([1, 2, 3, 4])
sheet.append([2, 3, 4, 5, 6])
sheet.append([3, 4, 5, 6])

3. Get sheet data

As mentioned above, the data of each cell is Cell, which needs to be obtained through the sheet.cell(row, col) method. It should be noted here that the row and column indices of excel all start from 1, and traversal from 0 will result in an error. The following gets the value of D7:

cell = sheet.cell(7, 4)
print(cell.value)
999

4. Row and column conversion

The coordinate system used for the added value above is the combination of letters + numbers such as A-col 5-row, and the coordinate form of 10-row 2-col is used to obtain the Cell cell, so the association between the two involves the conversion of col question. Here openpyxl library provides util function, just call to get index-string conversion:

from openpyxl.utils import get_column_letter, column_index_from_string

num = 10
# index 转换为 字符
index2string = get_column_letter(num)
# 字符转换为 index
string2index = column_index_from_string(index2string)

print(num, "->", index2string)
print(index2string, "->", string2index)
10 -> J
J -> 10

5. Access multiple rows, multiple columns

A. Slice access

for i in sheet['A1':'A10']:
    cell = i
(<Cell 'Sheet1'.A1>,)
(<Cell 'Sheet1'.A2>,)
(<Cell 'Sheet1'.A3>,)
(<Cell 'Sheet1'.A4>,)
(<Cell 'Sheet1'.A5>,)
(<Cell 'Sheet1'.A6>,)
(<Cell 'Sheet1'.A7>,)
(<Cell 'Sheet1'.A8>,)
(<Cell 'Sheet1'.A9>,)
(<Cell 'Sheet1'.A10>,)

 The above code accesses rows 1-10 of column A, corresponding to the area shown in the following figure:

  Similarly, you can use slices to access data in the same row:

for i in sheet['A1':'D1']:
    cell = i
(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>, <Cell 'Sheet1'.D1>)

B. Area Traversal

Traverse the specified table area, and select the size range of row and col to traverse the elements in the corresponding area:

for row in sheet.iter_rows(min_row=5, max_row=8, min_col=1, max_col=3):
    for cell in row:
        print(cell)
<Cell 'Sheet1'.A5>
<Cell 'Sheet1'.B5>
<Cell 'Sheet1'.C5>
<Cell 'Sheet1'.A6>
<Cell 'Sheet1'.B6>
<Cell 'Sheet1'.C6>
<Cell 'Sheet1'.A7>
<Cell 'Sheet1'.B7>
<Cell 'Sheet1'.C7>
<Cell 'Sheet1'.A8>
<Cell 'Sheet1'.B8>
<Cell 'Sheet1'.C8>

The above code accesses columns 1-3, which are columns AC, and rows 5-8, and the elements correspond to the following boxes:

C. Global Traversal

Traversing rows or cols actually traverses the entire sheet.

# 从 row 的角度遍历 sheet
for rows in sheet.rows:
    for row in rows:
        print(row.value)

# 从 col 的角度遍历 sheet
for cols in sheet.columns:
    for col in cols:
        print(col.value)

6. Get the number of rows and columns

# 获取行列数
rowNum = sheet.max_row
colNum = sheet.max_column
print("row: ", rowNum, "col: ", colNum)
row: 8 col: 4

7. Use formulas

A. Single point use formula

The following command defines the value at C15 as 5 + 5 = 10

sheet['C15'] = '=sum({}, {})'.format(5, 5)

B. Batch use formula

First create a new table sheet2, then the column names are Name, Score and add the corresponding values:

sheet2 = workbook.create_sheet("Sheet2", 0)
sheet2['A1'] = "Name"
sheet2['B1'] = "Score"
sheet2.append(['A', 1])
sheet2.append(['B', 2])
sheet2.append(['C', 3])
sheet2['C1'] = "Add_1"

 The formula is applied below, where the data in the Add_1 column is the value of B2-B4 + 1:

for row in range(2, 5):
    colLetter = get_column_letter(3)
    sheet2[colLetter + str(row)] = '=sum({}, {})'.format(sheet2.cell(row, 2).value, 1)

8. Save, read, delete tables

Where file_path is the storage of the corresponding table, delete the location:

# 保存表
workbook.save(file_path)

# 删表
workbook.remove(file_path)

# 读取sheet
wb = openpyxl.load_workbook(file_path)
sheet = wb.get_sheet_by_name(sheet_name)

Three. openpyxl style operation

1 Introduction

The above describes how to add data to the table, traverse and process the data in the table, and analyze the table style below. Now we know that each cell corresponds to a Cell Class, namely from openpyxl.cell import Cell :

 In addition to calling the .value method to obtain its corresponding value, the cell can also modify the following format:

A. font - font related

B. fill - fill color related

C.alignment - alignment related

D.border - border related

E.comment - Comment related

The following example is based on the basic sheet obtained by append in the previous step:

2.font font

The following is to switch the cell in the B2 position to the bold red font of the 11th bold font:

cell = sheet.cell(column=2, row=2)

# 指定 font 字体名 name + 大小 size + 加粗 blod + 斜体 italic + 颜色 color
font = Font(name='黑体简', size=11, italic=False, color="FF0000", bold=True)
cell.font = font

The meaning of the parameters here is:

name font
size size
italic italic
blood bold
color color

Details of parameters such as font style can also be viewed directly in excel, and you can see the effect in advance:

3.fill fill related

fill is mainly responsible for the cell fill color. The cells are filled with full fill and gradient fill as follows:

A. (2-3) position is filled with gray PatternFill

fgColor is the encoding of the corresponding color

cell = sheet5.cell(row=2, column=3)
cell.fill = PatternFill("solid", fgColor="00778899")

B. (2-4) position is filled with red and green gradient

A variety of colors can also be added in stop, which will achieve multi-level gradients

cell2 = sheet5.cell(row=2, column=4)
cell2.fill = GradientFill(stop=("007CFC00", "00FF4500"))

4.alignment alignment related

alignment = Alignment(horizontal="center", vertical="center", 
                      text_rotation=0, wrap_text=False)
cell = sheet.cell(row=2, column=3)
cell.alignment = alignment

Center the element at the (2,3) position, the meaning of the parameter is:

horizontal Horizontal alignment mode
vertical vertical alignment mode
text_rotation Rotation angle
wrap_text Whether to automatically wrap

5. border border related

Add boundaries to the (2,4) position elements, with green and black thin lines up and down, and red and green double lines on the left and right:

cell = sheet.cell(row=2, column=4)
cell.border = Border(left=Side(style='double', color="FF0000"),
                     right=Side(style='double', color="0000FF"),
                     top=Side(style='thin', color="00FF00"),
                     bottom=Side(style='thin'))

 Border contains a total of left, right, top, bottom, that is, the border lines of four directions: up, down, left, and right.

SIde is the attribute corresponding to the boundary line, and there are many styles. If you fill in the style and fill in any code, an error will be reported, and then the exception stack will display the style attributes supported by the current version, which can be selected according to your needs:

ValueError: Value must be one of {'dotted', 'dashed', 'mediumDashDotDot', 
'double', 'thick', 'dashDot', 'medium', 'slantDashDot', 'mediumDashDot', 'thin', 
'dashDotDot', 'hair', 'mediumDashed'}

6.comment comments

Annotate the (1,1) position element, the first parameter is the annotation content, and the second parameter is the corresponding Author:

# 注释
cell = sheet.cell(row=1, column=1)
comment = Comment("This is a comment!", "DDD")
cell.comment = comment

7.height-width related

sheet5.row_dimensions[1].height = 50
sheet5.column_dimensions['B'].width = 20

Adjust the row width and column height of cells in column 'B' and row '1':

8. Merge and unmerge cells

A. Merge cells

sheet.merge_cells("C1:D2")

Take C1 as the upper left corner of the border and D2 as the lower right corner of the border to merge cells, and the data will be retained as the Value corresponding to C1:

 B. Unmerge cells

sheet.unmerge_cells("C1:D2")

 The cells are unmerged, but the data of the remaining cells are not filled

Four. openpyxl practice

1. Case requirements

After the above explanation and analysis, we are familiar with the basic operation of openpyxl. Let's return to the problem mentioned in the introduction. We need to put the following data:

A	B	C	D
0.012 0.951 0.234 -0.137
0.038 0.999 -0.225 0.139

translates to:

 Here is a brief analysis of the conversion process:

A. Initialize Workbook and Sheet 

B. Add the corresponding element to the corresponding position of the Sheet through row, col

C. If the value is greater than 0, the corresponding position Cell color is changed to red, and the value less than 0 is changed to green

D. All cells are centered

E. Keep 1 decimal place

2. Case realization

def colorFulData(_sheet, _head, _values):
    # 定义字体
    _font = None
    # 获取 values 的长款
    _colNum = len(_head)
    _rowNum = len(_values)
    # 定义对齐方式
    _alignment = Alignment(horizontal="center", vertical="center", text_rotation=0, wrap_text=False)
    for _col in range(1, _colNum + 1):
        # col 转换并添加元素到 Cell
        _sheet[get_column_letter(_col) + str(1)] = head[_col - 1]
        _cell = _sheet.cell(1, _col)
        _cell.alignment = _alignment
        for _row in range(1, _rowNum + 1):
            # 保留一位小数
            _sheet[get_column_letter(_col) + str(_row + 1)] = format(values[_row - 1][_col - 1], '.1%')
            _cell = _sheet.cell(_row + 1, _col)
            numValue = float(values[_row - 1][_col - 1])
            # 根据值改变字体颜色
            if numValue > float(0):
                _font = Font(name='黑体简', size=11, italic=False, color="00FF0000", bold=True)
            elif numValue < float(0):
                _font = Font(name='黑体简', size=11, italic=False, color="0000FF00", bold=True)
            _cell.font = _font
            _cell.alignment = _alignment


head = ["A", "B", "C", "D"]
values = [[0.012, 0.951, 0.234, -0.137], [0.038, 0.999, -0.225, 0.139]]
colorFulData(sheet, head, values)
workbook.save(file_path)

You only need to pass in the header and the corresponding values ​​data. There is no try catch here, so the input value should be in a legal form, otherwise an error will be reported. More font styles, cell merging, cell filling, etc. can also be added In the corresponding Cell above, the following is the effect obtained by the implementation of the basic form:

Five. openpyxl expansion

The color of the above cells or the filled color all use the color code. The following are some commonly used color codes, which can be applied to your own programs if necessary:

    '00F0F8FF', '00FAEBD7', '0000FFFF', '007FFFD4', '00F0FFFF', '00F5F5DC', 
    '00FFE4C4', '00000000', '00FFEBCD', '000000FF', '008A2BE2', '00A52A2A', 
    '00DEB887', '005F9EA0', '007FFF00', '00D2691E', '00FF7F50', '006495ED',
    '00FFF8DC', '00DC143C', '0000FFFF', '0000008B', '00008B8B', '00B8860B', 
    '00A9A9A9', '00006400', '00BDB76B', '008B008B', '00556B2F', '00FF8C00',
    '009932CC', '008B0000', '00E9967A', '008FBC8F', '00483D8B', '002F4F4F',
    '0000CED1', '009400D3', '00FF1493', '0000BFFF', '00696969', '001E90FF', 
    '00B22222', '00FFFAF0', '00228B22', '00FF00FF', '00DCDCDC', '00F8F8FF', 
    '00FFD700', '00DAA520', '00808080', '00008000', '00ADFF2F', '00F0FFF0', 
    '00FF69B4', '00CD5C5C', '004B0082', '00FFFFF0', '00F0E68C', '00E6E6FA', 
    '00FFF0F5', '007CFC00', '00FFFACD', '00ADD8E6', '00F08080', '00E0FFFF', 
    '00FAFAD2', '0090EE90', '00D3D3D3', '00FFB6C1', '00FFA07A', '0020B2AA', 
    '0087CEFA', '00778899', '00B0C4DE', '00FFFFE0', '0000FF00', '0032CD32', 
    '00FAF0E6', '00FF00FF', '00800000','0066CDAA', '000000CD', '00BA55D3', 
    '009370DB', '003CB371', '007B68EE', '0000FA9A', '0048D1CC', '00C71585',
    '00191970', '00F5FFFA', '00FFE4E1', '00FFE4B5', '00FFDEAD', '00000080', 
    '00FDF5E6', '00808000', '006B8E23', '00FFA500', '00FF4500', '00DA70D6', 
    '00EEE8AA', '0098FB98', '00AFEEEE', '00DB7093', '00FFEFD5', '00FFDAB9',
    '00CD853F', '00FFC0CB', '00DDA0DD', '00B0E0E6', '00800080', '00FF0000', 
    '00BC8F8F', '004169E1', '008B4513', '00FA8072', '00FAA460', '002E8B57', 
    '00FFF5EE', '00A0522D', '00C0C0C0', '0087CEEB', '006A5ACD', '00708090',
    '00FFFAFA', '0000FF7F', '004682B4', '00D2B48C', '00008080', '00D8BFD8', 
    '00FF6347', '0040E0D0', '00EE82EE', '00F5DEB3', '00FFFFFF', '00F5F5F5', 
    '00FFFF00', '009ACD32'

Here you can simply modify the program to get the real color corresponding to the above code. There are still many beautiful colors to choose from: 

Guess you like

Origin blog.csdn.net/BIT_666/article/details/124329787