Python学习-提取excel表格中数据

xlrd模块安装方法:pip install xlrd

运用xlrd和re实现提取excel表格中所有数据,并获取其中某一个值

运用代码如下:

import xlrd
import re

def open_excel(inpath):
      data = xlrd.open_workbook(inpath,encoding_override='utf-8')
      table = data.sheets()[1] #选中表的sheet
      nrows = table.nrows  #获取当前sheet表的行号
      ncols = table.ncols  #获取当前sheet表的列号
      list = [] #用于存放某一列数据
      for i in range(1,nrows): #遍历每行的数据,0为表头
          alldata = table.row_values(i) #循环输出每行数据
          result = alldata[1] #取出表中第一列数据
          list.append(result) #写入空列表
      for s in range(0,len(list)):
          name = re.findall(r'正则表达式(.+?)',list[s]) #正则提取数据中的某个值

inpath = "excel文件路径"
open_excel(inpath)
import xlrd

def r_xlrd(xlsx_path="./test_prodid.xlsx"):
    #打开excel表格
    data = xlrd.open_workbook(xlsx_path)

    #读取第一个表单sheet
    table_sheet = data.sheets()[0]
    #指定表单名称获取sheet
    table_sheet1 = data.sheet_by_name("Sheet1")
    #通过索引的方式获取sheet
    table_sheet2 = data.sheet_by_index(0)

    #输出的表单为一个对象
    print(table_sheet)
    print(table_sheet1)
    print(table_sheet2)

    #输入表格的行数
    table_nrows = table_sheet.nrows
    print(table_nrows)

    #输出表格的列数
    table_ncols = table_sheet.ncols
    print(table_ncols)

    #输出表格某一行数据
    table_row = table_sheet.row_values(0)
    print(table_row)

    #输出表格某一列数据
    table_col = table_sheet.col_values(0)
    print(table_col)

    #输出某一个单元格的值
    table_cell = table_sheet.cell(0,1).value
    print(table_cell)

    return table_sheet,table_nrows,table_ncols,table_row,table_col,table_cell

if __name__ == '__main__':
    xlsx = r_xlrd(xlsx_path="./test_prodid.xlsx")

 

最近 xlrd  更新到了 2.0.1版本,读取.xlsx格式时报错 ”xlrd.biffh.XLRDError: Excel xlsx file; not supported“

是因为2.0.1版本只支持 .xls格式的

解决办法:

卸载掉最新的xlrd:pip uninstall xlrd

安装旧版版的xlrd:pip install xlrd ==1.2.0

import xlrd
from xlrd import xldate_as_tuple
import datetime

#将excel表格内容导入到tables列表中
def import_excel(excel):
  for rown in range(excel.nrows):
   array = {'id':'','name':''}
   array['id'] = table.cell_value(rown,0)
   array['name'] = table.cell_value(rown,1)
   tables.append(array)


if __name__ == "__main__":
    # 读取excel表格数据
    # 导入需要读取的第一个Excel表格的路径
    data1 = xlrd.open_workbook(r"excel的路径")
    table = data1.sheets()[0]
    # 创建一个空列表,存储Excel的数据
    tables = []

    import_excel(table)
    print(tables)

 新建表格,写入内容

import xlwt

def w_xlsx():
    '''创建新的xlsx表格,编码为 ascii 编码'''
    wb = xlwt.Workbook("./w_xlsx.xls")
    ws = wb.add_sheet("weng")
    ws.write(0,0,label = "ascii")
    ws.write(0,1,label = "编码")
    ws.write(1,0,label = "完成表格编写")

    wb.save("./w_xlsx.xls")

if __name__ == '__main__':
     w_xlsx()

 修改表格内容,导入xlutils.copy中的copy函数

import xlrd
from xlutils.copy import copy

def u_xlsx(u_xlsx="./w_xlsx.xls"):
    #打开表格文件
    rb = xlrd.open_workbook(u_xlsx)

    #复制表单内容
    wb = copy(rb)
    ws = wb.get_sheet(0)
    ws.write(0, 0,'hanged!')
    ws.write(8,0,label = '好的1')
    wb.save("./w_xlsx.xls")

if __name__ == '__main__':
    u_xlsx()

 

import xlrd

def import_excel(excel_path):
    #新建一个空列表存储表格数据
    tables = []
    # 导入需要读取的第一个Excel表格的路径
    data1 = xlrd.open_workbook(excel_path)
    # 选择excel表格中的第一个sheet
    table = data1.sheets()[0]
    for rown in range(table.nrows):
       array = {}
       array['url'] = table.cell_value(rown,0)
       array['解耦前'] = table.cell_value(rown,1)
       array['解耦后'] = table.cell_value(rown,2)

       tables.append(array)
    return tables

if __name__ == '__main__':
    a = import_excel()
    print(a)

 

posted @ 2021-02-05 10:53  小哈别闹  阅读(4450)  评论(0编辑  收藏  举报