我需要从目录中存在的100个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')
任何人都可以帮忙成功吗?
答案 0 :(得分: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')