2013-10-08 3 views
2

디렉토리에있는 100 개의 Excel 통합 문서에서 특정 셀 값을 읽고 별도의 Excel 시트에 해당 데이터를 써야합니다. 다음은 내가 실수 한 오류입니다.여러 개의 Excel 파일을 하나의 디렉토리에 저장하고 특정 셀 값을 다른 Excel 시트에 작성

#!/usr/bin/env python 
import os 
import xlwt 
import xlrd 

index=0 
workbook = xlwt.Workbook() 
Testsheet = workbook.add_sheet('test') 
print "Enter the row you want" 
row=input() 
print "Enter the column you want" 
col=input() 

for file in os.listdir('.'): 
    wb = xlrd.open_workbook(file) 
    wb.sheet_names() 
    sh = wb.sheet_by_name(u'Sheet1') 
    data = sh.cell(row,col).value 
    Testsheet.write(0, index, data) 
    index=index+1 

workbook.save('Test.xls') 

누구든지이 작업을 성공적으로 수행 할 수 있습니까?

답변

2

괜찮습니다!

#!/usr/bin/env python 
import os 
import xlwt 
import xlrd 


index=0 

workbook = xlwt.Workbook() 
Testsheet = workbook.add_sheet('test') 
print "Enter the row you want" 
row=input() 
print "Enter the column you want" 
col=input() 
path= 'E:/Test' 
for root,dirs,files in os.walk(path): 
xlsfiles=[ _ for _ in files if _.endswith('.xls') ] 
for xlsfile in xlsfiles: 
    wb = xlrd.open_workbook(os.path.join(root,xlsfile)) 
    n = len(wb.sheets()) 
for s in range(n) : 
     sheet = wb.sheet_by_index(s) 
     data=sheet.cell(row,col).value 
     print data 
     Testsheet.write(index, 0, data) 
    index=index+1 

workbook.save('Test.xls')