excel-xls

本文提供了一个Python类用于操作Excel文件,包括获取行数、列数、工作表名称、单元格数据,设置单元格数据,获取使用范围地址及一块区域的数据等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from excel import Excel
class WorkBook(Excel):
    val1 = 'Total'
    val2 = 'Total+directive'

    list_content = list()  # the content of the current worksheet

    def __init__(self):
        super().__init__()

    def get_rows(self,sheet = ""):
        if sheet == '':
            sht = self.xlBook.Worksheets(1)
        else:
            sht = self.xlBook.Worksheets(sheet)
        self.rows = sht.usedrange.rows.count
        return sht.usedrange.rows.count

    def get_cols(self,sheet = ""):
        if sheet == '':
            sht = self.xlBook.Worksheets(1)
        else:
            sht = self.xlBook.Worksheets(sheet)
        self.cols = sht.usedrange.columns.count
        return sht.usedrange.columns.count

    def get_worksheets(self):
        for i in self.xls.worksheets:
            print(i.name)

    def getCell(self, row=1, col=1, sheet=""):  # 获取单元格的数据
        if sheet == '':
            sht = self.xlBook.Worksheets(1)
        else:
            sht = self.xlBook.Worksheets(sheet)
        return sht.Cells(row, col).Value

    def setCell(self, row, col, value, sheet = ''):  # 设置单元格的数据
        if sheet == '':
            sht = self.xlBook.Worksheets(1)
        else:
            sht = self.xlBook.Worksheets(sheet)
        sht.Cells(row, col).Value = value

    def get_usedrange(self,sheet = None ):
        if sheet == None:
            sht = self.xlBook.Worksheets(1)
        else:
            sht = self.xlBook.Worksheets(sheet)
        self.list_content = sht.usedrange.value
        #print(self.list_content)
        return sht.usedrange.address

    def getRange(self, sheet, row1, col1, row2, col2):  # 获得一块区域的数据,返回为一个二维元组
        "return a 2d array (i.e. tuple of tuples)"
        sht = self.xlBook.Worksheets(sheet)
        return sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value
    def find(self,value):
        print('ok')
        print(self.xlBook.activesheet.name)
        print(self.xlBook.activesheet.UsedRange.address)
        print(self.xls.cells.find(value))
        pass



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值