Python combat: Use xlrd read Excel files

Encircle the wood, was born in Haomo; nine units from tired soil; a thousand miles begins with one step. ------ * I "moral"

I. Introduction

Recently are learning to use Ali cloud RPA content found using python development, but at the moment Ali cloud RPA is closed source. But the personal feeling and automation development essentially the same, which read and write Excel files in python have a lot of frame. So I read the relevant information online, and do a little reading Excel cases, write a blog post now read Excel to share knowledge about the use of xlrd library.

Second, the basic concept

In Excel mainly related to three concepts: Workbooks, Sheets, Cells. Wherein excel sheet is a Workbook; Sheet worksheet is a page table; Cell is a simple grid. Usually read and write Excel substantially three steps as follows: Open Workbook, positioning Sheet, operation Cell. Read and write the following points were introduced a few common approach of python.

Third, read Excel

Here we use a case study to demonstrate the Python read Excel process. First, we need to prepare in advance a copy Excel, the content contains Sheet1, Sheet2, Sheet3 three. Sheet1 in which the content shown below, now class information and student information content in Excel we want to read. Printed to the console.
Here Insert Picture Description

  1. Import library xlrd
import xlrd

If not, then you can use the library xlrd pip import the library:

pip install xlrd
  1. Open Excel file
data = xlrd.open_workbook(filename)  # 文件名以及路径,如果路径或者文件名有中文给前面加一个r拜师原生字符。
  1. Sheet read
    here provides two ways acquiring Sheet: 1 by obtaining indexed sequential Sheet; Sheet 2 acquired by name.
table = data.sheets()[0]          #通过索引顺序获取
table = data.sheet_by_index(sheet_indx)) #通过索引顺序获取
table = data.sheet_by_name(sheet_name)#通过名称获取
  1. Cell reading cell
    after obtaining the Sheet, we can read the table of contents in the Sheet. When following the information we need to read the cell, the following method may be used.
table.cell(rowx,colx)   #返回单元格对象
table.cell_type(rowx,colx)    #返回单元格中的数据类型
table.cell_value(rowx,colx)   #返回单元格中的数据
  1. Operative
    Sometimes we need to operate the entire row of the data, the following method may be used.
nrows = table.nrows  #获取该sheet中的有效行数
table.row(rowx)  #返回由该行中所有的单元格对象组成的列表
table.row_slice(rowx)  #返回由该列中所有的单元格对象组成的列表
table.row_types(rowx, start_colx=0, end_colx=None)    #返回由该行中所有单元格的数据类型组成的列表
table.row_values(rowx, start_colx=0, end_colx=None)   #返回由该行中所有单元格的数据组成的列表
table.row_len(rowx) #返回该列的有效单元格长度

Fourth, real

Here we have to read the contents of Excel just mentioned,

  1. Read the class name.
  2. Basic information of students to start reading from line 6.
    Here Insert Picture Description
# -*- coding: utf-8 -*-
# 读取excel
import xlrd


def read_excel():
    print("-" * 50)
    # 打开Excel
    data = xlrd.open_workbook(r"D:\work\py_workspace\TestDemo1\Test.xlsx")
    sheets = data.sheet_names()
    print("文件中共有{}个Sheet,名称如下:".format(len(sheets)))
    for item in sheets:
        print(item)
    print("-" * 50)

    # 读取Sheet
    sheet_table = data.sheet_by_name("Sheet1")
    print("读取Sheet1内容,有效行数{},有效列数{}".format(sheet_table.nrows, sheet_table.ncols))
    print("-" * 50)

    # 读取单元格
    school_name = sheet_table.cell(1, 1)
    print("班级:", school_name.value)
    print("-" * 50)

    # 读取行
    rows_count = sheet_table.nrows  # 获取总行数
    print("班级学生信息如下:")
    for i in range(5, rows_count):
        row_list = sheet_table.row_slice(i)
        print(row_list)


if __name__ == "__main__":
    read_excel()

Run the following code:
Here Insert Picture Description

V. Related Links

The following links can also understand Python other related content, we hope the relevant content can help. Thank you!
Link 1: Python combat: Python write Excel files

Published 19 original articles · won praise 67 · views 20000 +

Guess you like

Origin blog.csdn.net/m1090760001/article/details/103103951