-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_example.py
More file actions
61 lines (50 loc) · 1.97 KB
/
generate_example.py
File metadata and controls
61 lines (50 loc) · 1.97 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
"""Generate an example Excel file to verify the library works end-to-end."""
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.settings")
import django
django.setup()
from django_export_excel.table import Table, Column, TableMeta
from django_export_excel.exporter import ModelExcelExporter
from django_export_excel.styles import Style
from django_export_excel import colors
from tests.models import Person
class PersonTable(Table):
first_name = Column(header_name="First Name", width=25)
last_name = Column(header_name="Last Name", width=25)
email = Column(header_name="Email Address", width=40)
age = Column(header_name="Age", width=15)
full = Column(header_name="Full Name", width=35, dehydrate=True)
class Meta(TableMeta):
model = Person
columns = ["first_name", "last_name", "email", "age", "full"]
header_style = Style(
bold=True,
font_size=12,
height=40,
background_color=colors.SEA_GREEN,
font_color=colors.WHITE,
)
row_style = Style(
bold=False,
font_size=11,
height=25,
)
none_text = "-"
def get_full(self, obj):
return f"{obj.first_name} {obj.last_name}"
class PersonExporter(ModelExcelExporter):
table_class = PersonTable
file_name = "example_output"
sheet_name = "People"
include_row_number = True
people = [
Person(first_name="Alice", last_name="Johnson", email="alice@example.com", age=30),
Person(first_name="Bob", last_name="Smith", email="bob@example.com", age=25),
Person(first_name="Charlie", last_name="Brown", email="charlie@example.com", age=None),
Person(first_name="Diana", last_name="Prince", email="diana@example.com", age=35),
Person(first_name="Eve", last_name="Adams", email="eve@example.com", age=28),
]
exporter = PersonExporter()
exporter.generate(people)
exporter.save("example_output.xls")
print("Generated: example_output.xls")