diff --git a/bpeng/reports/cbra_diag.py b/bpeng/reports/cbra_diag.py index c0c929931a3f369653151ef3d1a7f4eb1db519d6..6d4c3412abbedbd652925e4e9d1610d8ea4a793c 100644 --- a/bpeng/reports/cbra_diag.py +++ b/bpeng/reports/cbra_diag.py @@ -5,12 +5,16 @@ from datetime import datetime +import os import pandas as pd from pptx import Presentation from pptx.chart.data import ChartData from pptx.dml.color import RGBColor -from pptx.enum.chart import (XL_CHART_TYPE, XL_LABEL_POSITION, - XL_LEGEND_POSITION, XL_TICK_LABEL_POSITION) +from pptx.enum.chart import ( + XL_CHART_TYPE, + XL_LEGEND_POSITION, + XL_TICK_LABEL_POSITION +) from pptx.enum.text import MSO_AUTO_SIZE, PP_ALIGN from pptx.util import Inches, Pt @@ -39,11 +43,11 @@ class CbraDiagnostic: input_values = sheet_input[1] # Define presentation to edit as the finished template - prs = Presentation(template_file) + diag_rep = Presentation(template_file) # Slide 1: cover slide - # cover_slide_layout = prs.slide_layouts[0] - cover_slide = prs.slides[0] + # cover_slide_layout = diag_rep.slide_layouts[0] + cover_slide = diag_rep.slides[0] cover_title = cover_slide.shapes.title cover_subtitle = cover_slide.placeholders[1] @@ -57,7 +61,7 @@ class CbraDiagnostic: # Slide 2: Executive Summary + Table of Contents # shapes[0] = slide #, 1 = executive summary text box, 2 = title , 3 = table of contents - exec_summary_slide = prs.slides[1] + exec_summary_slide = diag_rep.slides[1] exec_results_box = exec_summary_slide.shapes[1] exec_results_box.text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT exec_results_box.text_frame.alignment = PP_ALIGN.LEFT @@ -131,7 +135,7 @@ class CbraDiagnostic: # Slide 3: Energy Analysis, includes heatload pie chart + image from # BlocMaps (might be static) + efficiency % from BlocMaps - heat_load_slide = prs.slides[2] + heat_load_slide = diag_rep.slides[2] energy_analyze_box = heat_load_slide.shapes[3] # BlocMaps Efficiency Comparison @@ -173,7 +177,7 @@ class CbraDiagnostic: # pre+post-retrofit, ECM table w/ names, savings for elec and gas, and total savings # Slide 4: Utility breakdown (chart is in bottom right corner) - utility_projection_slide = prs.slides[3] + utility_projection_slide = diag_rep.slides[3] # Define stacked bar chart data util_cat = ["Existing", "Projected"] @@ -183,7 +187,6 @@ class CbraDiagnostic: util_break_plug = sheet_input.loc[16:17, 4] util_break_h2o = sheet_input.loc[16:17, 5] - util_break_data = ChartData() util_break_data.categories = util_cat util_break_data.add_series("Space Heating", util_break_sh) @@ -195,10 +198,14 @@ class CbraDiagnostic: # Set chart location and formatting chart_horz_pos, chart_vert_pos = Inches(0.05), Inches(2.4) chart_width, chart_height = Inches(3.5), Inches(2.5) - util_break_chart = utility_projection_slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_STACKED, - chart_horz_pos, chart_vert_pos, - chart_width, chart_height, - util_break_data).chart + util_break_chart = utility_projection_slide.shapes.add_chart( + XL_CHART_TYPE.COLUMN_STACKED, + chart_horz_pos, + chart_vert_pos, + chart_width, + chart_height, + util_break_data + ).chart # Formatting for chart # Legend @@ -225,9 +232,14 @@ class CbraDiagnostic: # Create ECM summary table ecm_count = input_values.str.contains('Y').sum() - ecm_table = utility_projection_slide.shapes.add_table(ecm_count+2, 3, Inches(3.81), - Inches(0.25), Inches(5.83), - Inches(3.76)) + ecm_table = utility_projection_slide.shapes.add_table( + ecm_count + 2, + 3, + Inches(3.81), + Inches(0.25), + Inches(5.83), + Inches(3.76) + ) ecm_table.last_row = True ecm_table.horz_banding = False ecm_table_head = ['ECM', "Heating Savings *", "Electricity Savings *"] @@ -259,11 +271,10 @@ class CbraDiagnostic: for j in range(len(input_values)): if input_values[j] == 'N': slide_idx = int(((j-6)/3) - counter) - prs.slides.delete_slide(prs, slide_idx) + diag_rep.slides.delete_slide(diag_rep, slide_idx) counter = counter + 1 - - return project_address, prs + return project_address, diag_rep @staticmethod def generate_report(): @@ -274,5 +285,32 @@ class CbraDiagnostic: file_name = input('Enter File Name (with .xlsx extension): ') file_input = pd.ExcelFile(file_name) sheet_input = file_input.parse("Inputs", header=None) - project_address, prs = CbraDiagnostic._generate_report(template_file, sheet_input) - prs.save('EEDR {}.pptx'.format(project_address)) + project_address, diag_rep = CbraDiagnostic._generate_report(template_file, sheet_input) + diag_rep.save('EEDR {}.pptx'.format(project_address)) + + @staticmethod + def cbra_multi_rep(): + """ + + Generate report for every excel file in a given directory + """ + target_dir = input('Enter directory where pptx template is located: ') + cbra_pptx = os.path.join(target_dir, "EEDR_CBRA_Template_Final.pptx") + + if os.path.exists(cbra_pptx): + repgen_list = [] + + for xlsx_item in [xl for xl in os.listdir(os.getcwd()) if xl.endswith(".xlsx")]: + + repgen_list.append(xlsx_item) + + for i in range(len(repgen_list)): + template_file = cbra_pptx + file_name = repgen_list[i] + file_input = pd.ExcelFile(file_name) + sheet_input = file_input.parse("Inputs", header=None) + proj_loc, diag_rep = CbraDiagnostic._generate_report(template_file, sheet_input) + diag_rep.save('EEDR {}.pptx'.format(proj_loc)) + print('Reports generated!') + else: + raise Exception('Template not found. Please download pptx template to directory')