def make_report(self): print('Building report...') doc = docxtpl.DocxTemplate(density_report_template) styles = doc.styles date_now = datetime.today().strftime('%c') basic_info_table = self.make_table_doco( self.information_table.round(3), 'Summary') histo_inline = docxtpl.InlineImage(doc, self.histo_out) density_im_inline = docxtpl.InlineImage(doc, self.image_path, height=Mm(180)) context = { 'br_code': self.br_code, 'area_name': self.area_name, 'date_gen': date_now, 'cov_w_aoi': density_im_inline, 'histo': histo_inline, 'dens_table': basic_info_table, 'user': self.user, 'method': self.method, 'cell_count': self.compliant_cells, 'cell_sq': self.cell_size * self.cell_size } doc.render(context) doc.save(self.report_path) print('Wrote %s...' % os.path.basename(self.report_path))
def make_cover_page(self): doc = docxtpl.DocxTemplate(front_page) date_now = datetime.today().strftime('%c') new_plot_name_x = os.path.join(self.analysis_folder, 'Displacement_AFTER_X.png') old_plot_name_x = os.path.join(self.analysis_folder, 'Displacement_BEFORE_X.png') new_plot_name_y = os.path.join(self.analysis_folder, 'Displacement_AFTER_Y.png') old_plot_name_y = os.path.join(self.analysis_folder, 'Displacement_BEFORE_Y.png') cover_path = os.path.join( self.reprots_folder, '%s_%s_Front_Page.docx' % (self.br, self.area_name)) building_1_inline = docxtpl.InlineImage(doc, self.image_list[0], width=Mm(70)) old_x_inline = docxtpl.InlineImage(doc, old_plot_name_x, width=Mm(70)) old_y_inline = docxtpl.InlineImage(doc, old_plot_name_y, width=Mm(70)) new_y_inline = docxtpl.InlineImage(doc, new_plot_name_y, width=Mm(70)) new_x_inline = docxtpl.InlineImage(doc, new_plot_name_x, width=Mm(70)) self.about_area_pd.infer_objects().style.set_precision(2) self.adj_summary_pd.infer_objects().style.set_precision(2) self.about_area_pd.columns = ['Variable', 'Value'] ov_table = self.build_doc(self.about_area_pd, front_page) of_table = self.build_doc(self.adj_summary_pd, front_page) assessment = self.determine_assessment() context = { 'BR': self.br, 'area_name': self.area_name, 'building_1': building_1_inline, 'overview_table': ov_table, 'offset_table': of_table, 'x_before': old_x_inline, 'y_before': old_y_inline, 'x_after': new_x_inline, 'y_after': new_y_inline, 'RMSE_X_before': self.initial_x_rmse, 'RMSE_X_after': self.new_x_rmse, 'RMSE_Y_before': self.initial_y_rmse, 'RMSE_Y_after': self.new_y_rmse, 'CI95_X_before': self.initial_x_ci95, 'CI95_X_after': self.new_x_ci95, 'CI95_Y_before': self.initial_y_ci95, 'CI95_Y_after': self.new_y_ci95, 'assessment': assessment, 'gen': date_now } doc.render(context) doc.save(cover_path)
def fp_to_docx(fp_object, template, out_folder, doc_name): from docx.shared import Mm, Inches from os.path import join doc_template = docxtpl.DocxTemplate(template) in_vars = doc_template.get_undeclared_template_variables() dict_vars = {} for var in in_vars: try: if fp_object.fm_main[var]['domain_trans']: try: dict_vars[var] = fp_object.fm_main[var]['domain_trans'][ fp_object.attributes[var]] except KeyError: dict_vars[var] = fp_object.attributes[var] else: dict_vars[var] = fp_object.attributes[var] except KeyError: if 'att' in var: print('\tattachment {}'.format(var)) if 'att20_' in var: att_url = find_attachment(fp_object.att_res, var.replace('att20_', '')) if att_url == None: pass else: dict_vars[var] = docxtpl.InlineImage( doc_template, set_up_docx_inline(att_url), height=Mm(20)) else: att_url = find_attachment(fp_object.att_res, var.replace('att_', '')) if att_url == None: pass else: dict_vars[var] = docxtpl.InlineImage( doc_template, set_up_docx_inline(att_url), width=Inches(6)) elif 'rel_' in var: related_set = find_related_table(fp_object, var.replace('rel_', '')) if len(related_set.features) > 0: dict_vars[var] = related_set.return_sdf().to_dict( 'records') else: print('\tnon-attribute: {}'.format(var)) doc_template.render(dict_vars) if '.docx' not in doc_name: doc_name = doc_name + '.docx' print('\tsaving...') return doc_template.save(join(out_folder, doc_name))
def add_all_img(context, dict_res_df, sh_prop): # Подготавливаю картинку showlist = {} for bgs_id, _ in dict_res_df.items(): print(f'Show BGS={bgs_id}') res_df = dict_res_df[bgs_id]['res_df'] print(f'res_df.columns = {res_df.columns}') res_df['newtime'] = res_df['newtime'] + pd.Timedelta(hours=3) res_df.set_index('newtime', drop=False, inplace=True) res_df = res_df.sort_index() show = sh.Show(len(sh_prop), start, int((finish - start) / datetime.timedelta(minutes=1)), figsize=[11, 6.5], button_show=False) # +pd.Timedelta(hours=3) showlist[bgs_id] = show show.fig.suptitle(f'БГС №{bgs_id}', fontsize=12) for key, prop in sh_prop.items(): for name, col_name in prop.items(): show.add_plot(key, res_df['newtime'], res_df[col_name], label=name) imgs = {} for bgs_id, _ in showlist.items(): showlist[bgs_id].change_plot(0.1, legend=True) showlist[bgs_id].savefig('imgtmp_' + str(bgs_id) + '.png') # , dpi = 200 context['imgall_' + str(bgs_id)] = docxtpl.InlineImage( doc, 'imgtmp_' + str(bgs_id) + '.png') plt.close('all') return context
def make_sub_page(self, pd_in, building_name, old_data, new_data): page_number = old_data['Building ID'].head(1).values[0] + 2 doc = docxtpl.DocxTemplate(sub_page) date_now = datetime.today().strftime('%c') page_path = os.path.join( self.reprots_folder, '%s_%s_%s_Page.docx' % (self.br, self.area_name, building_name)) # make histos x_before_histo_path = os.path.join( self.analysis_folder, 'Displacement_%s_X_BEFORE.png' % building_name) y_before_histo_path = os.path.join( self.analysis_folder, 'Displacement_%s_Y_BEFORE.png' % building_name) y_after_histo_path = os.path.join( self.analysis_folder, 'Displacement_%s_Y_AFTER.png' % building_name) x_after_histo_path = os.path.join( self.analysis_folder, 'Displacement_%s_X_AFTER.png' % building_name) x_old_series = old_data['Offset_X'] y_old_series = old_data['Offset_Y'] y_new_series = new_data['Offset_Y'] x_new_series = new_data['Offset_X'] self.make_fuzziness_histo(x_old_series, x_before_histo_path) self.make_fuzziness_histo(y_old_series, y_before_histo_path) self.make_fuzziness_histo(y_new_series, y_after_histo_path) self.make_fuzziness_histo(x_new_series, x_after_histo_path) pd_in_reset_index = pd_in.reset_index().infer_objects() pd_in_reset_index.style.set_precision(precision=2) table_in = self.build_doc(pd_in_reset_index, sub_page) # find ss image longer_name = building_name + '.' candidates = [a for a in self.image_list if building_name in a] if len(candidates) == 1: building_image = candidates[0] elif len(candidates) > 1: candidates = [a for a in self.image_list if longer_name in a] building_image = candidates[0] else: building_image = None if building_image is not None: inline_image = docxtpl.InlineImage(doc, building_image, width=Mm(100)) else: inline_image = "No image provided" x_before_histo_inline = docxtpl.InlineImage(doc, x_before_histo_path, width=Mm(70)) y_before_histo_inline = docxtpl.InlineImage(doc, y_before_histo_path, width=Mm(70)) y_after_histo_inline = docxtpl.InlineImage(doc, y_after_histo_path, width=Mm(70)) x_after_histo_inline = docxtpl.InlineImage(doc, y_before_histo_path, width=Mm(70)) context = { 'BR': self.br, 'area_name': self.area_name, 'bnum': building_name, 'building_image': inline_image, 'building_adj_summary': table_in, 'x_histo_before': x_before_histo_inline, 'x_histo_after': x_after_histo_inline, 'y_histo_before': y_before_histo_inline, 'y_histo_after': y_after_histo_inline, 'RMSE_X_before': round(pd_in.loc['X RMSE (m)', 'Before adjustment'], 2), 'RMSE_X_after': round(pd_in.loc['X RMSE (m)', 'After adjustment'], 2), 'RMSE_Y_after': round(pd_in.loc['Y RMSE (m)', 'After adjustment'], 2), 'RMSE_Y_before': round(pd_in.loc['Y RMSE (m)', 'Before adjustment'], 2), 'CI95_Y_before': round(pd_in.loc['Y CI95', 'Before adjustment'], 2), 'CI95_X_before': round(pd_in.loc['X CI95', 'Before adjustment'], 2), 'CI95_X_after': round(pd_in.loc['X CI95', 'After adjustment'], 2), 'CI95_Y_after': round(pd_in.loc['Y CI95', 'After adjustment'], 2), 'gen': date_now, 'bid': page_number } doc.render(context) doc.save(page_path)
df_tmp = df[df['FIRC841'] > 1.] for param, prop in parameter_properties.items(): context[param + '_' + str(bgs_id) + '_mean'] = round( df_tmp[param].mean(), 2) context[param + '_' + str(bgs_id) + '_std'] = round( df_tmp[param].std(), 2) if prop['show']: fig, ax = plt.subplots(1, figsize=[11, 2]) fig.subplots_adjust(left=0.04, right=0.98, top=0.95, bottom=0.12) ax.plot(df.index, df[param], label=param) if 'adv_show' in prop.keys(): ax.plot(df.index, df[prop['adv_show']], label=prop['adv_show']) ax.grid() ax.legend() plt.pause(0.1) namefltmp = os.getcwd( ) + '\\tmp\\img_' + param + '_' + str(bgs_id) + '.png' fig.savefig(namefltmp) #str(bgs_id)+ plt.close(fig) context['img_' + param + '_' + str(bgs_id)] = docxtpl.InlineImage(doc, namefltmp) print(f'context = {context}') context = add_all_img(context, dict_res_df, sh_prop) render_doc(doc, context, otchet_file_name)