Python reads and writes Excel documents

I. Introduction

It is very convenient to use python to read and write the Excel library. After installing the two libraries of xlwt and xlrd, you can play happily. Next, let's see how to use it

Second, the required library

  • xlwt library, write Excel documents
  • xlrd library, read Excel documents

3. Object type

  • Workbook type: workbook is a workbook, corresponding to an entire Excel document, which does not need to be explained;
  • Worksheet type: worksheet is a worksheet. An Excel document can have several worksheets, as shown in the figure below. The worksheet object represents one of the tables below;
    insert image description here

Fourth, use the xlwt library to write Excel documents

Knowing the object type, let's try to write the nine-nine multiplication table into an Excel document

import library

  • import xlwt

write example

Next, we write a method to complete the function, and the comments are all in the code

def writeExample(savePath):
    #创建工作簿
    workbook = xlwt.Workbook(encoding="utf-8");

    #创建工作表
    worksheet = workbook.add_sheet("九九乘法表")

    #写入
    for i in range(0, 9):
        for j in range(0, i+1):
            str = ("%d * %d = %d"%(i+1,j+1,(i+1)*(j+1)))
            #实际写入语句
            worksheet.write(i,j,str)

    #保存
    workbook.save(savePath)

Effect

Nine nine multiplication table

Five, use the xlrd library to read Excel documents

import library

import xlrd

Read example

def readExample(excelPath):
    #获得book对象
    workbook = xlrd.open_workbook(excelPath)

    #获得sheet对象的3种写法
    worksheet = workbook.sheets()[0] #通过sheet()方法,并结合下标来获取
    # worksheet = workbook.sheet_by_index(0) #直接通过下标来获取
    # worksheet = workbook.sheet_by_name("your excel name") #通过名字来获取


    #因为xlrd库里的api没提示,这里手动指定类型,这样写起来就有提示了
    assert isinstance(worksheet,xlrd.sheet.Sheet)
    #自己写时若不知道类型,可以解开下面的打印,然后复制一下
    #print(type(worksheet))

    #打印行数和列数
    print("行数",worksheet.nrows)
    print("列数",worksheet.ncols)

    #获取第i,j个格子的值的3种写法
    print(worksheet.cell(0,0).value)
    print(worksheet.cell_value(0,0))
    print(worksheet.row(0)[0].value)

    #遍历
    for i in range(worksheet.nrows):
        for j in range(worksheet.ncols):
            print(worksheet.cell_value(i,j))

Effect

Read renderings

Six, complete code



import xlwt

excelPath = "./九九乘法表.xls"

def writeExample(savePath):
    #创建工作簿
    workbook = xlwt.Workbook(encoding="utf-8");

    #创建工作表
    worksheet = workbook.add_sheet("九九乘法表")

    #写入
    for i in range(0, 9):
        for j in range(0, i+1):
            str = ("%d * %d = %d"%(i+1,j+1,(i+1)*(j+1)))
            #实际写入语句
            worksheet.write(i,j,str)

    #保存
    workbook.save(savePath)

writeExample(excelPath)


import xlrd

def readExample(excelPath):
    #获得book对象
    workbook = xlrd.open_workbook(excelPath)

    #获得sheet对象的3种写法
    worksheet = workbook.sheets()[0] #通过sheet()方法,并结合下标来获取
    # worksheet = workbook.sheet_by_index(0) #直接通过下标来获取
    # worksheet = workbook.sheet_by_name("your excel name") #通过名字来获取


    #因为xlrd库里的api没提示,这里手动指定类型,这样就有提示了
    assert isinstance(worksheet,xlrd.sheet.Sheet)
    #自己写时若不知道类型,可以解开下面的打印,然后复制一下
    #print(type(worksheet))

    #打印行数和列数
    print("行数",worksheet.nrows)
    print("列数",worksheet.ncols)

    #获取第i,j个格子的值的3种写法
    print("获取第(0,0)个格子的值的3种写法")
    print(worksheet.cell(0,0).value)
    print(worksheet.cell_value(0,0))
    print(worksheet.row(0)[0].value)

    print()

    #遍历
    for i in range(worksheet.nrows):
        for j in range(worksheet.ncols):
            print(worksheet.cell_value(i,j),end="\t")
        print()

readExample(excelPath)

Guess you like

Origin blog.csdn.net/aaa27987/article/details/122920478
Recommended