forked from roytuts/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_excel.py
More file actions
62 lines (51 loc) · 1.68 KB
/
write_excel.py
File metadata and controls
62 lines (51 loc) · 1.68 KB
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
import xlwt
class OrderDetail(object):
def __init__(self, order_date, region, rep, item, units, unit_cost, total):
self.order_date = order_date
self.region = region
self.rep = rep
self.item = item
self.units = units
self.unit_cost = unit_cost
self.total = total
def write_to_excel(filename, sheet, odList):
book = xlwt.Workbook()
sh = book.add_sheet(sheet)
#total items
total_items = len(odList)
print("total_items: ", total_items)
#print on console
for od in odList:
print(od.order_date)
print(od.region)
print(od.rep)
print(od.item)
print(od.units)
print(od.unit_cost)
print(od.total)
print('\n')
#write headers
sh.write(0, 0, 'OrderDate')
sh.write(0, 1, 'Region')
sh.write(0, 2, 'rep')
sh.write(0, 3, 'Item')
sh.write(0, 4, 'Units')
sh.write(0, 5, 'Unit Cost')
sh.write(0, 6, 'Total')
#write row values
for idx in range(len(odList)):
#print(idx)
sh.write(idx+1, 0, odList[idx].order_date)
sh.write(idx+1, 1, odList[idx].region)
sh.write(idx+1, 2, odList[idx].rep)
sh.write(idx+1, 3, odList[idx].item)
sh.write(idx+1, 4, odList[idx].units)
sh.write(idx+1, 5, odList[idx].unit_cost)
sh.write(idx+1, 6, odList[idx].total)
book.save(filename)
od1 = OrderDetail('2016-1-6', 'East', 'Jones', 'Pencil', 95, 1.99, 189.05)
od2 = OrderDetail('2016-1-23', 'Central', 'Kivell', 'Binder', 50, 19.99, 999.50)
od3 = OrderDetail('2016-2-9', 'Central', 'Jardine', 'Pencil', 36, 4.99, 179.64)
od4 = OrderDetail('2016-2-26', 'Central', 'Gill', 'Pen', 27, 19.99, 539.73)
odList = [od1, od2, od3, od4]
write_to_excel('OrderDetails.xls', 'Sheet1', odList)