Spaces:
Sleeping
Sleeping
Create pptx2png.py
Browse files- pptx2png.py +139 -0
pptx2png.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
from pptx import Presentation
|
3 |
+
from pptx.enum.shapes import MSO_SHAPE_TYPE
|
4 |
+
from pptx.enum.text import PP_ALIGN
|
5 |
+
from PIL import Image, ImageDraw, ImageFont
|
6 |
+
import skia
|
7 |
+
|
8 |
+
def transfer_textbox_content_in_group(group_shape):
|
9 |
+
"""Edit the content of text boxes within a group shape."""
|
10 |
+
group_shape_item = {}
|
11 |
+
for l, shape in enumerate(group_shape.shapes):
|
12 |
+
shape_item = {}
|
13 |
+
if shape.has_text_frame:
|
14 |
+
shape_item['type'] = "text"
|
15 |
+
shape_item['location'] = (shape.left, shape.top)
|
16 |
+
text_frame = shape.text_frame
|
17 |
+
for r, paragraph in enumerate(text_frame.paragraphs):
|
18 |
+
original_run = paragraph.runs[0]
|
19 |
+
paragraph_item = {}
|
20 |
+
paragraph_item['text'] = paragraph.text
|
21 |
+
paragraph_item['align'] = paragraph.alignment
|
22 |
+
font_item = {}
|
23 |
+
font_item['name'] = original_run.font.name
|
24 |
+
font_item['size'] = original_run.font.size
|
25 |
+
font_item['bold'] = original_run.font.bold
|
26 |
+
font_item['italic'] = original_run.font.italic
|
27 |
+
font_item['underline'] = original_run.font.underline
|
28 |
+
font_item['color'] = original_run.font.color.rgb
|
29 |
+
font_item['language_id'] = original_run.font.language_id
|
30 |
+
paragraph_item['font'] = font_item
|
31 |
+
shape_item[f'paragraph_{r}'] = paragraph_item
|
32 |
+
group_shape_item[f"shape_{l}"] = shape_item
|
33 |
+
return group_shape_item
|
34 |
+
|
35 |
+
def pptx_to_images(pptx_path):
|
36 |
+
# Load the PowerPoint presentation
|
37 |
+
prs = Presentation(pptx_path)
|
38 |
+
|
39 |
+
# List to hold the images
|
40 |
+
images = []
|
41 |
+
|
42 |
+
# Conversion factor from EMUs to pixels
|
43 |
+
EMU_TO_PIXELS = 914400 / 96
|
44 |
+
|
45 |
+
# Conversion factor from EMUs to points
|
46 |
+
EMU_TO_POINTS = 12700
|
47 |
+
|
48 |
+
# Load a default font
|
49 |
+
default_font = ImageFont.load_default()
|
50 |
+
|
51 |
+
# Iterate through each slide in the presentation
|
52 |
+
for slide in prs.slides:
|
53 |
+
# Convert slide dimensions from EMUs to pixels
|
54 |
+
slide_width = int(prs.slide_width / EMU_TO_PIXELS)
|
55 |
+
slide_height = int(prs.slide_height / EMU_TO_PIXELS)
|
56 |
+
|
57 |
+
# Create a blank image with the same dimensions as the slide
|
58 |
+
img = Image.new('RGB', (slide_width, slide_height), color='white')
|
59 |
+
draw = ImageDraw.Draw(img)
|
60 |
+
|
61 |
+
# Draw the slide content onto the image
|
62 |
+
for shape in slide.shapes:
|
63 |
+
if shape.has_text_frame:
|
64 |
+
# Convert shape position from EMUs to pixels
|
65 |
+
shape_left = int(shape.left / EMU_TO_PIXELS)
|
66 |
+
shape_top = int(shape.top / EMU_TO_PIXELS)
|
67 |
+
text_frame = shape.text_frame
|
68 |
+
for paragraph in text_frame.paragraphs:
|
69 |
+
original_run = paragraph.runs[0]
|
70 |
+
font = ImageFont.truetype(original_run.font.name, original_run.font.size)
|
71 |
+
draw.text((shape_left, shape_top), paragraph.text, fill=original_run.font.color.rgb, font=font)
|
72 |
+
elif isinstance(shape, GroupShape):
|
73 |
+
group_content = transfer_textbox_content_in_group(shape)
|
74 |
+
for shape_key, shape_item in group_content.items():
|
75 |
+
if shape_item['type'] == "text":
|
76 |
+
text_location = shape_item['location']
|
77 |
+
text_left = int(text_location[0] / EMU_TO_PIXELS)
|
78 |
+
text_top = int(text_location[1] / EMU_TO_PIXELS)
|
79 |
+
for paragraph_key, paragraph_item in shape_item.items():
|
80 |
+
if paragraph_key.startswith('paragraph_'):
|
81 |
+
font_info = paragraph_item['font']
|
82 |
+
# font = ImageFont.load_default()
|
83 |
+
print(font_info['size'])
|
84 |
+
font_size = int(font_info['size'] / EMU_TO_POINTS) # Convert EMUs to points
|
85 |
+
font = ImageFont.truetype("fonts/FXZK-FANXMLT.ttf", font_size)
|
86 |
+
# font = ImageFont.truetype(font_info['name'], font_info['size'])
|
87 |
+
draw.text((text_left, text_top), paragraph_item['text'], fill=font_info['color'], font=font)
|
88 |
+
elif shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
|
89 |
+
# Convert shape position and size from EMUs to pixels
|
90 |
+
picture_left = int(shape.left / EMU_TO_PIXELS)
|
91 |
+
picture_top = int(shape.top / EMU_TO_PIXELS)
|
92 |
+
picture_width = int(shape.width / EMU_TO_PIXELS)
|
93 |
+
picture_height = int(shape.height / EMU_TO_PIXELS)
|
94 |
+
|
95 |
+
# Load the picture and resize it to the correct dimensions
|
96 |
+
picture = shape.image.blob
|
97 |
+
picture_img = Image.open(io.BytesIO(picture))
|
98 |
+
picture_img = picture_img.resize((picture_width, picture_height), Image.Resampling.LANCZOS)
|
99 |
+
|
100 |
+
# Paste the picture onto the image
|
101 |
+
img.paste(picture_img, (picture_left, picture_top))
|
102 |
+
|
103 |
+
# Convert the PIL image to a byte stream
|
104 |
+
img_byte_arr = io.BytesIO()
|
105 |
+
img.save(img_byte_arr, format='PNG')
|
106 |
+
img_byte_arr.seek(0)
|
107 |
+
|
108 |
+
# Append the image byte stream to the list
|
109 |
+
images.append(img_byte_arr)
|
110 |
+
|
111 |
+
return images, (slide_width, slide_height)
|
112 |
+
|
113 |
+
def render_images_with_skia(images, slide_dimensions, output_dir):
|
114 |
+
# Create the output directory if it doesn't exist
|
115 |
+
import os
|
116 |
+
if not os.path.exists(output_dir):
|
117 |
+
os.makedirs(output_dir)
|
118 |
+
|
119 |
+
# Create a Skia surface
|
120 |
+
slide_width, slide_height = slide_dimensions
|
121 |
+
surface = skia.Surface(slide_width, slide_height)
|
122 |
+
|
123 |
+
with surface as canvas:
|
124 |
+
for i, img_byte_arr in enumerate(images):
|
125 |
+
# Load the image into a Skia image
|
126 |
+
skia_image = skia.Image.MakeFromEncoded(img_byte_arr.getvalue())
|
127 |
+
|
128 |
+
# Draw the image onto the canvas
|
129 |
+
canvas.drawImage(skia_image, 0, 0)
|
130 |
+
|
131 |
+
# Save the canvas to a file
|
132 |
+
output_path = os.path.join(output_dir, f'slide_{i+1}.png')
|
133 |
+
surface.makeImageSnapshot().save(output_path, skia.kPNG)
|
134 |
+
|
135 |
+
if __name__ == "__main__":
|
136 |
+
pptx_path = 'templates_from_gaoding/ppt/1.pptx' # Replace with your .pptx file path
|
137 |
+
output_dir = 'output_slides' # Directory to save the rendered images
|
138 |
+
images, slide_dimensions = pptx_to_images(pptx_path)
|
139 |
+
render_images_with_skia(images, slide_dimensions, output_dir)
|