打开多个EXCEL文件,获取数据,然后写入Pandas /输出到单个Excel文件

时间:2015-02-23 03:13:53

标签: python excel

我是编程和python(使用python27)的新手,我正在尝试编写代码以从特定文件夹中打开多个excel文件,获取特定的单元格值,然后输出到单个excel文件,每行代表打开每个文件的记录。

这是我的代码,我坚持下一个流程应该是什么:

import os
import glob
import xlrd
import datetime


yesterday = datetime.date.fromordinal(datetime.date.today().toordinal()-1)

for root,dirs,files in os.walk(src):
    files = [ _ for _ in files if _.endswith('.xlsx') ]

for xlsfile in files:
    wb = xlrd.open_workbook(os.path.join(root,xlsfile))
    sht = wb.sheet_by_name('Sheet1')
    name = xlsfile
    rev = sht.cell_value(0,1)
    gp = sht.cell_value(1,1)
    sls = sht.cell_value(2,1)
    sp = sht.cell_value(3,1)
    cps = sht.cell_value(4,1)
    print yesterday, name, rev, gp, sls, sp, cps

1 个答案:

答案 0 :(得分:0)

此处输出到xls文件的更改::

import os
import glob
import pyexcel as pe
import pyexcel.ext.xls
import xlrd
import datetime


yesterday = datetime.date.fromordinal(datetime.date.today().toordinal()-1)

for root,dirs,files in os.walk(src):
    files = [ _ for _ in files if _.endswith('.xlsx') ]

array = []

for xlsfile in files:
    wb = xlrd.open_workbook(os.path.join(root,xlsfile))
    sht = wb.sheet_by_name('Sheet1')
    name = xlsfile
    rev = sht.cell_value(0,1)
    gp = sht.cell_value(1,1)
    sls = sht.cell_value(2,1)
    sp = sht.cell_value(3,1)
    cps = sht.cell_value(4,1)
    array.append([yesterday, name, rev, gp, sls, sp, cps])
    print yesterday, name, rev, gp, sls, sp, cps

pe.save_as(array=array, "myfile.xls")

可以找到更多文档here