python-2.7 - 读取目录中的多个 Excel 文件并将特定单元格值写入另一个 Excel 工作表

标签 python-2.7 xlrd xlw

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

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

最佳答案

这工作正常!!!

#!/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')

关于python-2.7 - 读取目录中的多个 Excel 文件并将特定单元格值写入另一个 Excel 工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19246844/

相关文章:

Python 选择和计数元素

python - 使用 BeautifulSoup Python 获取特定文本,例如 "Something new"

python - 使用 python xlrd 从 Excel 单元格中获取公式

c++ - XLW - 编译的 XLL "The file format and extension don' t 匹配”

excel - 使用 xlrd/xlutils 复制工作表

python - 像我使用计算器一样使用 input() 函数是否安全?

python - 使用 xlrd 读取 excel 时出现数字格式问题

python - 如何在 python 中快速将 xlsx 文件转换为 csv 文件?

c++ - 在 C++ 中使用 Scripting.FileSystemObject 时,#import 之后的下一步是什么?