如何基于python操作excel并获取内容

爱蒂网

如何基于python操作excel并获取内容

2020-03-16 23:31:43 分类 / 代码专栏 来源 / 互联网

这篇文章主要介绍了如何基于python操作excel并获取内容,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了如何基于python操作excel并获取内容,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

背景:从excel表中获取请求url、请求数据、请求类型、预期结果

因此,需要学会如何使用python从excel获取这些信息

#coding=utf-8
import xlrd
#创建对象时,获取对应excel 表格
#读取Excel行数
#获取单元格内容
class OperationExcel:
  
  def __init__(self,file_name = None,sheet_id = 0):
    if file_name:
      self.file_name = file_name
      self.sheet_id = sheet_id
    else:
      self.file_name = '../data.xlsx'
      self.sheet_id = 0
    self.data =self.get_data()
  
  def get_line(self):
    return self.data.nrows
  
  def get_cell_value(self,row,col):
    return self.data.cell_value(row,col)
  
  def get_data(self):
    data = xlrd.open_workbook(self.file_name)
    tables = data.sheets()[self.sheet_id]
    return tables

if __name__ == '__main__':
  opers = OperationExcel()
  print opers.get_line()
  print opers.get_cell_value(1,0)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持爱蒂网。

猜你喜欢