-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopenpyxl_simple_python2excel.py
More file actions
69 lines (54 loc) · 1.33 KB
/
openpyxl_simple_python2excel.py
File metadata and controls
69 lines (54 loc) · 1.33 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
63
64
65
66
67
68
69
import pathlib
import random
from dataclasses import dataclass
from typing import List
from openpyxl import Workbook
from openpyxl.chart import LineChart, Reference
@dataclass
class Sale:
quantity: int
@dataclass
class Product:
id: str
name: str
sales: List[Sale]
products = []
for idx in range(1, 6):
sales: List[Sale] = []
for _ in range(5):
sale = Sale(quantity=random.randrange(5, 100))
sales.append(sale)
product = Product(id=str(idx), name="Product %s" % idx, sales=sales)
products.append(product)
workbook = Workbook()
sheet = workbook.active
sheet.append(
["product Id", "product name", "month 1", "month 2", "month 3", "month4", "month 5"]
)
for product in products:
data = [product.id, product.name]
for sale in product.sales:
data.append(sale.quantity)
sheet.append(data)
chart = LineChart()
data = Reference(
worksheet=sheet,
min_row=2,
max_row=6,
min_col=2,
max_col=7,
)
chart.add_data(data, titles_from_data=True, from_rows=True)
sheet.add_chart(chart, "B8")
cats = Reference(
worksheet=sheet,
min_row=1,
max_row=1,
min_col=3,
max_col=7,
)
chart.set_categories(cats)
chart.x_axis.title = "Months"
chart.y_axis.title = "Sales (per unit)"
filepath = pathlib.Path.home().joinpath("Downloads/test13.xlsx")
workbook.save(filepath)