|
3 | 3 | import os |
4 | 4 | from typing import Any, Dict |
5 | 5 |
|
6 | | -from pptx import Presentation |
7 | 6 | from pptx.enum.shapes import MSO_SHAPE_TYPE |
| 7 | +from pptx.presentation import Presentation as PptxPresentation |
8 | 8 |
|
9 | 9 |
|
10 | 10 | def color_format_to_dict(color_format: Any) -> Dict[str, Any]: |
@@ -188,7 +188,7 @@ def slide_master_to_dict(slide_master: Any) -> Dict[str, Any]: |
188 | 188 | for layout in slide_master.slide_layouts: |
189 | 189 | layout_dict = slide_layout_to_dict(layout) |
190 | 190 | if slide_layouts := master_data.get("slide_layouts"): |
191 | | - slide_layouts.append(layout_dict) |
| 191 | + slide_layouts.append(layout_dict) # type: ignore |
192 | 192 |
|
193 | 193 | return master_data |
194 | 194 |
|
@@ -227,48 +227,45 @@ def slide_to_dict(slide: Any) -> Dict[str, Any]: |
227 | 227 | return slide_data |
228 | 228 |
|
229 | 229 |
|
230 | | -def presentation_to_dict(pptx_path: str) -> Dict[str, Any]: |
| 230 | +def presentation_to_dict(ppt: PptxPresentation) -> Dict[str, Any]: |
231 | 231 | """Convert presentation information to dictionary""" |
232 | | - prs = Presentation(pptx_path) |
233 | 232 |
|
234 | 233 | # スライドのサイズを安全に取得 |
235 | 234 | slide_width = None |
236 | 235 | slide_height = None |
237 | 236 |
|
238 | | - if hasattr(prs, "slide_width"): |
239 | | - if hasattr(prs.slide_width, "pt"): |
240 | | - slide_width = prs.slide_width.pt |
| 237 | + if slide_width := getattr(ppt, "slide_width"): |
| 238 | + if pt := getattr(slide_width, "pt"): |
| 239 | + slide_width = pt |
241 | 240 | else: |
242 | | - slide_width = prs.slide_width |
| 241 | + slide_width = slide_width |
243 | 242 |
|
244 | | - if hasattr(prs, "slide_height"): |
245 | | - if hasattr(prs.slide_height, "pt"): |
246 | | - slide_height = prs.slide_height.pt |
| 243 | + if slide_height := getattr(ppt, "slide_height"): |
| 244 | + if pt := getattr(slide_height, "pt"): |
| 245 | + slide_height = pt |
247 | 246 | else: |
248 | | - slide_height = prs.slide_height |
| 247 | + slide_height = ppt.slide_height |
249 | 248 |
|
250 | 249 | prs_data = { |
251 | | - "file_path": pptx_path, |
252 | | - "slides_count": len(prs.slides), |
253 | | - "slide_masters_count": len(prs.slide_masters), |
254 | | - "slide_layouts_count": len(prs.slide_layouts), |
| 250 | + "slides_count": len(ppt.slides), |
| 251 | + "slide_masters_count": len(ppt.slide_masters), |
| 252 | + "slide_layouts_count": len(ppt.slide_layouts), |
255 | 253 | "slide_width": slide_width, |
256 | 254 | "slide_height": slide_height, |
257 | | - "slides": [slide_to_dict(slide) for slide in prs.slides], |
258 | | - "slide_masters": [slide_master_to_dict(master) for master in prs.slide_masters], |
| 255 | + "slides": [slide_to_dict(slide) for slide in ppt.slides], |
| 256 | + "slide_masters": [slide_master_to_dict(master) for master in ppt.slide_masters], |
259 | 257 | } |
260 | 258 |
|
261 | 259 | # ノートマスターの情報を追加 |
262 | | - if hasattr(prs, "notes_master") and prs.notes_master is not None: |
| 260 | + if notes_master := getattr(ppt, "notes_master"): |
263 | 261 | notes_placeholders = [] |
264 | | - if hasattr(prs.notes_master, "placeholders"): |
| 262 | + if placeholders := getattr(notes_master, "placeholders"): |
265 | 263 | notes_placeholders = [ |
266 | | - placeholder_to_dict(placeholder) |
267 | | - for placeholder in prs.notes_master.placeholders |
| 264 | + placeholder_to_dict(placeholder) for placeholder in placeholders |
268 | 265 | ] |
269 | 266 |
|
270 | 267 | prs_data["notes_master"] = { |
271 | | - "shapes": [shape_to_dict(shape) for shape in prs.notes_master.shapes], |
| 268 | + "shapes": [shape_to_dict(shape) for shape in ppt.notes_master.shapes], |
272 | 269 | "placeholders": notes_placeholders, |
273 | 270 | } |
274 | 271 |
|
@@ -305,7 +302,3 @@ def main(): |
305 | 302 | json.dump(prs_data, f, ensure_ascii=False, indent=args.indent) |
306 | 303 |
|
307 | 304 | print(f"Structure of '{args.pptx_file}' has been output to '{output_path}'.") |
308 | | - |
309 | | - |
310 | | -if __name__ == "__main__": |
311 | | - main() |
0 commit comments