Python3 integrated xlrd

1. Follow the xlrd module

pip install xlrd

2. Use of xlrd function

2.1: Import xlrd module

import xlrd

2.2: Open the specified file

x1 = xlrd.open_workbook("data.xls")

2.3: Get the sheet:

  • Get all sheet names: x1.sheet_names()
  • Get the number of sheets: x1.nsheets
  • Get all sheet objects: x1.sheets()
  • Search by sheet name: x1.sheet_by_name("test")
  • Search by index: x1.sheet_by_index(3)
# -*- coding:utf-8 -*-

import xlrd
import os

filename = "demo.xls"
filePath = os.path.join(os.getcwd(), filename)

print filePath

# 1、打开文件
x1 = xlrd.open_workbook(filePath)

# 2、获取sheet对象
print 'sheet_names:', x1.sheet_names()  # 获取所有sheet名字
print 'sheet_number:', x1.nsheets        # 获取sheet数量
print 'sheet_object:', x1.sheets()       # 获取所有sheet对象
print 'By_name:', x1.sheet_by_name("test")  # 通过sheet名查找
print 'By_index:', x1.sheet_by_index(3)  # 通过索引查找

2.4 Get the summary data of the sheet:

  • Get the sheet name: sheet1.name
  • Get the total number of rows: sheet1.nrows
  • Get the total number of columns: sheet1.ncols
# -*- coding:utf-8 -*-

import xlrd
import os
from datetime import date,datetime

filename = "demo.xls"
filePath = os.path.join(os.getcwd(), filename)
print filePath

# 打开文件
x1 = xlrd.open_workbook(filePath)

# 获取sheet的汇总数据
sheet1 = x1.sheet_by_name("plan")
print "sheet name:", sheet1.name   # get sheet name
print "row num:", sheet1.nrows  # get sheet all rows number
print "col num:", sheet1.ncols  # get sheet all columns number

2.5 Cell reading

2.5.1 Row operations

  • sheet1.row_values(0) # Get all the contents of the first row, merge the cells, display the value in the first row, and leave the others blank.
  • sheet1.row(0) # Get the cell value type and content
  • sheet1.row_types(0) # Get cell data type
# -*- coding:utf-8 -*-

import xlrd
import os
from datetime import date,datetime

filename = "demo.xls"
filePath = os.path.join(os.getcwd(), filename)

x1 = xlrd.open_workbook(filePath)
sheet1 = x1.sheet_by_name("plan")

# 单元格批量读取
print sheet1.row_values(0)  # 获取第一行所有内容,合并单元格,首行显示值,其它为空。
print sheet1.row(0)         # 获取单元格值类型和内容
print sheet1.row_types(0)   # 获取单元格数据类型

2.5.2 Column operation

  • sheet1.row_values(0, 6, 10) # Take the first row, the 6th to 10th columns (excluding the 10th table)
  • sheet1.col_values(0, 0, 5) # Take the first column, rows 0~5 (excluding row 5)
  • sheet1.row_slice(2, 0, 2) # Get the cell value type and content
  • sheet1.row_types(1, 0, 2) # Get cell data type
# -*- coding:utf-8 -*-

import xlrd
import os
from datetime import date,datetime

filename = "demo.xls"
filePath = os.path.join(os.getcwd(), filename)

print filePath

# 1、打开文件
x1 = xlrd.open_workbook(filePath)
sheet1 = x1.sheet_by_name("plan")

# 列操作
print sheet1.row_values(0, 6, 10)   # 取第1行,第6~10列(不含第10表)
print sheet1.col_values(0, 0, 5)    # 取第1列,第0~5行(不含第5行)
print sheet1.row_slice(2, 0, 2)     # 获取单元格值类型和内容,同sheet1.row(0)
print sheet1.row_types(1, 0, 2)     # 获取单元格数据类型

2.6 Reading a specific cell

2.6.1 Get cell value:

  • sheet1.cell_value(1, 2)
  • sheet1.cell(1, 2).value
  • sheet1.row(1)[2].value 

2.6.2 Get the cell type:

  • sheet1.cell(1, 2).ctype
  • sheet1.cell_type(1, 2)
  • sheet1.row(1)[2].ctype
# -*- coding:utf-8 -*-

import xlrd
import os
from datetime import date,datetime

filename = "demo.xls"
filePath = os.path.join(os.getcwd(), filename)

x1 = xlrd.open_workbook(filePath)
sheet1 = x1.sheet_by_name("plan")

# 特定单元格读取
# 取值
print sheet1.cell_value(1, 2)
print sheet1.cell(1, 2).value
print sheet1.row(1)[2].value

#取类型
print sheet1.cell(1, 2).ctype
print sheet1.cell_type(1, 2)
print sheet1.row(1)[2].ctype

2.7 xlrd supports data types

  • Empty: 0
  • String: 1
  • Number: 2
  • Date: 3
  • Boolean: 4
  • error:5


3. The xlrd function is blocked

class ExcelRead:
    def __init__(self,excelpath):
        '''打开指定的excel文件'''
        self.excelread=xlrd.open_workbook(excelpath)

    def get_AllSheets(self):
        '''获取所有的sheets'''
        sheets=self.excelread.sheet_names()
        return sheets

    def get_SheetbyName(self,sheetname):
        '''
        通过name获取指定的sheet
        :param sheetname: sheet名称
        :return: sheet
        '''
        sheet=self.excelread.sheet_by_name(sheetname)
        return sheet

    def get_nrows(self,sheetname):
        '''
        获取指定sheet的行数
        :param sheetname:sheet名称
        :return: sheet总行数
        '''
        sheet=self.get_SheetbyName(sheetname)
        rows_num=sheet.nrows
        return rows_num

    def get_ncolumns(self,sheetname):
        '''
        获取指定sheet的列数
        :param sheetname:sheet名称
        :return: sheet总列数
        '''
        sheet=self.get_SheetbyName(sheetname)
        columns_num=sheet.ncols
        return columns_num

    def get_value(self,sheetname,row,col):
        '''
        通过行、列定位单元格获取value
        :param row: 行
        :param col: 列
        :return: 单元格value
        '''
        sheet=self.get_SheetbyName(sheetname)
        getvalue=sheet.cell_value(row,col)
        return getvalue

    def get_row_values(self,sheetname,row):
        '''
        获取excel一行的数据
        :param sheetname:sheet名称
        :param row: 行数
        :return: 返回一行的数据
        '''
        row_value_list=[]
        sheet=self.get_SheetbyName(sheetname)
        sheet_row_len=sheet.ncols       #通过列数获取一行元素的长度
        for i in range(sheet_row_len):
            get_value=self.get_value(sheetname,row,i)
            row_value_list.append(get_value)
        return row_value_list

    def get_column_values(self,sheetname,col):
        '''
        获取excel一列的数据
        :param sheetname:sheet名称
        :param col: 列数
        :return: 返回一列的数据
        '''
        col_value_list=[]
        sheet=self.get_SheetbyName(sheetname)
        sheet_column_len=sheet.nrows    #通过行数获取一列元素的长度
        for i in range(sheet_column_len):
            get_value=self.get_value(sheetname,i,col)
            col_value_list.append(get_value)
        return col_value_list

 

Guess you like

Origin blog.csdn.net/zhouzhiwengang/article/details/113105019