Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pywebio.input import file_upload, textarea
|
| 2 |
+
from pywebio.output import put_file, put_text
|
| 3 |
+
from pywebio.platform.flask import webio_view
|
| 4 |
+
from pywebio import start_server
|
| 5 |
+
from flask import Flask, redirect
|
| 6 |
+
import markdown
|
| 7 |
+
from pptx import Presentation
|
| 8 |
+
from pptx.util import Inches
|
| 9 |
+
import io
|
| 10 |
+
|
| 11 |
+
app = Flask(__name__)
|
| 12 |
+
|
| 13 |
+
def markdown_to_pptx(md_text):
|
| 14 |
+
prs = Presentation()
|
| 15 |
+
lines = md_text.split('\n')
|
| 16 |
+
current_slide = None
|
| 17 |
+
bullet_level = 0
|
| 18 |
+
|
| 19 |
+
for line in lines:
|
| 20 |
+
stripped_line = line.strip()
|
| 21 |
+
|
| 22 |
+
if stripped_line.startswith('#'):
|
| 23 |
+
level = len(stripped_line) - len(stripped_line.lstrip('#'))
|
| 24 |
+
title = stripped_line.lstrip('#').strip()
|
| 25 |
+
|
| 26 |
+
if level == 1:
|
| 27 |
+
if current_slide:
|
| 28 |
+
prs.slides.append(current_slide)
|
| 29 |
+
current_slide = prs.slides.add_slide(prs.slide_layouts[0])
|
| 30 |
+
title_shape = current_slide.shapes.title
|
| 31 |
+
title_shape.text = title
|
| 32 |
+
bullet_level = 0
|
| 33 |
+
elif level == 2:
|
| 34 |
+
if not current_slide:
|
| 35 |
+
current_slide = prs.slides.add_slide(prs.slide_layouts[1])
|
| 36 |
+
title_shape = current_slide.shapes.title
|
| 37 |
+
title_shape.text = title
|
| 38 |
+
else:
|
| 39 |
+
bullet_level = 1
|
| 40 |
+
text_frame = current_slide.shapes.placeholders[1].text_frame
|
| 41 |
+
p = text_frame.add_paragraph()
|
| 42 |
+
p.text = title
|
| 43 |
+
p.level = bullet_level - 1
|
| 44 |
+
else:
|
| 45 |
+
if not current_slide:
|
| 46 |
+
current_slide = prs.slides.add_slide(prs.slide_layouts[1])
|
| 47 |
+
title_shape = current_slide.shapes.title
|
| 48 |
+
title_shape.text = title
|
| 49 |
+
else:
|
| 50 |
+
bullet_level = level - 1
|
| 51 |
+
text_frame = current_slide.shapes.placeholders[1].text_frame
|
| 52 |
+
p = text_frame.add_paragraph()
|
| 53 |
+
p.text = title
|
| 54 |
+
p.level = bullet_level - 1
|
| 55 |
+
elif stripped_line.startswith('*') or stripped_line.startswith('-'):
|
| 56 |
+
if not current_slide:
|
| 57 |
+
current_slide = prs.slides.add_slide(prs.slide_layouts[1])
|
| 58 |
+
title_shape = current_slide.shapes.title
|
| 59 |
+
title_shape.text = "Untitled Slide"
|
| 60 |
+
text_frame = current_slide.shapes.placeholders[1].text_frame
|
| 61 |
+
p = text_frame.add_paragraph()
|
| 62 |
+
p.text = stripped_line.lstrip('*-').strip()
|
| 63 |
+
p.level = bullet_level
|
| 64 |
+
elif stripped_line:
|
| 65 |
+
if not current_slide:
|
| 66 |
+
current_slide = prs.slides.add_slide(prs.slide_layouts[1])
|
| 67 |
+
title_shape = current_slide.shapes.title
|
| 68 |
+
title_shape.text = "Untitled Slide"
|
| 69 |
+
text_frame = current_slide.shapes.placeholders[1].text_frame
|
| 70 |
+
p = text_frame.add_paragraph()
|
| 71 |
+
p.text = stripped_line
|
| 72 |
+
p.level = bullet_level
|
| 73 |
+
|
| 74 |
+
if current_slide:
|
| 75 |
+
prs.slides.append(current_slide)
|
| 76 |
+
|
| 77 |
+
output = io.BytesIO()
|
| 78 |
+
prs.save(output)
|
| 79 |
+
output.seek(0)
|
| 80 |
+
return output
|
| 81 |
+
|
| 82 |
+
def pdf_compressor():
|
| 83 |
+
put_text("Upload a Markdown file or paste Markdown text to convert to PowerPoint:")
|
| 84 |
+
file = file_upload("Upload Markdown File", accept=".md")
|
| 85 |
+
md_text = textarea("Paste Markdown Text", placeholder="Enter or paste your markdown text here", help_text="You can paste your markdown text here")
|
| 86 |
+
|
| 87 |
+
if file:
|
| 88 |
+
md_text = file['content'].decode('utf-8')
|
| 89 |
+
|
| 90 |
+
if md_text:
|
| 91 |
+
pptx_output = markdown_to_pptx(md_text)
|
| 92 |
+
put_file({
|
| 93 |
+
"filename": "output.pptx",
|
| 94 |
+
"content": pptx_output,
|
| 95 |
+
"type": "application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
| 96 |
+
})
|
| 97 |
+
else:
|
| 98 |
+
put_text("No markdown content provided.")
|
| 99 |
+
|
| 100 |
+
if __name__ == '__main__':
|
| 101 |
+
start_server(pdf_compressor, host='0.0.0.0', port=7860, debug=True)
|