-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexamples.py
101 lines (68 loc) · 2.76 KB
/
examples.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 30 15:37:48 2016
@author: mkonrad
"""
import numpy as np
import pandas as pd
from pandas_excel_styler import DataFrameExcelStyler, create_style_for_validations
#%% Create some random data
np.random.seed()
col1 = np.random.random(20)
col2 = np.random.randint(0, 11, 20)
col3 = np.random.choice(list('abcf'), 20)
# create a data frame
df = pd.DataFrame.from_items([('one', col1), ('two', col2), ('three', col3)])
# wrap it as DataFrameExcelStyler
# this will not copy the data from the original data frame (unless you set copy=True)!
df = DataFrameExcelStyler(df)
# of course you could also fill a DataFrameExcelStyler directly
#df = DataFrameExcelStyler.from_items([('one', col1), ('two', col2), ('three', col3)])
### define some styles
# for the colors, see https://github.com/python-excel/tutorial/raw/master/python-excel.pdf page 33
# "Colours in Excel files are a confusing mess"
bold_style = {"font": {"bold": True}}
red_font_style = {"font": {"color": "red"}}
red_bg_style = {"pattern": {"pattern": "solid_fill", "fore_color": "red"}}
orange_bg_style = {"pattern": {"pattern": "solid_fill", "fore_color": "orange"}}
### Example 1 ###
# create a cell_styles matrix
# it must have the same number of rows and columns as your DataFrame
cell_styles = np.empty((df.shape[0], df.shape[1]), dtype='object')
cell_styles.fill(None) # filling it with None means that no styling is applied to any cell
# set some styles
cell_styles[0, 0] = bold_style
cell_styles[1, 1] = red_font_style
cell_styles[2, 2] = red_bg_style
print(cell_styles)
# output as example output
f_out = 'example_output/example1.xls'
print("writing output to file", f_out)
df.to_excel(f_out, cell_styles=cell_styles) # uses xlwt, works
#df.to_excel('example_output/test.xlsx', cell_styles=cell_styles) # uses openpyxl, doesn't work
#%%
### Example 2 ###
# Conditional coloring
cell_styles = np.empty(df.shape, dtype='object')
cell_styles.fill(None)
# all values in column "one" below 0.25 will get a red background
# values between 0.25 and 0.5 will get an orange background
col_idx = 0
cell_styles[df.one.values < 0.25, col_idx] = red_bg_style
cell_styles[(df.one.values >= 0.25) & (df.one.values < 0.5), col_idx] = orange_bg_style
print(cell_styles)
f_out = 'example_output/example2.xls'
print("writing output to file", f_out)
df.to_excel(f_out, cell_styles=cell_styles)
#%%
### Test 3 ###
# style from validation columns
# set some boolean validation columns
df['one_valid'] = df['one'] >= 0.5
df['three_valid'] = df['three'].isin(list('abc'))
# create the styles
cell_styles = create_style_for_validations(df, remove_validation_cols=True)
print(cell_styles)
f_out = 'example_output/example3.xls'
print("writing output to file", f_out)
df.to_excel(f_out, cell_styles=cell_styles)