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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM