Given an existing shape (with a text frame) and a paragraphs_spec describing paragraphs and runs, populate the shape's text frame. 'paragraphs_spec' is a list of paragraphs, each containing: - bullet: bool - level: int (indent level) - alignment: str ("left", "center"
(shape, paragraphs_spec, vertical_anchor=None)
| 1373 | return shape |
| 1374 | |
| 1375 | def fill_textframe(shape, paragraphs_spec, vertical_anchor=None): |
| 1376 | """ |
| 1377 | Given an existing shape (with a text frame) and a paragraphs_spec |
| 1378 | describing paragraphs and runs, populate the shape's text frame. |
| 1379 | |
| 1380 | 'paragraphs_spec' is a list of paragraphs, each containing: |
| 1381 | - bullet: bool |
| 1382 | - level: int (indent level) |
| 1383 | - alignment: str ("left", "center", "right", or "justify") |
| 1384 | - font_size: int |
| 1385 | - runs: list of run dictionaries, each with: |
| 1386 | text: str |
| 1387 | bold: bool |
| 1388 | italic: bool |
| 1389 | color: [r,g,b] or None |
| 1390 | font_size: int (optional, overrides paragraph default) |
| 1391 | fill_color: [r,g,b] or None |
| 1392 | |
| 1393 | :param vertical_anchor: Optional MSO_ANCHOR constant or string ("top", "middle", "bottom") |
| 1394 | to control vertical alignment of text within textbox |
| 1395 | """ |
| 1396 | text_frame = shape.text_frame |
| 1397 | # Ensure stable layout |
| 1398 | text_frame.auto_size = MSO_AUTO_SIZE.NONE |
| 1399 | text_frame.word_wrap = True |
| 1400 | |
| 1401 | # Set vertical anchor if provided |
| 1402 | if vertical_anchor is not None: |
| 1403 | if isinstance(vertical_anchor, str): |
| 1404 | anchor_map = { |
| 1405 | "top": MSO_ANCHOR.TOP, |
| 1406 | "middle": MSO_ANCHOR.MIDDLE, |
| 1407 | "bottom": MSO_ANCHOR.BOTTOM, |
| 1408 | } |
| 1409 | text_frame.vertical_anchor = anchor_map.get(vertical_anchor.lower(), MSO_ANCHOR.TOP) |
| 1410 | else: |
| 1411 | text_frame.vertical_anchor = vertical_anchor |
| 1412 | |
| 1413 | # Clear out existing paragraphs |
| 1414 | text_frame.clear() |
| 1415 | |
| 1416 | for p_data in paragraphs_spec: |
| 1417 | p = text_frame.add_paragraph() |
| 1418 | |
| 1419 | # # bulleting |
| 1420 | # p.bullet = p_data.get("bullet", False) |
| 1421 | |
| 1422 | # bullet level (indent) |
| 1423 | p.level = p_data.get("level", 0) |
| 1424 | |
| 1425 | # paragraph alignment |
| 1426 | align_str = p_data.get("alignment", "left") |
| 1427 | p.alignment = _parse_alignment(align_str) |
| 1428 | |
| 1429 | # paragraph-level font size |
| 1430 | default_font_size = p_data.get("font_size", 24) |
| 1431 | p.font.size = Pt(default_font_size) |
| 1432 |
nothing calls this directly
no test coverage detected