stzhao commited on
Commit
cd3a196
·
verified ·
1 Parent(s): 4a27668

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -1,12 +1,10 @@
 
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
@@ -14,7 +12,7 @@ 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."""
@@ -29,7 +27,6 @@ def transfer_textbox_content_in_group(group_shape):
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
@@ -43,8 +40,9 @@ def transfer_textbox_content_in_group(group_shape):
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):
@@ -80,17 +78,33 @@ def transfer_to_structure(prs, images_dir_path):
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
  from pptx import Presentation
3
+ from pptx.util import Pt, Inches
4
  from pptx.shapes.group import GroupShape
5
  from pptx.dml.color import RGBColor
6
  from pptx.enum.text import PP_ALIGN
 
7
  from pptx.shapes.picture import Picture
 
 
8
  import json
9
  import os
10
  from PIL import Image
 
12
 
13
  def print_json(item):
14
  item_json = json.dumps(item, ensure_ascii=False, indent=4)
15
+ return item_json
16
 
17
  def transfer_textbox_content_in_group(group_shape):
18
  """Edit the content of text boxes within a group shape."""
 
27
  original_run = paragraph.runs[0]
28
  paragraph_item = {}
29
  paragraph_item['text'] = paragraph.text
 
30
  paragraph_item['align'] = paragraph.alignment
31
  font_item = {}
32
  font_item['name'] = original_run.font.name
 
40
  group_shape_item[f"shape_{l}"] = shape_item
41
  return group_shape_item
42
 
43
+ def transfer_to_structure(pptx_file, images_dir_path):
44
  item = {}
45
+ prs = Presentation(pptx_file)
46
 
47
  # Iterate through each slide in the presentation
48
  for i, slide in enumerate(prs.slides):
 
78
  image_stream = io.BytesIO(shape.image.blob)
79
  shape_image = Image.open(image_stream)
80
  shape_image.save(image_path)
 
 
81
  pass
82
  slide_item[f"shape_{j}"] = shape_item
83
  item[f"slide_{i}"] = slide_item
84
 
85
+ return print_json(item)
86
+
87
+ def copy_font_style(original_run, new_run):
88
+ new_run.font.name = original_run.font.name
89
+ new_run.font.bold = original_run.font.bold
90
+ new_run.font.italic = original_run.font.italic
91
+ new_run.font.underline = original_run.font.underline
92
+ new_run.font.color.rgb = original_run.font.color.rgb
93
+ new_run.font.language_id = original_run.font.language_id
94
+
95
+ def process_pptx(pptx_file):
96
  images_dir_path = "images"
97
+ if not os.path.exists(images_dir_path):
98
+ os.makedirs(images_dir_path)
99
+ return transfer_to_structure(pptx_file.name, images_dir_path)
100
+
101
+ # Gradio interface
102
+ iface = gr.Interface(
103
+ fn=process_pptx,
104
+ inputs=gr.File(label="Upload PowerPoint File"),
105
+ outputs=gr.Textbox(label="JSON Output"),
106
+ title="PowerPoint to JSON Converter",
107
+ description="Upload a PowerPoint file to convert its structure to JSON."
108
+ )
109
+
110
+ iface.launch()