""" * Version: 0.1.0.0.0 * Purposes: * Inspects the content in the PowerPoint and save them as Markdown files under a specified folder. * System parameters: * sys_parameter 1: The path to the single PowerPoint file as the input. * [deprecated] sys_parameter 2: The path to the new folder where the Markdown files are going to be stored as the output. * sys_parameter 2 (optional): Currently, the only supported mode is `silent`. """ from nis import match import sys, os, re from pptx import Presentation if __name__ == "__main__": path_to_ppt = sys.argv[1] # path_to_new_mds = sys.argv[2] mode = "standard" if len(sys.argv) > 2: mode = sys.argv[2] prs = Presentation(path_to_ppt) # Slide layouts if mode != "silent": layout_idx = 0 print("Available layouts in master:") for layout in prs.slide_layouts: print( "\t[" + str(layout_idx) + "]", layout.name, ", #Placeholder count in layout:", len(layout.placeholders), ) layout_idx += 1 slide_count = len(prs.slides) slide_idx = 0 # os.makedirs(path_to_new_mds) title_prefix = ["# "] bulletin_prefix = [ "+ ", " + ", " + ", " + ", " + ", ] # level 0, 1, 2, 3, 4 for slide in prs.slides: slide_idx += 1 file_name = None if slide_idx == 1: file_name = "01title.md" else: file_name = str(slide_idx).rjust(3, "0") + ".md" # md = open(path_to_new_mds + "/" + file_name, "x") shape_idx = 0 if mode != "silent": print("Slide", slide_idx) print("\tLayout:", slide.slide_layout.name) print("\tAvailable shapes:") for shape in slide.shapes: print( "\t\t", "[" + str(shape_idx) + "]", "[" + shape.name + "]", "[" + str(shape.shape_type).upper() + "]", ) shape_idx += 1 # shape_handling_seq = 0 # shape_text_content = [] # shape_picture_content = [] # shape_table_content = [] # shape_chart_content = [] # for shape in slide.shapes: # try: # print( # "shape type for shape[" + str(shape_handling_seq) + "]:", # shape.shape_type, # ) # if shape.has_text_frame: # for paragraph in shape.text_frame.paragraphs: # print("paragraph level:", paragraph.level) # if ( # "".join(paragraph.text) == None # or len("".join(paragraph.text)) == 0 # ): # # md.write("\n") # shape_text_content += ["\n"] # else: # if ( # ("TITLE" in shape.name.upper()) # or ( # str(shape.shape_type) # == "PLACEHOLDER (14)" # Google slide # and len(shape_text_content) == 0 # and paragraph.level == 0 # ) # or ( # str(shape.shape_type) # == "TEXT_BOX (17)" # Google slide # and len(shape_text_content) == 0 # and paragraph.level == 0 # ) # ): # # md.write( # # title_prefix[paragraph.level] # # + "".join(paragraph.text) # # + "\n" # # ) # shape_text_content += [ # title_prefix[paragraph.level] # + "".join(paragraph.text) # + "\n" # ] # elif ( # ("TEXT" in shape.name.upper()) # or ( # "PLACEHOLDER" # in str(shape.shape_type).upper() # Google slide # and len(shape_text_content) > 0 # ) # or ( # "TEXT_BOX" # in str(shape.shape_type).upper() # Google slide # and len(shape_text_content) > 0 # ) # ): # # md.write( # # bulletin_prefix[paragraph.level] # # + "".join(paragraph.text) # # + "\n" # # ) # shape_text_content += [ # bulletin_prefix[paragraph.level] # + "".join(paragraph.text) # + "\n" # ] # else: # # md.write("".join(paragraph.text) + "\n") # shape_text_content += ["".join(paragraph.text) + "\n"] # elif re.match("(PICTURE|图像)", str(shape.shape_type).upper()): # # md.write("![`smart`](place-holder.png)" + "\n\n") # shape_picture_content += ["![`smart`](place-holder.png)" + "\n\n"] # elif "TABLE" in str(shape.shape_type).upper(): # # md.write("" + "\n") # shape_table_content += ["" + "\n"] # elif "CHART" in str(shape.shape_type).upper(): # # md.write("" + "\n") # shape_chart_content += ["" + "\n"] # else: # print("\t\tCannot handle this shape_type:", shape.shape_type) # except AttributeError as ex: # AttributeError: 'Picture' object has no attribute 'text_frame' # print("cmd_inspect_ppt from", shape.name.upper(), ex) # shape_handling_seq += 1 # for line in shape_text_content: # md.write(line) # for line in shape_table_content: # md.write(line) # for line in shape_chart_content: # md.write(line) # for line in shape_picture_content: # md.write(line) # md.write("-" * 60 + "\n") # if slide.has_notes_slide: # notes_slide = slide.notes_slide # text_frame = notes_slide.notes_text_frame # md.write(text_frame.text + "\n") # md.close() # if mode != "silent": # print("cmd_inspect_ppt.py: Done.")