Detailed explanation of xlrd and xlwt operation Excel files

There are many modules for Python to operate Excel, and each has its own advantages and disadvantages. Different modules support different operations and file types. The following is the support of each module:

.xls .xlsx get file content data input Modify file content keep style adjustment insert picture
xlrd × × × × ×
xlwt × × ×
xlutils × × × ×
xlwings
openpyxl ×
pandas × × ×

The comparison will reveal that the functions of xlwings are the most comprehensive and powerful, but its execution efficiency is also the highest. However, xlwings is relatively difficult to learn, and many concepts and operation methods in it are different from those when operating Excel. The problem with openpyxl is that it cannot operate the old version of excel files (.xls), and must operate in units of cells when manipulating data, and cannot directly manipulate the file content in rows or columns. The combination of xlrd , xlwt and xlutils can easily read, write and modify excel files, and can also operate Excel files in rows and columns more conveniently, but the disadvantage is that it is impossible to write and modify .xlsx files operate. When solving practical problems, readers can choose to use the appropriate tool according to their actual needs.

The following mainly introduces how the xlrd and xlwt modules can read and write .xls and .xlsx files.

1. xlrd gets Excel file content

It is very convenient for xlrd to read the content of Excel files, and the operation steps are as convenient as operating Excel files through Excel software.

1. Installation

pip install xlrd

2. use

Before using xlrd to get the content of the Excel file, you need to prepare an Excel file. In order to demonstrate the effect, I will use the following content as the content of the demonstration file:

The process of using xlrd to read Excel is the same as manually operating Excel files: open the workbook (Workbook) --> select the worksheet (sheet) --> operate the cell (cell)

2.1 Open the workbook

xlrd.open_workbook(excel文件路径)- Open the excel file corresponding to the specified path, and return the workbook object corresponding to the excel file.

import xlrd
wb = xlrd.open_workbook('files/data1.xls')

2.2 Select worksheet

A workbook may contain multiple worksheets. The case file data1.xls I gave above is a workbook that contains two worksheets: students and teacher . When obtaining the content of the excel file, you need to first determine which worksheet the data to be obtained comes from.

工作簿对象.sheet_names()- Get the sheet names of all worksheets in the workbook

s_names = wb.sheet_names()
print(s_names)

Results of the:

['students', 'teacher']

工作簿对象.sheets()- Get the worksheet objects corresponding to all worksheets in the workbook

工作簿对象.sheet_by_index(下标)- Get the worksheet object corresponding to the specified subscript

工作簿对象.sheet_by_name(表名)- Get the worksheet object corresponding to the specified table name

all_sheet = wb.sheets()
print(all_sheet)
print(all_sheet[0])

teacher_s = df.sheet_by_index(1)
print(teacher_s) 

students_s = df.sheet_by_name('students')
print(students_s)

Results of the:

[Sheet  0:<students>, Sheet  1:<teacher>]
Sheet  0:<students>
Sheet  1:<teacher>
Sheet  0:<students>

2.3 Get row and column information

工作表对象.nrows- Get the number of rows in the worksheet

工作表对象.ncols- Get the number of columns in the worksheet

print(students_s.nrows)
print(students_s.ncols)

Results of the:

5
4

工作表对象.row_values(行下标)- Get all the data in the row corresponding to the specified subscript, and return the result in the form of a list

工作表对象.col_values(列下标)- Get all the data in the column corresponding to the specified subscript, and the result is returned in the form of a list (the subscript starts from 0)

print(students_s.row_values(1))
print(students_s.col_values(0))

Results of the:

['小明', '男', 20.0, 99.0]
['姓名', '小明', '张三', '小花', '老王']

According to the previous method of obtaining the number of rows combined with obtaining the entire row, you can cycle through the entire excel line by line:

for x in range(students_s.nrows):
    print(students_s.row_values(x))

Results of the:

['姓名', '性别', '年龄', '分数']
['小明', '男', 20.0, 99.0]
['张三', '男', 25.0, 87.0]
['小花', '女', 22.0, 95.0]
['老王', '男', 30.0, 77.0]

2.4 Operating cells

工作表对象.row(行下标)- Get all the cells in the row corresponding to the specified subscript. The result is a list, and the elements in the list are cell objects (note that they are not cell contents)

工作表对象.col(列下标)- Get all the cells in the column of the specified subscript object.

工作表对象.cell(行下标, 列下标)- Get the cell object corresponding to the specified row subscript and column subscript.

单元格对象.value- Get the value in the specified cell

print(students_s.row(1))
print(students_s.col(1))
print(students_s.cell(3, 0))
print(students_s.row(1)[0].value)
print(students_s.col(1)[2].value)
print(students_s.cell(3, 0).value)

Results of the:

[text:'小明', text:'男', number:20.0, number:99.0]
[text:'性别', text:'男', text:'男', text:'女', text:'男']
text:'小花'
小明
男
小花

The above are all related operations of xlrd to obtain the content of Excel files. Generally speaking, it is relatively simple. Compared with openpyxl , xlrd can obtain the content of the worksheet in units of rows or columns. It is really convenient.

2. xlwt writes Excel files

1. Installation

pip install xlwt

2. use

When xlwt writes to an Excel file, it can only write to the Excel file created by xlwt , and cannot directly write to the existing Excel file. If you want to write to an existing Excel file, you need to use the xlutils module later.

2.1 Create a new workbook and worksheet

Creating a new workbook is actually creating a new Excel file. Unlike manually creating an Excel file through Excel software, xlwt will not automatically create a worksheet when creating a new workbook, so at least one worksheet needs to be created after the workbook is created.

xlwt.Workbook()- Create a new workbook object and return

工作簿对象.add_sheet(表名)- Create a new worksheet in the workbook

工作簿对象.save(文件路径)- Save the Excel file corresponding to the workbook object to the specified location (the file must be saved in xls format)

import xlwt

wb = xlwt.Workbook()
fruits_s = wb.add_sheet('水果')
vegetable_s = wb.add_sheet('蔬菜')
wb.save('files/data2.xls')

Results of the:

2.2 Write data to the specified cell

工作表对象.write(行下标, 列下标, 内容)- Write the specified content to the specified cell (a unique cell can be determined by row subscript and column subscript)

fruits_s.write(0, 0, '名称')
fruits_s.write(0, 1, '单价')
fruits_s.write(0, 2, '数量')
fruits_s.write(0, 3, '总价')

wb.save('files/data2.xls')		# 注意:所有写操作完成后必须保存

Results of the:

One thing to pay special attention to here is that by default, the same cell cannot be repeatedly written. If you need to repeatedly write to the same cell, you must set the value of the parameter cell_overwrite_ok to True when creating a worksheet. .

fruits_s = fruits_s = wb.add_sheet('水果')
fruits_s.write(0, 0, '名称')
fruits_s.write(0, 1, '单价')
fruits_s.write(0, 2, '数量')
fruits_s.write(0, 3, '总价')
fruits_s.write(0, 3, '总计')		# 对0、3这个位置的单元格重复进行写操作
wb.save('files/data2.xls')

Results of the:

Exception: Attempt to overwrite cell: sheetname='水果' rowx=0 colx=3

If you set cell_overwrite_ok to True when creating a worksheet , writing to the same cell multiple times will not report an error:

fruits_s = wb.add_sheet('水果', cell_overwrite_ok=True)
fruits_s.write(0, 0, '名称')
fruits_s.write(0, 1, '单价')
fruits_s.write(0, 2, '数量')
fruits_s.write(0, 3, '总价')
fruits_s.write(0, 3, '总计')		# 对0、3这个位置的单元格重复进行写操作
wb.save('files/data2.xls')

Results of the:
 

Qianfeng Education Java Introduction Full Set of Video Tutorials (java core technology, suitable for java zero foundation, necessary for self-study of Java)

 

Guess you like

Origin blog.csdn.net/longz_org_cn/article/details/132063662