Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pptx import Presentation
|
| 2 |
+
from pptx.util import Pt
|
| 3 |
+
from pptx.shapes.group import GroupShape
|
| 4 |
+
from pptx.dml.color import RGBColor
|
| 5 |
+
from pptx.enum.text import PP_ALIGN
|
| 6 |
+
import pptx_ea_font
|
| 7 |
+
from pptx.shapes.picture import Picture
|
| 8 |
+
from pptx.util import Inches
|
| 9 |
+
|
| 10 |
+
import json
|
| 11 |
+
import os
|
| 12 |
+
from PIL import Image
|
| 13 |
+
import io
|
| 14 |
+
|
| 15 |
+
def print_json(item):
|
| 16 |
+
item_json = json.dumps(item, ensure_ascii=False, indent=4)
|
| 17 |
+
print(item_json)
|
| 18 |
+
|
| 19 |
+
def transfer_textbox_content_in_group(group_shape):
|
| 20 |
+
"""Edit the content of text boxes within a group shape."""
|
| 21 |
+
group_shape_item = {}
|
| 22 |
+
for l, shape in enumerate(group_shape.shapes):
|
| 23 |
+
shape_item = {}
|
| 24 |
+
if shape.has_text_frame:
|
| 25 |
+
shape_item['type'] = "text"
|
| 26 |
+
shape_item['location'] = (shape.left, shape.top)
|
| 27 |
+
text_frame = shape.text_frame
|
| 28 |
+
for r, paragraph in enumerate(text_frame.paragraphs):
|
| 29 |
+
original_run = paragraph.runs[0]
|
| 30 |
+
paragraph_item = {}
|
| 31 |
+
paragraph_item['text'] = paragraph.text
|
| 32 |
+
# paragraph_item['location'] = paragraph.left
|
| 33 |
+
paragraph_item['align'] = paragraph.alignment
|
| 34 |
+
font_item = {}
|
| 35 |
+
font_item['name'] = original_run.font.name
|
| 36 |
+
font_item['bold'] = original_run.font.bold
|
| 37 |
+
font_item['italic'] = original_run.font.italic
|
| 38 |
+
font_item['underline'] = original_run.font.underline
|
| 39 |
+
font_item['color'] = original_run.font.color.rgb
|
| 40 |
+
font_item['language_id'] = original_run.font.language_id
|
| 41 |
+
paragraph_item['font'] = font_item
|
| 42 |
+
shape_item[f'paragraph_{r}'] = paragraph_item
|
| 43 |
+
group_shape_item[f"shape_{l}"] = shape_item
|
| 44 |
+
return group_shape_item
|
| 45 |
+
|
| 46 |
+
def transfer_to_structure(prs, images_dir_path):
|
| 47 |
+
item = {}
|
| 48 |
+
|
| 49 |
+
# Iterate through each slide in the presentation
|
| 50 |
+
for i, slide in enumerate(prs.slides):
|
| 51 |
+
# Iterate through each shape in the slide
|
| 52 |
+
slide_item = {}
|
| 53 |
+
for j, shape in enumerate(slide.shapes):
|
| 54 |
+
# If the shape is a group, process its shapes
|
| 55 |
+
shape_item = {}
|
| 56 |
+
if shape.has_text_frame:
|
| 57 |
+
text_frame = shape.text_frame
|
| 58 |
+
for paragraph in text_frame.paragraphs:
|
| 59 |
+
# Clear the existing text but keep the paragraph
|
| 60 |
+
paragraph.clear()
|
| 61 |
+
# Add a new run with the new content and copy font style
|
| 62 |
+
run = paragraph.add_run()
|
| 63 |
+
run.text = new_content
|
| 64 |
+
if paragraph.runs:
|
| 65 |
+
original_run = paragraph.runs[0]
|
| 66 |
+
copy_font_style(original_run, run)
|
| 67 |
+
elif isinstance(shape, GroupShape):
|
| 68 |
+
shape_item['type'] = "group"
|
| 69 |
+
group_shape_item = transfer_textbox_content_in_group(shape)
|
| 70 |
+
shape_item['group_content'] = group_shape_item
|
| 71 |
+
pass
|
| 72 |
+
elif isinstance(shape, Picture):
|
| 73 |
+
shape_item['type'] = "picture"
|
| 74 |
+
image_path = os.path.join(images_dir_path, f"picture_{j}.png")
|
| 75 |
+
shape_item['image_path'] = image_path
|
| 76 |
+
shape_item['size'] = shape.image.size # width, height
|
| 77 |
+
shape_item['dpi'] = shape.image.dpi # (horz_dpi, vert_dpi)
|
| 78 |
+
shape_item['location'] = (shape.left, shape.top)
|
| 79 |
+
shape_item['location_inches'] = (Inches(shape.left).inches, Inches(shape.top).inches)
|
| 80 |
+
image_stream = io.BytesIO(shape.image.blob)
|
| 81 |
+
shape_image = Image.open(image_stream)
|
| 82 |
+
shape_image.save(image_path)
|
| 83 |
+
# print(shape.image)
|
| 84 |
+
# shape.image.save(image_path)
|
| 85 |
+
pass
|
| 86 |
+
slide_item[f"shape_{j}"] = shape_item
|
| 87 |
+
item[f"slide_{i}"] = slide_item
|
| 88 |
+
|
| 89 |
+
print_json(item)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
pptx_file_path = "templates_from_gaoding/ppt/1.pptx"
|
| 94 |
+
images_dir_path = "images"
|
| 95 |
+
prs = Presentation(pptx_file_path)
|
| 96 |
+
transfer_to_structure(prs, images_dir_path)
|