xlrd批量讀取xls格式excel檔案資料。

2021-09-02 17:42:14 字數 1337 閱讀 4982

要提取所有excel檔案裡的藍色標記的資料到一張表裡 ,效果如下:

主要採用了xlrd獲取xls檔案的單元格資料。後面用pandas存放資料並轉成了excel檔案。中間又加了一點多執行緒的東西。其實呢這個例子只有5個檔案,用不用多執行緒沒什麼影響.速度都很快.,0.1秒就可以完成了。最近一直在一點點地學習,又怕忘了,只得好好記下來了。沒辦法,笨鳥只能勤快點。

**如下:

# -*- coding: utf-8 -*-

"""created on tue nov 20 13:40:42 2018

@author: fanxiaolei

xlrd的單元格行列索引都是從0開始的。xlrd沒有關閉工作簿的函式。

"""import glob

import xlrd

import pandas as pd

import threading

import time

t1=time.clock()

res=

def read_excel(f):

global res

wb=xlrd.open_workbook(f)

ws=wb.sheet_by_index(0)

def run(spath):

threads=

xls=glob.glob(spath)

if xls:

for xl in xls:

t = threading.thread(target=read_excel,args=(xl,))

t.start()

for thread in threads:

thread.join()

if __name__=='__main__':

run('附件/*.xls')

df=pd.dataframe(res,columns=['姓名','承包','非承包'])

df.to_excel('合計.xlsx',sheet_name='合計',index=false)

print('耗時:',time.clock()-t1)

效果圖上面已經發過了。速度還是比較快的。平均耗時0.093秒就能完成,還可以的。

xlrd讀取Excel檔案

book xlrd.open workbook day 01.xls book.nsheetsbook.sheet names sh.name 獲取工作表總行數 sh.nrows 獲取工作表總列數 sh.ncolssh.cell value rowx rowx,colx cols for rowx ...

Python之xlrd模組讀取xls檔案與報錯解決

pip3 install xlrd sheet編號從0開始 rows,colnum編號均從0開始 合併的單元格僅返回第一格內容 sheets只能被呼叫一次,可獲取所有sheet取idx 無論 內資料型別是int還是str,純數字內容會返回float import xlrd file name lis...

python讀取excel檔案 xlrd

使用 xlrd 模組 1 安裝 xlrd 模組 xlrd 模組安裝 pip install xlrd ipython 裡面好像自帶有xlrd模組,不需要另外安裝 2 匯入模組 import xlrd 3 開啟檔案 data xlrd.open workbook f python.xls 4 獲取資料...