-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExcel_Write.py
More file actions
235 lines (186 loc) · 8.4 KB
/
Copy pathExcel_Write.py
File metadata and controls
235 lines (186 loc) · 8.4 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# Excel_Write.py - Write and format Excel output files
# This Python file uses the following encoding: utf-8
import os
import sys
import io
import datetime
from pathlib import Path
import csv
import datetime
from tkinter.tix import COLUMN
import pandas as pd
import xlsxwriter as xw
import Excel_Columns as ExCol
# -----------------------------------------------------------------------------
# Write to Excel
# -----------------------------------------------------------------------------
def to_excel(dataframe_groups, output_filename):
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.ExcelWriter.html
# https://xlsxwriter.readthedocs.io/
num_groups = len(dataframe_groups.keys())
curr_group_idx = 1
# Make list of columns to output and headers to use
col_output_list = []
col_header_list = []
# col_data_defs = ExCol.column_def[1:]
col_data_defs = ExCol.column_def
col_data_defs.popitem(last=False)
for col_fmt_key in col_data_defs:
col_output_list.append(col_data_defs[col_fmt_key]["df_name"])
col_header_list.append(col_data_defs[col_fmt_key]["col_text"])
# Get reference to XlsxWriter object
writer = pd.ExcelWriter(output_filename)
# Make the output dataframes and write them
# -----------------------------------------
# with pd.ExcelWriter(output_filename) as writer:
for group_key in dataframe_groups.keys():
datamark_sheet_name = "DM{}".format(group_key)
print(" Sheet '{}' ({} of {}) ...".format(datamark_sheet_name, curr_group_idx, num_groups))
# Make a blank column just in case it is needed
b_series = pd.Series(index= dataframe_groups[group_key].index, dtype='float64')
out_df = dataframe_groups[group_key]
# Step through the list of columns to output. Either find and copy
# an existing data column or copy the blank column at each column
# position.
df_columns = dataframe_groups[group_key].columns.values.tolist()
for output_column_name in col_output_list:
if output_column_name not in df_columns:
# print("Missing column - ", output_column_name)
# out_df = out_df.assign(output_column_name=b_series)
# out_df[output_column_name]=b_series.values
out_df = out_df.assign(**{output_column_name:b_series})
# Write the dataframe to the spreadsheet
# out_df.to_excel(writer, sheet_name=datamark_sheet_name,
# header=col_header_list, columns=col_output_list,
# index_label="msecSinceMidnite")
out_df.to_excel(writer, sheet_name=datamark_sheet_name,
header=False, columns=col_output_list,
index_label="msecSinceMidnite", startrow=5)
curr_group_idx += 1
# if curr_group_idx == 2:
# break;
# Do some worksheet formatting
# ----------------------------
# Note: once a cell format has been set it cannot be added to. You get to
# set cell format once.
# writer.sheets -> dict of sheet name strings
# worksheet -> one sheet name string
# dm_sheet -> xlsxwriter.worksheet.Worksheet
# writer.book -> xlsxwriter.workbook.Workbook
for worksheet in writer.sheets:
dm_sheet = writer.sheets[worksheet]
# Add default top row formats
sec_title_format = writer.book.add_format()
sec_title_format.set_bold()
hdr_row_format = writer.book.add_format()
hdr_row_format.set_bold()
hdr_row_format.set_text_wrap()
hdr_row_format.set_align('top')
hdr_row_format.set_align('center')
dm_sheet.set_row(4, None, hdr_row_format)
# Format each column header
col_idx = 1
for col_fmt_key in ExCol.column_def:
### Header row stuff
# Setup header cell format
hdr_cell_format = writer.book.add_format()
hdr_cell_format.set_bold()
hdr_cell_format.set_text_wrap()
hdr_cell_format.set_align('top')
hdr_cell_format.set_align('center')
# Add color
if ExCol.column_def[col_fmt_key]["col_color"] != "":
hdr_cell_format.set_bg_color(ExCol.column_def[col_fmt_key]["col_color"])
# Add border
if ExCol.column_def[col_fmt_key]["border"] == True:
hdr_cell_format.set_right(2)
# Write cell text and format
if ExCol.column_def[col_fmt_key]["sec_title"] != "":
dm_sheet.write_string(0, col_idx, ExCol.column_def[col_fmt_key]["sec_title"], sec_title_format)
dm_sheet.write_string(3, col_idx, ExCol.column_def[col_fmt_key]["df_name"])
dm_sheet.write_string(4, col_idx, ExCol.column_def[col_fmt_key]["col_text"], hdr_cell_format)
# Write cell comment
if ExCol.column_def[col_fmt_key]["equation"] != "":
dm_sheet.write_comment(3, col_idx, "= "+ExCol.column_def[col_fmt_key]["equation"], {'font_size': 12, 'width' : 500})
if ExCol.column_def[col_fmt_key]["comment"] != "":
dm_sheet.write_comment(4, col_idx, ExCol.column_def[col_fmt_key]["comment"], {'font_size': 12, 'width' : 300})
### Column stuff
col_format = writer.book.add_format()
# Hide columns
if ExCol.column_def[col_fmt_key]["col_hide"] == True:
col_options = {'hidden':True}
else:
col_options = {}
# Add column border
if ExCol.column_def[col_fmt_key]["border"] == True:
col_format.set_right(2)
if ExCol.column_def[col_fmt_key]["num_format"] != "":
col_format.set_num_format(ExCol.column_def[col_fmt_key]["num_format"])
dm_sheet.set_column(col_idx, col_idx, 12, col_format, col_options)
col_idx += 1
# Freeze the top row
dm_sheet.freeze_panes(5,0)
writer.close()
# =============================================================================
# Test the Excel writer
if __name__=='__main__':
test_filename = "Excel_Test.xlsx"
# Make a test dataframe group
data_array = [
{ "priTimeStamp" : 10000,
"priPFwd" : 1000,
"priP45" : 1000,
"priPalt" : 4000,
"priIAS" : 150,
"priAngleOfAttack" : 3,
"priDataMark" : 0,
"priTAS" : "=I2*(1+(H2/1000*0.017))"},
{ "priTimeStamp" : 10020,
"priPFwd" : 1005,
"priP45" : 1000,
"priPalt" : 4010,
"priIAS" : 160,
"priAngleOfAttack" : 3.1,
"priDataMark" : 0,
"priTAS" : "=I2*(1+(H2/1000*0.017))" },
{ "priTimeStamp" : 10040,
"priPFwd" : 1010,
"priP45" : 1000,
"priPalt" : 4100,
"priIAS" : 155,
"priAngleOfAttack" : 3,
"priDataMark" : 1,
"priTAS" : "=I2*(1+(H2/1000*0.017))" },
{ "priTimeStamp" : 10060,
"priPFwd" : 1015,
"priP45" : 1000,
"priPalt" : 4110,
"priIAS" : 165,
"priAngleOfAttack" : 3,
"priDataMark" : 1,
"priTAS" : "=I2*(1+(H2/1000*0.017))" },
]
# print(data_array)
index_time = [ 0, 20, 40, 80]
dataframe = pd.DataFrame(data_array, index_time)
# print(dataframe)
# Group the data by data mark
datamark_groups = dataframe.groupby("priDataMark")
# Make a numerically sorted list of data mark keys
dg_ikeys = []
for group_key in datamark_groups.indices.keys():
dg_ikeys.append(int(group_key))
dg_ikeys.sort()
# Make dataframes based on group start and stop times
dataframe_group = {}
for group_key in datamark_groups.groups.keys():
first_line = datamark_groups.groups[group_key][0] # - 2000
last_line = datamark_groups.groups[group_key][-1] # - 2000
dataframe_group[group_key] = dataframe.loc[first_line:last_line]
print(dataframe_group)
to_excel(dataframe_group, test_filename)
# workbook = xw.Workbook(test_filename)
#for sheet in workbook.worksheets():
# format_sheet(sheet)
# workbook.close()
print("Excel_Write done!")