ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python的excel表格操作,数据提取分析

2021-10-26 10:02:13  阅读:214  来源: 互联网

标签:row1 sheet 表格 Python self excel cell column row


自己编写了一些表格的操作方法,只需传入表格路径和表的位置,就能在根据自己使用的方法展示不同类型的数据如:字典,列表,字典里套列表,列表里套列表,查询最大行数,在指定的行数插入数据等,有疑问欢迎评论

class Excel:  #表格操作
    def __init__(self,filename,sheetname):  #传入表和表名称
        self.filename = filename
        self.sheetname = sheetname
        self.wb = openpyxl.load_workbook(self.filename)     #生成文件对象,表示要操作的是哪个文件
        self.sheet = self.wb[self.sheetname]
        self.maxrow = self.sheet.max_row
        self.maxcolumn = self.sheet.max_column

#最大行
    def getMaxRow(self):
        maxrow = self.sheet.max_row
        return maxrow

#最大列
    def getMaxColumn(self):
        maxcolumn=self.sheet.max_column
        return maxcolumn
#读取xlsx中某一行的内容
    def listRow(self,rowname):
        list1=[]
        maxcolumn=self.sheet.max_column
        for i in range(maxcolumn):
            row1=self.sheet.cell(row=rowname,column=i+1).value
            list1.append(row1)
        return list1

    def dictRow(self, rownum=1):
        dict1={}
        maxcolumn = self.sheet.max_column
        for i in range(maxcolumn):
            key=self.sheet.cell(row=1,column=i+1).value
            row1 = self.sheet.cell(row=rownum, column=i + 1).value
            dict1[key]=row1
        return dict1
#获取某一列的内容  #以列表的显示显示
    def listColumn(self,maxco):
        list1 = []
        for i in range(self.sheet.max_row):
            row1 = self.sheet.cell(row=i+1, column=maxco).value
            list1.append(row1)
        return list1

# 获取某一列的内容  #以字典的形式的显示,第一行的内容是字典的键
    def dictColumn(self,maxco):
        dict1={}
        maxrow = self.sheet.max_row
        for i in range(maxrow):
            key = self.sheet.cell(row=1, column=i + 1).value
            row1 = self.sheet.cell(row=i+1, column=maxco).value
            dict1[key]=row1
        return dict1
#获取所有的数据,以列表里面套字典的方式读取出来
    def dictAll(self):
        list1=[]
        dict2={}
        list2=[]
        for i in range(1,self.maxcolumn+1):
            list1.append(self.sheet.cell(row=1,column=i).value)
        for n in range(2,self.maxrow+1):
            for x in range(1,self.maxcolumn+1):
                dict2[list1[x-1]]=self.sheet.cell(row=n,column=x).value
            list2.append(dict2)
            dict2={}
        return list2
#在指定单元格里写数据:
    def wtxls(self,rowunm,colnum,data):
        self.sheet.cell(row=rowunm,column=colnum).value=data
        self.wb.save(self.filename)
#获取所有的数据,用列表的方式
    def listAll(self):
        row1=[]
        row2=[]
        maxrow=self.sheet.max_row
        maxcol = self.sheet.max_column
        for i in range(1,maxrow):
            for x in range(maxcol):
                row1.append(self.sheet.cell(row=i + 1, column=x + 1).value)
            row2.append(row1)
            row1=[]
        return row2
#获取所有行的内容 但是指定列
    def listData(self,startCol,endCol):
        row1 = []
        row2 = []
        for i in range(1, self.sheet.max_row):
            for x in range(startCol,endCol+1):
                row1.append(self.sheet.cell(row=i + 1, column=x).value)
            row2.append(row1)
            row1 = []
        return row2

标签:row1,sheet,表格,Python,self,excel,cell,column,row
来源: https://blog.csdn.net/m0_58002043/article/details/120966221

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有