-
Notifications
You must be signed in to change notification settings - Fork 0
/
solar_system_example.py
82 lines (65 loc) · 2.33 KB
/
solar_system_example.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
"""This is a standard example of how to use an API to get data to fill in a template.
The SpaceX example `spacex_example.py` is a more advanced example using this approach."""
import cloudofficeprint as cop
import requests
# Get solar system data from https://api.le-systeme-solaire.net/rest/bodies/
res = requests.get('https://api.le-systeme-solaire.net/rest/bodies/').json()
# Setup Cloud Office Print server
SERVER_URL = "https://api.cloudofficeprint.com/"
API_KEY = "YOUR_API_KEY" # Replace by your own API key
server = cop.config.Server(
SERVER_URL,
cop.config.ServerConfig(api_key=API_KEY)
)
# Create the main element collection that contains all data
data = cop.elements.ElementCollection()
# Add the title to the data
data.add(cop.elements.Property('main_title', 'The solar system'))
# Add the source for the data
data.add(cop.elements.Hyperlink(
name='data_source',
url='https://api.le-systeme-solaire.net/rest/bodies/',
text='Data source'
))
# Process data: we only want planets
planet_list = []
for body in res['bodies']:
if body['isPlanet']:
collec = cop.elements.ElementCollection.from_mapping(body)
planet_list.append(collec)
planets = cop.elements.ForEach('planets', planet_list)
data.add(planets)
# Add planet radius chart to data
color = [None for _ in planet_list]
color[0] = '#7298d4'
radius_series = cop.elements.PieSeries(
x=[planet['name'] for planet in planets.as_dict['planets']],
y=[planet['equaRadius'] for planet in planets.as_dict['planets']],
name='radius',
colors=color
)
radius_chart_options = cop.elements.ChartOptions(
border=False
)
radius_chart_options.set_legend(
style=cop.elements.ChartTextStyle(
color='black'
)
)
radius_chart = cop.elements.Pie3DChart(
name='planet_radius_chart',
pies=(radius_series,),
options=radius_chart_options
)
data.add(radius_chart)
# Create printjob
printjob = cop.PrintJob(
data=data,
server=server,
template=cop.Resource.from_local_file(
'./examples/solar_system_example/pptx/solar_system_template.pptx'), # pptx
# template=cop.Resource.from_local_file(
# './examples/solar_system_example/docx/solar_system_template.docx'), # docx
)
printjob.execute().to_file('./examples/solar_system_example/pptx/output')
# printjob.execute().to_file('./examples/solar_system_example/docx/output')