-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy patherppivotdragdrop.py
226 lines (201 loc) · 9.7 KB
/
erppivotdragdrop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#
# erppivotdragdrop.py:
# Load raw EPR data, clean up header info,
# insert additional data fields and build 5 pivot tables
# Support drag and drop of multiple spreadsheets
#
import ctypes
import itertools
import re
import sys
import traceback
import win32com.client as win32
win32c = win32.constants
tablecount = itertools.count(1)
def addpivot(wb, sourcedata, title, filters=(), columns=(),
rows=(), sumvalue=(), sortfield=""):
"""Build a pivot table using the provided source location data
and specified fields
"""
newsheet = wb.Sheets.Add()
newsheet.Cells(1, 1).Value = title
newsheet.Cells(1, 1).Font.Size = 16
# Build the Pivot Table
tname = "PivotTable%d" % next(tablecount)
pc = wb.PivotCaches().Add(SourceType=win32c.xlDatabase,
SourceData=sourcedata)
pt = pc.CreatePivotTable(TableDestination="%s!R4C1" % newsheet.Name,
TableName=tname,
DefaultVersion=win32c.xlPivotTableVersion10)
wb.Sheets(newsheet.Name).Select()
wb.Sheets(newsheet.Name).Cells(3, 1).Select()
for fieldlist, fieldc in ((filters, win32c.xlPageField),
(columns, win32c.xlColumnField),
(rows, win32c.xlRowField)):
for i, val in enumerate(fieldlist):
wb.ActiveSheet.PivotTables(tname).PivotFields(val).Orientation = fieldc
wb.ActiveSheet.PivotTables(tname).PivotFields(val).Position = i+1
wb.ActiveSheet.PivotTables(tname).AddDataField(
wb.ActiveSheet.PivotTables(tname).PivotFields(sumvalue[7:]),
sumvalue,
win32c.xlSum)
if len(sortfield) != 0:
wb.ActiveSheet.PivotTables(tname).PivotFields(sortfield[0]).AutoSort(sortfield[1], sumvalue)
newsheet.Name = title
# Uncomment the next command to limit output file size, but make sure
# to click Refresh Data on the PivotTable toolbar to update the table
#
# newsheet.PivotTables(tname).SaveData = False
return tname
def runexcel(args):
"""Open the spreadsheet ABCDCatering.xls, clean it up,
and add pivot tables
"""
sawerror = False
print("Running erppivotdragdrop")
if len(args) == 1:
ctypes.windll.user32.MessageBoxW(None, "Error: Please drag at least one Excel file", "erppivotdragdrop", 0)
sys.exit(1)
try:
excel = win32.gencache.EnsureDispatch('Excel.Application')
for fname in args[1:]:
if not re.search(r'(?i)\.xlsx?$', fname):
print("Error: File %s doesn't seem to be an Excel file, expecting .xls or .xlsx file" % fname)
sawerror = True
continue
if not re.match('[A-Za-z]:', fname):
print("Error: erppivotdragdrop doesn't support command line execution")
print(" Please drag and drop the Excel file onto the program icon")
sawerror = True
continue
print("Processing %s" % fname)
try:
wb = excel.Workbooks.Open(fname)
except:
print("Failed to open Excel file %s, skipping" % fname)
sawerror = True
continue
try:
ws = wb.Sheets('Sheet1')
except:
print("Failed to open Sheet 'Sheet1' in file %s, skipping" % fname)
wb.Close()
sawerror = True
continue
xldata = ws.UsedRange.Value
newdata = []
for row in xldata:
if len(row) == 13 and row[-1] is not None:
newdata.append(list(row))
lasthdr = "Col A"
for i, field in enumerate(newdata[0]):
if field is None:
newdata[0][i] = lasthdr + " Name"
else:
lasthdr = newdata[0][i]
logolookup = {'Applied Materials': 'AMAT', 'Electronic Arts': 'EA',
'Hewlett-Packard': 'HP', 'KLA-Tencor': 'KLA'}
if ("Company Name" in newdata[0]):
cindx = newdata[0].index("Company Name")
newdata[0][cindx+1:cindx+1] = ["Logo Name"]
for rcnt in range(1, len(newdata)):
if newdata[rcnt][cindx] in logolookup:
newdata[rcnt][cindx+1:cindx+1] = [logolookup[newdata[rcnt][cindx]]]
else:
newname = newdata[rcnt][cindx].split()[0]
newdata[rcnt][cindx+1:cindx+1] = [newname]
logolookup[newdata[rcnt][cindx]] = newname
foodlookup = {'Caesar Salad': 'Salad', 'Cheese Pizza': 'Pizza',
'Cheeseburger': 'Burger', 'Chocolate Sundae': 'Dessert',
'Churro': 'Snack', 'Hamburger': 'Burger', 'Hot Dog': 'HotDog',
'Pepperoni Pizza': 'Pizza', 'Potato Chips': 'Snack',
'Soda': 'Drink'}
if ("Food Name" in newdata[0]):
cindx = newdata[0].index("Food Name")
newdata[0][cindx+1:cindx+1] = ["Food Category"]
for rcnt in range(1, len(newdata)):
if newdata[rcnt][cindx] in foodlookup:
newdata[rcnt][cindx+1:cindx+1] = [foodlookup[newdata[rcnt][cindx]]]
else:
newdata[rcnt][cindx+1:cindx+1] = ['UNDEFINED']
rowcnt = len(newdata)
colcnt = len(newdata[0])
wsnew = wb.Sheets.Add()
wsnew.Range(wsnew.Cells(1, 1), wsnew.Cells(rowcnt, colcnt)).Value = newdata
wsnew.Columns.AutoFit()
src = "%s!R1C1:R%dC%d" % (wsnew.Name, rowcnt, colcnt)
# What were the total sales in each of the last four quarters?
addpivot(wb, src,
title="Sales by Quarter",
filters=(),
columns=(),
rows=("Fiscal Quarter",),
sumvalue="Sum of Net Booking",
sortfield=())
# What are the sales for each food item in each quarter?
addpivot(wb, src,
title="Sales by Food Item",
filters=(),
columns=("Food Name",),
rows=("Fiscal Quarter",),
sumvalue="Sum of Net Booking",
sortfield=())
# Who were the top 10 customers for ABCD Catering in 2009?
addpivot(wb, src,
title="Top 10 Customers",
filters=(),
columns=(),
rows=("Company Name",),
sumvalue="Sum of Net Booking",
sortfield=("Company Name", win32c.xlDescending))
# Who was the highest producing sales rep for the year?
addpivot(wb, src,
title="Top Sales Reps",
filters=(),
columns=(),
rows=("Sales Rep Name", "Company Name"),
sumvalue="Sum of Net Booking",
sortfield=("Sales Rep Name", win32c.xlDescending))
# What food item had the highest unit sales in Q4?
ptname = addpivot(wb, src,
title="Unit Sales by Food",
filters=("Fiscal Quarter",),
columns=(),
rows=("Food Name",),
sumvalue="Sum of Quantity",
sortfield=("Food Name", win32c.xlDescending))
wb.Sheets("Unit Sales by Food").PivotTables(ptname).PivotFields("Fiscal Quarter").CurrentPage = "2009-Q4"
# What food category had the highest unit sales in Q4?
ptname = addpivot(wb, src,
title="Unit Sales by Food Category",
filters=("Fiscal Quarter",),
columns=(),
rows=("Food Category",),
sumvalue="Sum of Quantity",
sortfield=("Food Category", win32c.xlDescending))
wb.Sheets("Unit Sales by Food Category").PivotTables(ptname).PivotFields("Fiscal Quarter").CurrentPage = "2009-Q4"
outfname = re.sub(r'(?i)\.xlsx?', '_new', fname)
try:
if int(float(excel.Version)) >= 12:
wb.SaveAs(outfname+'.xlsx', win32c.xlOpenXMLWorkbook)
print("Wrote %s" % outfname+'.xlsx')
else:
wb.SaveAs(outfname+'.xls')
print("Wrote %s" % outfname+'.xls')
except:
print("Error: Problem during file save")
sawerror = True
wb.Close()
if sawerror:
print("Errors occurred, please check the above messages")
ctypes.windll.user32.MessageBoxW(None, "Error: Problems occurred, please check them and try again", "erppivotdragdrop", 0)
else:
print("erppivotdragdrop Finished")
ctypes.windll.user32.MessageBoxW(None, "erppivotdragdrop Finished", "erppivotdragdrop", 0)
except:
traceback.print_exc()
print("Errors occurred, please check the messages above")
ctypes.windll.user32.MessageBoxW(None, "Error: Problems occurred, please check them and try again", "erppivotdragdrop", 0)
excel.Application.Quit()
if __name__ == "__main__":
runexcel(sys.argv)