How to use Python to manipulate Excel files? Just reading this blog is enough!

foreword

How to use Python to manipulate Excel files? Just reading this blog is enough!

In our work, we often need to process and analyze data. Excel, as a widely used data analysis tool, is well known by many people. However, for some users with non-technical background, how to operate Excel may be somewhat difficult. At this time, Python has become a very useful tool.

This article will introduce how to use Python to read and write Excel files. First, we will introduce the third-party libraries that can be used in Python xlrd, xlwtand xlutils, through examples to show "how to use xlwtthe library to write data into an Excel file", "how to use xlrdthe library to read data from an Excel file" and "How to use the cooperation of three libraries to read and write operations at the same time".

Through the introduction of this article, you will learn:

  • How to get the value of the cell;
  • How to iterate over the entire worksheet;
  • How to create a new worksheet and cell, and write data into the cell;
  • How to use save()method to save Excel file to disk.

If you want to learn how to use Python to manipulate Excel files, then this article is for you. Hope it helps you better understand and apply this tool.

1. Write to an Excel file

First of all, let's learn, randomly generate data, write it into an Excel file and save it, the library used is xlwt, the installation command, the pip install xlwtinstallation is simple and convenient, no dependencies, and very fast.

1.1. Create a new WorkBook object

import xlwt
wb = xlwt.Workbook()

1.2, create a new sheet

sheet = wb.add_sheet('第一个sheet')

1.3, write data

head_data = ['姓名','地址','手机号','城市']
for head in head_data:
    sheet.write(0,head_data.index(head),head)

writeThe function writes data in x rows and x columns, and the header data is always the first row, so row 0. The column of the data is the index of the list where the current data is located, just use the index function directly.

1.4. Create fake data

With the header data, now start to write the content, which are random names, random addresses, random numbers, random cities, and the source of the data is the faker library, a library that creates fake data for testing. The installation command: pip install faker.

Because the header information has been written, the next step is to write data from the first line, with four data in each line, and 99 user data are to be written, so use a loop to loop 99 times, the code is as follows:

import faker
fake = faker.Faker()
for i in range(1,100):
    sheet.write(i,0,fake.first_name() + ' ' + fake.last_name())
    sheet.write(i,1,fake.address())
    sheet.write(i,2,fake.phone_number())
    sheet.write(i,3,fake.city())

1.5, save as xls file

wb.save('虚假用户数据.xls')

Then find the file and open the xls file with office or wps:

2. Read Excel files

The writing of the file has been completed, and the next step is to learn the reading operation of Excel. The library for reading Excel is xlrd, which corresponds to read; the installation command of xlrd:pip install xlrd

2.1. Open the Excel file

import xlrd
wb = xlrd.open_workbook('虚假用户数据.xls')

2.2. Read Excel data

# 获取文件中全部的sheet,返回结构是list。
sheets = wb.sheets()
# 通过索引顺序获取。
sheet = sheets[0]
# 直接通过索引顺序获取。
sheet = wb.sheet_by_index(0)
# 通过名称获取。
sheet = wb.sheet_by_name('第一个sheet')

2.3, print data

# 获取行数
rows = sheet.nrows
# 获取列数
cols = sheet.ncols
for row in range(rows):
    for col in range(cols):
        print(sheet.cell(row,col).value,end=' , ')
    print('\n')

Printing effect (only intercept part):

3. Append content to the existing Excel file

Requirement: Add an additional 50 pieces of user data to the "false user data.xls", that is, title + data, reaching 150 pieces.

3.1. Import library

import xlrd
from xlutils.copy import copy

3.2. Use xlrd to open the file, and then xlutils assigns the opened workbook

wb = xlrd.open_workbook('虚假用户数据.xls',formatting_info=True)
xwb = copy(wb)

3.3. After having a workbook, start to specify the sheet and get the total number of rows of this sheet

sheet = xwb.get_sheet('第一个sheet')
rows = sheet.get_rows()

Specify the sheet named "the first sheet", and then get all the rows

3.4. With the specific number of rows, and then ensure that the original data does not change, write data from the 101st row

import faker
fake = faker.Faker()
for i in range(len(rows),150):
    sheet.write(i,0,fake.first_name() + ' ' + fake.last_name())
    sheet.write(i,1,fake.address())
    sheet.write(i,2,fake.phone_number())
    sheet.write(i,3,fake.city())

The range function starts from len(rows) and ends at 150-1, a total of 50 items. The faker library is used to create fake data. This was used to write data before, and 50 pieces were written in a loop.

3.5, finally save it

xwb.save('虚假用户数据.xls')

Use xwb, which is the workbook object after the operation, just save the original file name directly.

4. Summary

This article introduces three commonly used libraries in Python: xlrd, xlwt and xlutils, which are used to read Excel files, write Excel files and process Excel files respectively. These libraries have their own advantages and disadvantages, and they need to be selected according to specific needs in actual use.

At the same time, this article also provides some sample code to demonstrate how to use these libraries. Through these sample codes, readers can better understand the usage and operation steps of these libraries.

Finally, readers are reminded to read their documentation and API carefully when using these libraries to avoid unnecessary errors.

5. Reference materials

Guess you like

Origin blog.csdn.net/huangge1199/article/details/130948097