Skip to content

Commit 42f9069

Browse files
committed
refactor: add default values to getattr calls for improved error handling
1 parent 05760de commit 42f9069

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

examples/presentation_tree.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@
302302
],
303303
"slide_masters": [
304304
{
305-
"id": 4650166864,
305+
"id": 4407743248,
306306
"shapes": [
307307
{
308308
"name": "Title Placeholder 1",

src/tppt/_pptx/tree.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ def color_format_to_dict(color_format: Any) -> dict[str, Any]:
99
data = {}
1010

1111
try:
12-
if rgb := getattr(color_format, "rgb"):
12+
if rgb := getattr(color_format, "rgb", None):
1313
data["rgb"] = str(rgb)
1414

15-
if theme_color := getattr(color_format, "theme_color"):
15+
if theme_color := getattr(color_format, "theme_color", None):
1616
data["theme_color"] = str(theme_color)
1717

18-
if brightness := getattr(color_format, "brightness"):
18+
if brightness := getattr(color_format, "brightness", None):
1919
data["brightness"] = brightness
2020
except Exception:
2121
pass
@@ -36,20 +36,20 @@ def text_frame_to_dict(text_frame: Any) -> dict[str, Any]:
3636
for run in p.runs:
3737
run_data = {"text": run.text}
3838

39-
if font := getattr(run, "font"):
39+
if font := getattr(run, "font", None):
4040
font_data = {}
41-
if name := getattr(font, "name"):
41+
if name := getattr(font, "name", None):
4242
font_data["name"] = name
43-
if size := getattr(font, "size"):
43+
if size := getattr(font, "size", None):
4444
font_data["size"] = size.pt if hasattr(size, "pt") else size
45-
if bold := getattr(font, "bold"):
45+
if bold := getattr(font, "bold", None):
4646
font_data["bold"] = bold
47-
if italic := getattr(font, "italic"):
47+
if italic := getattr(font, "italic", None):
4848
font_data["italic"] = italic
49-
if underline := getattr(font, "underline"):
49+
if underline := getattr(font, "underline", None):
5050
font_data["underline"] = underline
5151

52-
if color := getattr(font, "color"):
52+
if color := getattr(font, "color", None):
5353
font_data["color"] = color_format_to_dict(color)
5454

5555
run_data["font"] = font_data
@@ -131,10 +131,10 @@ def placeholder_to_dict(placeholder: Any) -> dict[str, Any]:
131131
# Add placeholder-specific information
132132
try:
133133
placeholder_data["placeholder_type"] = getattr(
134-
placeholder.placeholder_format, "type"
134+
placeholder.placeholder_format, "type", None
135135
)
136136
placeholder_data["placeholder_idx"] = getattr(
137-
placeholder.placeholder_format, "idx"
137+
placeholder.placeholder_format, "idx", None
138138
)
139139
except Exception:
140140
pass
@@ -192,15 +192,15 @@ def slide_to_dict(slide: Any) -> dict[str, Any]:
192192
}
193193

194194
# Add placeholder information
195-
if placeholders := getattr(slide, "placeholders"):
195+
if placeholders := getattr(slide, "placeholders", None):
196196
slide_data["placeholders"] = [
197197
placeholder_to_dict(placeholder) for placeholder in placeholders
198198
]
199199

200200
# Add notes information
201-
if notes_slide := getattr(slide, "notes_slide"):
201+
if notes_slide := getattr(slide, "notes_slide", None):
202202
notes_placeholders = []
203-
if placeholders := getattr(notes_slide, "placeholders"):
203+
if placeholders := getattr(notes_slide, "placeholders", None):
204204
notes_placeholders = [
205205
placeholder_to_dict(placeholder) for placeholder in placeholders
206206
]
@@ -220,14 +220,14 @@ def ppt2dict(ppt: PptxPresentation) -> dict[str, Any]:
220220
slide_width = None
221221
slide_height = None
222222

223-
if slide_width := getattr(ppt, "slide_width"):
224-
if pt := getattr(slide_width, "pt"):
223+
if slide_width := getattr(ppt, "slide_width", None):
224+
if pt := getattr(slide_width, "pt", None):
225225
slide_width = pt
226226
else:
227227
slide_width = slide_width
228228

229-
if slide_height := getattr(ppt, "slide_height"):
230-
if pt := getattr(slide_height, "pt"):
229+
if slide_height := getattr(ppt, "slide_height", None):
230+
if pt := getattr(slide_height, "pt", None):
231231
slide_height = pt
232232
else:
233233
slide_height = ppt.slide_height
@@ -243,9 +243,9 @@ def ppt2dict(ppt: PptxPresentation) -> dict[str, Any]:
243243
}
244244

245245
# Add note master information
246-
if notes_master := getattr(ppt, "notes_master"):
246+
if notes_master := getattr(ppt, "notes_master", None):
247247
notes_placeholders = []
248-
if placeholders := getattr(notes_master, "placeholders"):
248+
if placeholders := getattr(notes_master, "placeholders", None):
249249
notes_placeholders = [
250250
placeholder_to_dict(placeholder) for placeholder in placeholders
251251
]

0 commit comments

Comments
 (0)