def add_table(self, slide: Slide, table_data: Iterable[Iterable[any]], position: PPTXPosition = None, table_style: PPTXTableStyle = None, auto_merge: bool = False) -> Shape: """ Add a table shape with given table_data at position using table_style. table_data: outer iter -> rows, inner iter cols auto_merge: use 'merge_left' and 'merge_up' as entry to mark merging cells (not implemented jet) """ rows, cols = self._get_rows_cols(table_data) if position is None: position = self.default_position left, top = position.tuple() result = slide.shapes.add_table(rows, cols, left, top, width=Inches(cols), height=Inches(0.5 * rows)) table = result.table for ir, row in enumerate(table_data): for ic, entry in enumerate(row): table.cell(ir, ic).text = f"{entry}" if table_style: table_style.write_shape(result) if auto_merge: pass # todo: merge cells; replace text for merged cells with "" return result
def table_no_header() -> PPTXTableStyle: result = PPTXTableStyle() result.first_row_header = False result.row_banding = True result.col_banding = False return result
def create_measurements_meta_table(self, slide, table_style: PPTXTableStyle = None): table_data = get_measurements_meta_table_data(self.measurements) result = self.pptx_creator.add_table(slide, table_data) if table_style is None: table_style = table_style_measurements_meta() table_style.write_shape(result) return result
def create_measurement_meta_data_table( self, slide, measurement, table_style: PPTXTableStyle = None) -> Shape: table_data = get_measurement_meta_table_data(measurement) result = self.pptx_creator.add_table(slide, table_data, PPTXPosition(0.521, 0.16)) if table_style is None: table_style = style_sheets.table_no_header() table_style.set_width_as_fraction(0.4) table_style.write_shape(result) return result
def create_measurements_result_data_table( self, slide, table_style: PPTXTableStyle = None): for measurement in self.measurements: if measurement not in self.measurements_unloading_data.keys(): self._get_measurement_result_table_data( measurement, self.poisson_ratio, self.beta) # todo: better method (name change?) table_data = get_measurements_result_table_data( self.measurements_unloading_data.values()) result = self.pptx_creator.add_table(slide, table_data) if table_style is None: table_style = table_style_summary() table_style.write_shape(result) return result
def create_summary_pptx(self, filtered: bool = False, pptx_template=TemplateExample()): self.pptx = PPTXCreator(template=pptx_template) self.title_slide = self.pptx.add_title_slide(self.title) table_data = self.get_files_date_table_data() table_style = PPTXTableStyle() table_style.set_width_as_fraction(0.55) self.pptx.add_table(self.title_slide, table_data, PPTXPosition(0.0, 0.224, 0.1, 0.1), table_style) for container in self.gdf_containers: slide = self.pptx.add_slide(f"Overview - {container.basename}.gdf") if filtered: measurements = container.measurements else: measurements = container.filtered_measurements self.pptx.add_matplotlib_figure( self.create_summary_figure(measurements), slide, PPTXPosition(0, 0.115), zoom=0.62) table_style = summary_table() table_style.font_style.set(size=11) table_style.set_width_as_fraction(0.245) table_data = measurements[0].get_summary_table_data() table_data.append(["comment", measurements[0].comment]) table_shape = self.pptx.add_table(slide, table_data, PPTXPosition(0.75, 0.115), table_style) minimize_table_height(table_shape) return self.pptx
def table_invisible() -> PPTXTableStyle: result = PPTXTableStyle() result.cell_style = PPTXCellStyle() result.cell_style.fill_style.fill_type = FillType.NOFILL # todo: implement control for border lines return result
def run(save_dir: str): pp = PPTXCreator(TemplateExample()) slide_01 = pp.add_slide("Table style example 01 - slide 01") slide_02 = pp.add_slide("Table style example 01 - slide 02") slide_03 = pp.add_slide("Table style example 01 - slide 03") slide_04 = pp.add_slide("Table style example 01 - slide 04") slide_05 = pp.add_slide("Table style example 01 - slide 05") slide_06 = pp.add_slide("Table style example 01 - slide 06") # data for a table with 5 rows and 3 cols. table_data = [] table_data.append([1, "The second column is longer."]) # rows can have different length table_data.append([2, "Table entries don't have to be strings,"]) # there is specific type needed for entries (implemented as text=f"{entry}") table_data.append([3, "because its implemented as text=f'{entry}'"]) table_data.append([4, "also note: the number of entries per row is", " not fixed"]) table_data.append([5, "That's it for now."]) # We can add these data as a table title_slide with PPTXCreator.add_table()... table_01 = pp.add_table(slide_01, table_data) # ... but if you open the slide in PowerPoint there are a few issues: # 1) the table is positioned in the top left corner - overlapping the title # 2) the first row is formated differently (like a column header) # 3) all columns have the same width (1 inch) # 4) the table width is too small (for the used paragraph size) # Lets handle the the position first using optional PPTXPosition parameter table_02 = pp.add_table(slide_02, table_data, PPTXPosition(0.02, 0.14)) # for more control we use PPTXTableStyle table_style = PPTXTableStyle() table_style.first_row_header = False table_style.width = 6.1 # table width in inches table_style.col_ratios = [0.3, 5, 1.3] table_03 = pp.add_table(slide_03, table_data, PPTXPosition(0.02, 0.14), table_style) # It's also possible to add the position directly to the table style: table_style.position = PPTXPosition(0.02, 0.14) # or to set the table width as a fraction of slide width: table_style.set_width_as_fraction(0.49) # change row/col bending table_style.col_banding = True table_style.row_banding = False table_04 = pp.add_table(slide_04, table_data, table_style=table_style) # we could also add a paragraph-style and a cell-style table_style.font_style = PPTXFontStyle().set(italic=True, name="Arial", color_rgb=(100, 200, 30)) # todo: cell-style table_05 = pp.add_table(slide_05, table_data, table_style=table_style) # you could also use a table style on an existing table table_06 = pp.add_table(slide_06, table_data) table_style.write_shape(table_06) pp.save(os.path.join(save_dir, "table_style_example_01.pptx"), overwrite=True)