def create_pptx_for_nanoindents(path, pptx_filename, pptx_template: Optional[AbstractTemplate] = None): pptx = PPTXCreator(template=pptx_template) pptx.add_title_slide(f"AFM on Nanoindents - {path.stem}") measurements = load_pygdf_measurements(path) for measurement in measurements: indent_analyzer = GDEFIndentAnalyzer(measurement) print(measurement.comment) slide = pptx.add_slide(measurement.comment) figure = measurement.create_plot() if figure is None: continue pptx.add_matplotlib_figure(figure, slide, position_2x2_00()) table_shape = pptx.add_table(slide, measurement.get_summary_table_data(), position_2x2_01(), table_style=summary_table()) minimize_table_height(table_shape) # figure.savefig(f"{measurement.basename.with_suffix('.png')}") # , transparent=transparent) indent_analyzer.add_indent_pile_up_mask_to_axes(figure.axes[0], roughness_part=0.05) # figure.savefig(f"{measurement.basename.with_name(measurement.basename.stem + '_masked.png')}", dpi=96) pptx.add_matplotlib_figure(figure, slide, position_2x2_10()) table_shape = pptx.add_table(slide, indent_analyzer.get_summary_table_data(), position_2x2_11(), table_style=summary_table()) minimize_table_height(table_shape) figure.clear() pptx.save(path.joinpath(f"{pptx_filename}.pptx"), overwrite=True) # todo: remove overwrite=True when testing is finished
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)
def run(save_dir: str): pp = PPTXCreator(TemplateExample()) title_slide = pp.add_title_slide( "Paragraph style example 01 - title slide") text = "This text has three paragraphs. This is the first.\n" \ "Das ist der zweite ...\n" \ "... and the third." text_shape = pp.add_text_box(title_slide, text, PPTXPosition(0.02, 0.24)) text_shape.width = Inches(6) text_shape.height = Inches(2) paragraph_style = PPTXParagraphStyle() paragraph_style.alignment = PP_PARAGRAPH_ALIGNMENT.CENTER paragraph_style.write_paragraph(text_shape.text_frame.paragraphs[0]) paragraph_style.line_spacing = 3 paragraph_style.write_paragraph(text_shape.text_frame.paragraphs[1]) paragraph_style.level = 8 paragraph_style.write_paragraph(text_shape.text_frame.paragraphs[2]) filename_pptx = os.path.join(save_dir, "paragraph_style_example_01.pptx") pp.save(filename_pptx, create_pdf=True, overwrite=True)
def run(save_dir: str): pp = PPTXCreator(TemplateExample()) PPTXFontStyle.lanaguage_id = MSO_LANGUAGE_ID.ENGLISH_UK PPTXFontStyle.name = "Roboto" title_slide = pp.add_title_slide("General example 01 - title slide") font = font_title( ) # returns a PPTXFontStyle instance with bold paragraph and size = 32 Pt font.write_shape( title_slide.shapes.title ) # change paragraph attributes for all paragraphs in shape slide2 = pp.add_slide("General example 01 - page2") pp.add_slide("General example 01 - page3") pp.add_slide("General example 01 - page4") pp.add_content_slide() # add slide with hyperlinks to all other slides text = "This text has three paragraphs. This is the first.\n" \ "Das ist der zweite ...\n" \ "... and the third." my_font = font_default() my_font.size = 16 text_shape = pp.add_text_box(title_slide, text, PPTXPosition(0.02, 0.24), my_font) my_font.set(size=22, bold=True, language_id=MSO_LANGUAGE_ID.GERMAN, strikethrough=TEXT_STRIKE_VALUES.SingleStrike, caps=TEXT_CAPS_VALUES.All) my_font.write_paragraph(text_shape.text_frame.paragraphs[1]) my_font.set(size=18, bold=False, italic=True, name="Vivaldi", language_id=MSO_LANGUAGE_ID.ENGLISH_UK, underline=MSO_TEXT_UNDERLINE_TYPE.WAVY_DOUBLE_LINE, color_rgb=(255, 0, 0), strikethrough=None, caps=None) my_font.write_paragraph(text_shape.text_frame.paragraphs[2]) table_data = [] table_data.append([1, 2]) # rows can have different length table_data.append( [4, slide2, 6] ) # there is specific type needed for entries (implemented as text=f"{entry}") table_data.append(["", 8, 9]) table = pp.add_table(title_slide, table_data, PPTXPosition(0.02, 0.4)) paragraph_style = PPTXParagraphStyle() paragraph_style.set(alignment=PP_PARAGRAPH_ALIGNMENT.CENTER) paragraph_style.write_shape(table) if matplotlib_installed: fig = create_demo_figure() pp.add_matplotlib_figure(fig, title_slide, PPTXPosition(0.3, 0.4)) pp.add_matplotlib_figure(fig, title_slide, PPTXPosition(0.3, 0.4, fig.get_figwidth(), -1.0), zoom=0.4) pp.add_matplotlib_figure(fig, title_slide, PPTXPosition(0.3, 0.4, fig.get_figwidth(), 0.0), zoom=0.5) pp.add_matplotlib_figure(fig, title_slide, PPTXPosition(0.3, 0.4, fig.get_figwidth(), 1.5), zoom=0.6) pp.add_text_box(title_slide, "Use latex-like syntax \nto create formula:", PPTXPosition(0.748, 0.23)) pp.add_latex_formula(f"\mu={5}^{5}", title_slide, PPTXPosition(0.75, 0.35)) formula02 = "\\int_0^\\infty e^{-x^2} dx=\\frac{\\sqrt{\\pi}}{2}" pp.add_latex_formula(formula02, title_slide, PPTXPosition(0.75, 0.45)) pp.add_latex_formula(formula02, title_slide, PPTXPosition(0.75, 0.55), font_size=24, color="red") formula03 = "\\hat{x}, \\check{x}, \\tilde{a}, \\bar{\\ell}, \\dot{y}, \\ddot{y}, \\vec{z_1}, \\vec{z}_1" pp.add_latex_formula(formula03, title_slide, PPTXPosition(0.75, 0.65), font_size=24, color="blue") formula04 = r"\frac{3}{4} \binom{3}{4} \genfrac{}{}{0}{}{3}{4}" pp.add_latex_formula(formula04, title_slide, PPTXPosition(0.75, 0.75), font_size=44, color="g") pp.save(os.path.join(save_dir, "general_example_01.pptx")) try: # only on Windows with PowerPoint installed: filename_pptx = os.path.join(save_dir, "general_example_01.pptx") filename_pdf = os.path.join(save_dir, "general_example_01.pdf") foldername_png = os.path.join(save_dir, "general_example_01_pngs") # use absolute path, because its not clear where PowerPoint saves PDF/PNG ... otherwise pp.save(filename_pptx, create_pdf=True, overwrite=True) pp.save_as_pdf(filename_pdf, overwrite=True) pp.save_as_png(foldername_png, overwrite_folder=True) except Exception as e: print(e)
def run(save_dir: str): filename_pptx = os.path.join(save_dir, "font_style_example_01.pptx") pp = PPTXCreator(TemplateExample()) # default language and paragraph-type for all created PPTXFontStyle instances: PPTXFontStyle.lanaguage_id = MSO_LANGUAGE_ID.ENGLISH_UK PPTXFontStyle.name = "Roboto" title_slide = pp.add_title_slide("Font style example presentation") font = font_title( ) # returns a PPTXFontStyle instance with bold paragraph and size = 32 Pt font.write_shape( title_slide.shapes.title ) # change paragraph attributes for all paragraphs in shape text_01 = "This text has four paragraphs. This is the first.\n" \ "Das ist der zweite ...\n" \ "... the third ...\n" \ "... and the last." my_font = PPTXFontStyle() my_font.size = 16 text_shape_01 = pp.add_text_box(title_slide, text_01, PPTXPosition(0.02, 0.24), my_font) my_font.set(size=22, bold=True, language_id=MSO_LANGUAGE_ID.GERMAN, strikethrough=TEXT_STRIKE_VALUES.SingleStrike, caps=TEXT_CAPS_VALUES.All) my_font.write_paragraph(text_shape_01.text_frame.paragraphs[1]) my_font.set(size=18, bold=False, italic=True, name="Vivaldi", language_id=MSO_LANGUAGE_ID.ENGLISH_UK, underline=MSO_TEXT_UNDERLINE_TYPE.WAVY_DOUBLE_LINE, color_rgb=(255, 0, 0), strikethrough=None, caps=None) my_font.write_paragraph(text_shape_01.text_frame.paragraphs[2]) my_font = PPTXFontStyle() my_font.set(size=52, bold=True) my_fill = PPTXFillStyle() my_fill.fill_type = FillType.PATTERNED my_fill.fore_color_rgb = (255, 0, 0) my_fill.back_color_rgb = (0, 0, 255) from pptx.enum.dml import MSO_PATTERN_TYPE my_fill.pattern = MSO_PATTERN_TYPE.PERCENT_50 my_font.fill_style = my_fill my_font.write_paragraph(text_shape_01.text_frame.paragraphs[3]) text_02 = "This text uses copied paragraph." my_copied_font = PPTXFontStyle() my_copied_font.read_font(text_shape_01.text_frame.paragraphs[1].font) text_shape_02 = pp.add_text_box(title_slide, text_02, PPTXPosition(0.42, 0.24), my_copied_font) pp.save(filename_pptx, overwrite=True)