Use openpyxl to split a sheet into multiple sheets

        This chapter introduces the application of openpyxl - splitting a sheet into multiple sheets. Here is a messy table with 200 rows, and the goal is to organize each class of the table into a sheet.

        Using openpyxl to deal with this kind of problem is more complicated. One idea is: first put the data into a dictionary, and then store the data in different sheets according to the different key-value pairs of the dictionary. Let's go step by step, first get the data of each row into a list:

import openpyxl
file = openpyxl.load_workbook(r"D:\临时\python试验\openpyxl\把文件拆分成多个sheet表.xlsx")
sheet_fd = file['Sheet1']          # 选择工作表

for row in sheet_fd.iter_rows(2,sheet_fd.max_row,1,sheet_fd.max_column,values_only=True):
    row_ls = [i for i in row]      # 每行生成列表
    print(row_ls)

        Then the first element of the list is used as the key of the dictionary, and the other forms a list as one of the values ​​of the dictionary.

import openpyxl
file = openpyxl.load_workbook(r"D:\临时\python试验\openpyxl\把文件拆分成多个sheet表.xlsx")
sheet_fd = file['Sheet1']

d = {}
for row in sheet_fd.iter_rows(2,sheet_fd.max_row,1,sheet_fd.max_column,values_only=True):
    row_ls = [i for i in row]
    jian = row_ls[0]        # 作为字典的键
    zhi = row_ls[1:]        # 除了第一个之外的元素组成列表作为字典的值
    d[jian] = d.get(jian,[]) + [zhi]        # !重点

print(d)

        Line 10 d.get(jian,[]) means to get the key jian in the dictionary d, if the key jian exists in the dictionary, then return the value corresponding to the key; if the key jian does not exist in the dictionary, then return an empty list.

        d.get(jian,[]) + [zhi] means to add an element (list zhi) to the returned value. Note that zhi must add [], because after adding [], [zhi] is [ ['Chen Jie', 47, 49, 29, 5]], the + sign is equivalent to extend, which adds the elements in the list instead of the entire list.

import openpyxl

file = openpyxl.load_workbook(r"D:\临时\python试验\openpyxl\把文件拆分成多个sheet表.xlsx")
sheet_fd = file['Sheet1']

d = {}
for row in sheet_fd.iter_rows(2,sheet_fd.max_row,1,sheet_fd.max_column,values_only=True):
    row_ls = [i for i in row]
    jian = row_ls[0]        # 作为字典的键
    zhi = row_ls[1:]        # 除了第一个之外的元素组成列表作为字典的值
    d[jian] = d.get(jian,[]) + [zhi]

new_file = openpyxl.Workbook()

for key,value in d.items():
    sheet = new_file.create_sheet(key)        # 创建键对应的文件
    sheet.append(['姓名','语文','数学','英语','综合科'])   # 写入表头
    for n_row in value:
        sheet.append(n_row)       # 写入数据行

new_file.remove(new_file['Sheet'])      # 移除创建new_file时默认产生的Sheet
new_file.save(r"D:\临时\python试验\openpyxl\一表拆成多表.xlsx")

        The excel file in this article: Link: https://pan.baidu.com/s/1RkLCCXuWfPz6rYcnuUJrrw?pwd=z3u1 
Extraction code: z3u1

Guess you like

Origin blog.csdn.net/m0_71559726/article/details/130367549