Skip to content

Commit c3e0581

Browse files
authored
Add files via upload
1 parent 3a59150 commit c3e0581

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

Diff for: python-write-excel/readme.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You can read tutorial https://www.roytuts.com/write-excel-file-using-python/

Diff for: python-write-excel/write_excel.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import xlwt
2+
3+
class OrderDetail(object):
4+
5+
def __init__(self, order_date, region, rep, item, units, unit_cost, total):
6+
self.order_date = order_date
7+
self.region = region
8+
self.rep = rep
9+
self.item = item
10+
self.units = units
11+
self.unit_cost = unit_cost
12+
self.total = total
13+
14+
def write_to_excel(filename, sheet, odList):
15+
book = xlwt.Workbook()
16+
sh = book.add_sheet(sheet)
17+
18+
#total items
19+
total_items = len(odList)
20+
print("total_items: ", total_items)
21+
22+
#print on console
23+
for od in odList:
24+
print(od.order_date)
25+
print(od.region)
26+
print(od.rep)
27+
print(od.item)
28+
print(od.units)
29+
print(od.unit_cost)
30+
print(od.total)
31+
print('\n')
32+
33+
#write headers
34+
sh.write(0, 0, 'OrderDate')
35+
sh.write(0, 1, 'Region')
36+
sh.write(0, 2, 'rep')
37+
sh.write(0, 3, 'Item')
38+
sh.write(0, 4, 'Units')
39+
sh.write(0, 5, 'Unit Cost')
40+
sh.write(0, 6, 'Total')
41+
42+
#write row values
43+
for idx in range(len(odList)):
44+
#print(idx)
45+
sh.write(idx+1, 0, odList[idx].order_date)
46+
sh.write(idx+1, 1, odList[idx].region)
47+
sh.write(idx+1, 2, odList[idx].rep)
48+
sh.write(idx+1, 3, odList[idx].item)
49+
sh.write(idx+1, 4, odList[idx].units)
50+
sh.write(idx+1, 5, odList[idx].unit_cost)
51+
sh.write(idx+1, 6, odList[idx].total)
52+
53+
book.save(filename)
54+
55+
od1 = OrderDetail('2016-1-6', 'East', 'Jones', 'Pencil', 95, 1.99, 189.05)
56+
od2 = OrderDetail('2016-1-23', 'Central', 'Kivell', 'Binder', 50, 19.99, 999.50)
57+
od3 = OrderDetail('2016-2-9', 'Central', 'Jardine', 'Pencil', 36, 4.99, 179.64)
58+
od4 = OrderDetail('2016-2-26', 'Central', 'Gill', 'Pen', 27, 19.99, 539.73)
59+
60+
odList = [od1, od2, od3, od4]
61+
62+
write_to_excel('OrderDetails.xls', 'Sheet1', odList)

0 commit comments

Comments
 (0)