Created
September 22, 2012 23:23
-
-
Save pythonexcels/3768201 to your computer and use it in GitHub Desktop.
Extract Payrate from Multiple Spreadsheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# payroll_steve_jeff.py | |
# Report payrates for two employees across multiple spreadsheets | |
# | |
import win32com.client as win32 | |
import glob | |
import os | |
xlfiles = sorted(glob.glob("*.xls")) | |
print "Reading %d files..."%len(xlfiles) | |
steve = [] | |
jeff = [] | |
cwd = os.getcwd() | |
excel = win32.gencache.EnsureDispatch('Excel.Application') | |
for xlfile in xlfiles: | |
wb = excel.Workbooks.Open(cwd+"\\"+xlfile) | |
ws = wb.Sheets('PAYROLL') | |
xldata = ws.UsedRange.Value | |
names = [r[1] for r in xldata] | |
if u'SMITHFIELD, STEVE' in names: | |
indx = names.index(u'SMITHFIELD, STEVE') | |
steve.append(xldata[indx][4]) | |
else: | |
steve.append(0) | |
if u'JOHNSON, JEFF' in names: | |
indx = names.index(u'JOHNSON, JEFF') | |
jeff.append(xldata[indx][4]) | |
else: | |
jeff.append(0) | |
wb.Close() | |
print "File,Jeff,Steve" | |
for i in range(len(xlfiles)): | |
print "%s,%0.2f,%0.2f"%(xlfiles[i],jeff[i],steve[i]) | |
excel.Application.Quit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you please provide me the sample excel sheet for this script