Use Python to read and write Excel data!

Compared with other programming languages, we all know that Python's biggest advantage is its simple code and rich third-party open source libraries for developers to use. With the popularity of data analysis in recent years, Python has also become one of the most popular programming languages. As for reading and storing data, for ordinary people, apart from databases, the most common one is Microsoft Excel.
Insert image description here

1. Preparation

1.1 First introduction to Excel

Microsoft Excel is a spreadsheet software written by Microsoft for computers using Windows and Apple Macintosh operating systems.

1.2 Differences in formats

There are two formats, xls and xlsx, in Excel. The difference between them is:

  • The file formats are different. xls is a unique binary format. Its core structure is a compound document type structure, while the core structure of xlsx is an XML type structure. It uses XML-based compression to make it take up less space. This is the meaning of the last x in xlsx.
  • The versions are different. xls is the file format generated by Excel2003 and previous versions, and xlsx is the file format generated by Excel2007 and later versions.
  • Compatibility varies. The xlsx format is backward compatible and compatible with the xls format.

1.3 Use of libraries

Among the modules that come with Python are the xlrd and xlwt modules for xls format, but these two libraries are only for xls operations. When we want to operate xlsx format files, we need to use the openpyxl third-party library.
Insert image description here

1.4 Overall idea

When using the above modules, theoretically we can fully operate the reading and writing of Excel in different formats. Many people are confused, what is the role of this article? Wouldn’t it be great if we just study the corresponding three modules directly?

The answer is: Although these libraries have completely converted Excel’s concepts of files, tables, rows, and columns into objects in Python, each operation requires traversing every cell, and many times we even spend a lot of time. On the edge of thinking about looping cells, this in itself is reinventing the wheel, so I spent half a day sorting out the following six functions.

Insert image description here

Second code display

2.1 xlz format

2.1.1 Read xls format file

def read_xls_excel(url,index):
    '''
    读取xls格式文件
    参数:
        url:文件路径
        index:工作表序号(第几个工作表,传入参数从1开始数)
    返回:
        data:表格中的数据
    '''
    # 打开指定的工作簿
    workbook = xlrd.open_workbook(url)
    # 获取工作簿中的所有表格
    sheets = workbook.sheet_names()
    # 获取工作簿中所有表格中的的第 index 个表格
    worksheet = workbook.sheet_by_name(sheets[index-1])
    # 定义列表存储表格数据
    data = []
    # 遍历每一行数据
    for i in range(0, worksheet.nrows):
        # 定义表格存储每一行数据
        da = []
        # 遍历每一列数据
        for j in range(0, worksheet.ncols):
            # 将行数据存储到da列表
            da.append(worksheet.cell_value(i, j))
        # 存储每一行数据
        data.append(da)
    # 返回数据
    return data

2.1.2 Write xls format file

def write_xls_excel(url,sheet_name,two_dimensional_data):
  '''
    写入xls格式文件
    参数:
        url:文件路径
        sheet_name:表名
        two_dimensional_data:将要写入表格的数据(二维列表)
    '''
    # 创建工作簿对象
    workbook = xlwt.Workbook()
    # 创建工作表对象
    sheet = workbook.add_sheet(sheet_name)
    # 遍历每一行数据
    for i in range(0,len(two_dimensional_data)):
        # 遍历每一列数据
        for j in range(0,len(two_dimensional_data[i])):
            # 写入数据
            sheet.write(i,j,two_dimensional_data[i][j])
    # 保存
    workbook.save(url)
    print("写入成功")

2.1.3 Append to write xls format file

def write_xls_excel_add(url, two_dimensional_data, index):
    '''
    追加写入xls格式文件
    参数:
        url:文件路径
        two_dimensional_data:将要写入表格的数据(二维列表)
        index:指定要追加的表的序号(第几个工作表,传入参数从1开始数)
    '''
    # 打开指定的工作簿
    workbook = xlrd.open_workbook(url)
    # 获取工作簿中的所有表格
    sheets = workbook.sheet_names()
    # 获取指定的表
    worksheet = workbook.sheet_by_name(sheets[index-1])
    # 获取表格中已存在的数据的行数
    rows_old = worksheet.nrows
    # 将xlrd对象拷贝转化为xlwt对象
    new_workbook = copy(workbook)
    # 获取转化后工作簿中的第index个表格
    new_worksheet = new_workbook.get_sheet(index-1)
    # 遍历每一行数据
    for i in range(0, len(two_dimensional_data)):
        # 遍历每一列数据
        for j in range(0, len(two_dimensional_data[i])):
            # 追加写入数据,注意是从i+rows_old行开始写入
            new_worksheet.write(i+rows_old, j, two_dimensional_data[i][j])
    # 保存工作簿
    new_workbook.save(url)
    print("追加写入成功")

2.2 xlsx format

2.2.1 Read xlsx format file

def read_xlsx_excel(url, sheet_name):
    '''
    读取xlsx格式文件
    参数:
        url:文件路径
        sheet_name:表名
    返回:
        data:表格中的数据
    '''
    # 使用openpyxl加载指定路径的Excel文件并得到对应的workbook对象
    workbook = openpyxl.load_workbook(url)
    # 根据指定表名获取表格并得到对应的sheet对象
    sheet = workbook[sheet_name]
    # 定义列表存储表格数据
    data = []
    # 遍历表格的每一行
    for row in sheet.rows:
        # 定义表格存储每一行数据
        da = []
        # 从每一行中遍历每一个单元格
        for cell in row:
            # 将行数据存储到da列表
            da.append(cell.value)
        # 存储每一行数据
        data.append(da)
    # 返回数据
    return data

2.2.2 Write xlsx format file

def write_xlsx_excel(url, sheet_name, two_dimensional_data):
    '''
    写入xlsx格式文件
    参数:
        url:文件路径
        sheet_name:表名
        two_dimensional_data:将要写入表格的数据(二维列表)
    '''
    # 创建工作簿对象
    workbook = openpyxl.Workbook()
    # 创建工作表对象
    sheet = workbook.active
    # 设置该工作表的名字
    sheet.title = sheet_name
    # 遍历表格的每一行
    for i in range(0, len(two_dimensional_data)):
        # 遍历表格的每一列
        for j in range(0, len(two_dimensional_data[i])):
            # 写入数据(注意openpyxl的行和列是从1开始的,和我们平时的认知是一样的)
            sheet.cell(row=i + 1, column=j + 1, value=str(two_dimensional_data[i][j]))
    # 保存到指定位置
    workbook.save(url)
    print("写入成功")

2.2.3 Append to write xlsx format file

def write_xlsx_excel_add(url, sheet_name, two_dimensional_data):
    '''
    追加写入xlsx格式文件
    参数:
        url:文件路径
        sheet_name:表名
        two_dimensional_data:将要写入表格的数据(二维列表)
    '''
    # 使用openpyxl加载指定路径的Excel文件并得到对应的workbook对象
    workbook = openpyxl.load_workbook(url)
    # 根据指定表名获取表格并得到对应的sheet对象
    sheet = workbook[sheet_name]
    for tdd in two_dimensional_data:
        sheet.append(tdd)
    # 保存到指定位置
    workbook.save(url)
    print("追加写入成功")

I hope this article will be helpful to you who are learning Python!

at last

Let me share with you a complete set of Python learning materials, free of charge! free! free! I compiled them all when I was studying. It’s not easy to organize. Please like and share~

Scan the CSDN official certification QR code below on WeChat to get it!

1. Learning routes in all directions of Python

The Python all-direction route is to organize the commonly used technical points of Python to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively.
Insert image description here

2. Python learning software

If a worker wants to do his job well, he must first sharpen his tools. The commonly used development software for learning Python is here, saving everyone a lot of time.
Insert image description here

3. Python introductory learning video

When we watch videos and learn, we can't just move our eyes and brain but not our hands. The more scientific learning method is to use them after understanding. At this time, hands-on projects are very suitable.
Insert image description here

4. Python exercises

Check learning results
Insert image description here

5. Python practical cases

Optical theory is useless. You must learn to follow along and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases.
Insert image description here

6. Python interview materials

We must learn Python to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and Alibaba bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
Insert image description here
Insert image description here
Friends who need it can scan the CSDN official certification QR code below on WeChat to get it for free ! !

Guess you like

Origin blog.csdn.net/maiya_yaya/article/details/131509384