2

我需要从目录中存在的 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')

任何人都可以帮助成功地做到这一点吗?

4

1 回答 1

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')
于 2013-10-09T04:50:31.150 回答