1. 程式人生 > >python讀取當前目錄下的所有的excel檔案

python讀取當前目錄下的所有的excel檔案

1,使用dos中的dir命令搜尋

檢視dir用法

dir /?

搜尋.xlsx檔案,選項/b是除去多餘的資訊,/s遞迴搜尋,包含當前目錄的子目錄。

dir *.xlsx /b /s 

結果輸出到檔案,供其他程式呼叫

dir *.xlsx /b /s >filelist.txt

2, 用python自己的函式os.walk()

列出當前資料夾及其子資料夾中的所有檔案

在根據字尾選出.xlsx

import os
filelist=[]

for root, dirs, files in os.walk(".", topdown=False):
    for name in files:
        str=os.path.join(root, name)
        if str.split('.')[-1]=='xlsx':
            filelist.append(str)

filelist

3,下面是一個用openpyxl庫處理表格的樣例。

import os
from openpyxl import Workbook    
from openpyxl import load_workbook

os.system("DIR *.* /B>list.txt")
f = open("list.txt", "r")
name=[]
while True:    
    line = f.readline()
    if line:        
        line=line[0:len(line)-1] #delete char '\n'
        #print (line)
        if  line.endswith('xlsx',len(line)-4,len(line) ):
            name.append(line)
    else:    
        break 
		
if len(name)==0:
	print('No .xlsx file in this directory')
	exit()

outbook = Workbook()   #輸出book物件  
outsheet = outbook.active   #獲取book的sheet   

rbook = load_workbook( name[0] ) 
rsheet = rbook.get_sheet_names()  
sheet1 = rbook.get_sheet_by_name(rsheet[0])
for j in range(1,12):
    outsheet.cell(row=1,column=j).value = sheet1.cell(row=1,column=j).value
    
outr=2
for n in name:
    print('name: ',end='')
    print(n)
    rbook = load_workbook( n ) 
    rsheet = rbook.get_sheet_names()  
    sheet1 = rbook.get_sheet_by_name(rsheet[0])
    
    for i in range(1,9):       
        if sheet1.cell(row=i,column=2).value==None and not sheet1.cell(row=i,column=3).value == None:
            for j in range(1,10):
                outsheet.cell(row=outr,column=j).value = sheet1.cell(row=i,column=j).value
                
                print ( sheet1.cell(row=i,column=j).value )
            break;
    outr=outr+1
outbook.save('out.xlsx')  #儲存輸出