Python read write modify exception

Keywords: Excel Python

Article directory

1. Create write exception for the first time

from time import sleep
import xlrd
from xlutils.copy import copy
import xlwt

class OperateExcle:
    def __init__(self):
        self.fileName = "test.xlsx"

    # Create an exception and write data
    def creatWriteExcle(self):
        excle = xlwt.Workbook()  # Open excel
        testSheet = excle.add_sheet('test')  # Add a sheet named test
        testSheet.write(0, 0, "Xiao Ming")  # Write Xiaoming in row 0 and column 0
        testSheet.write(1, 0, "Small black")
        testSheet.write(2, 0, "Xiao Bai")
        testSheet.write(0, 1, "puppy")
        testSheet.write(1, 1, "kitten")
        testSheet.write(2, 1, "Little fish")
        excle.save(self.fileName)  # Save the specified path and filename

if __name__ == "__main__":
    operate = OperateExcle()
    operate.creatWriteExcle() # Create and write excle


Create the following:

2. Read excel internal data

from time import sleep
import xlrd
from xlutils.copy import copy
import xlwt

class OperateExcle:
    def __init__(self):
        self.fileName = "test.xlsx"

    # Read excle internal data
    def readExcle(self):
        excle = xlrd.open_workbook(self.fileName)  # Open corresponding file
        sheetData = excle.sheet_by_name("test")  # Get the corresponding sheet by name
        row = sheetData.row_values(0)  # Get all data of 0 row
        print("0 That's ok",row)
        col = sheetData.col_values(0)  # Get all data of 0 column
        print("0 column",col)
        data = sheetData.row_values(1)[1] # Get the data of 1 row and 1 column
        print("1 Row 1 columns",data)

if __name__ == "__main__":
    operate = OperateExcle()
    operate.readExcle()

3. Modify the existing exception

from time import sleep
import xlrd
from xlutils.copy import copy
import xlwt

class OperateExcle:
    def __init__(self):
        self.fileName = "test.xlsx"

    # Add or modify the existing excle. Close the excle file
    def  modifyExcle(self):
        oldExcle = xlrd.open_workbook(self.fileName)  # Open the existing table first
        newExcle = copy(oldExcle)  # Copy new file
        newExcleSheet = newExcle.get_sheet(0)  # Get the corresponding sheet according to index
        newExcleSheet.write(0, 0, "Wang Wang")  # Modify 0 row and 0 column to be Wang Wang
        newExcleSheet.write(0, 5, "Meow meow") # Add 0 rows and 5 columns as meow
        newExcle.save(self.fileName)  # Save the new file with the same name as the original file, then overwrite the original file

if __name__ == "__main__":
    operate = OperateExcle()
    operate.modifyExcle()

Of course, python can also use excle to merge cells, modify text styles, fonts, backgrounds,
Set operation mode, add hyperlink, etc.
More operations are not written out here, just add on this basis.

Document reference:
Using python to write data to excel

371 original articles published· Liked 127· 500000 visitors+
His message board follow

Posted by Aimless on Sat, 11 Apr 2020 07:29:42 -0700