Skip to content

added functionality for reading alt text of shape#512

Open
cray2015 wants to merge 1 commit into
scanny:masterfrom
cray2015:master
Open

added functionality for reading alt text of shape#512
cray2015 wants to merge 1 commit into
scanny:masterfrom
cray2015:master

Conversation

@cray2015
Copy link
Copy Markdown

#508 issue implemented in this PR. kindly review it.

@honcharovs
Copy link
Copy Markdown

I have implemented this property on my local system and on my fork copy.
It works as it should.

Copy link
Copy Markdown

@honcharovs honcharovs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works as it should.
Can you change
pptx/oxml/shapes/shared.py line 314 ' to " around 'descr'?

@cray2015
Copy link
Copy Markdown
Author

@honcharovs are you asking me or the repo owner?

@will-plutoflume
Copy link
Copy Markdown

Hi All. Aware this is an old thread. Is there anything preventing this being merged? It would be a really useful addition.

@Vlad22
Copy link
Copy Markdown

Vlad22 commented Feb 6, 2023

Hi All. Just want to upvote this. @scanny, please review and include.

@rgmon
Copy link
Copy Markdown

rgmon commented Feb 8, 2023

Hi, it would be useful capability. @scanny could you include it?

@hermanc1
Copy link
Copy Markdown

hermanc1 commented Sep 9, 2023

This would be super useful. Any obstacles to it being merged?

@scanny
Copy link
Copy Markdown
Owner

scanny commented Sep 11, 2023

@hermanc1 You can accomplish this inline like this:

alt_text = shape._element._nvXxPr.cNvPr.attrib.get("descr", "")

or wrap it in a utility/work-around function like this:

from pptx.shapes.base import BaseShape

def shape_alt_text(shape: BaseShape) -> str:
    """Alt-text defined in shape's `descr` attribute, or "" if not present."""
    return shape._element._nvXxPr.cNvPr.attrib.get("descr", "")

We might well add this at something like shape.alt_text, but it's not currently on the docket and a function like this is easy to add to your package or in some sort of pptxutils package you put together if you use it on a lot of projects and want to maintain only one copy of the functions like this.

@hermanc1
Copy link
Copy Markdown

Thank you @scanny - I appreciate it! I implemented it now and it works well. One small note: in the docstring for the function, you call the attribute desc, but (as in your code) it is descr.
Thank you once again - it really helps!

@scanny
Copy link
Copy Markdown
Owner

scanny commented Sep 25, 2023

Good catch, thanks @hermanc1 :) I've updated the docstring.

I expect to be in making a few additions before long so have shortlisted this to consider for the next release as well.

@robertschulze
Copy link
Copy Markdown

is there any update?

@mdylan2
Copy link
Copy Markdown

mdylan2 commented Jul 30, 2024

Any progress here?

@wienand
Copy link
Copy Markdown

wienand commented Oct 29, 2024

Can I somehow help to get this merged?

@ZmeiGorynych
Copy link
Copy Markdown

+1

loadfix pushed a commit to loadfix/python-pptx that referenced this pull request May 29, 2026
…]) (scanny#512)

Closes scanny#512.

Scheduled / unattended deck rebuilds crash the day a primary image
source quietly moves: the CDN URL 404s, the brand-asset path on the
shared drive is renamed, the local cache was cleaned by the agent.

`pptx.kit.image_fallback_chain` wraps `add_picture` with a fallback
chain — a list of candidate sources tried in priority order. The
first one that loads becomes the embedded image; the rest are
ignored. When every source fails, an `on_fail` policy picks
**raise** (default), **warn** (log + return None), or **placeholder**
(embed a deterministic 1x1 transparent PNG so the rest of the slide
build can continue).

Each candidate may be a filesystem path (`str` / `os.PathLike`), an
`http(s)://` / `file://` URL (stdlib `urllib`, no extra runtime dep,
configurable timeout), a `bytes`/`bytearray`/`memoryview` blob, or
any file-like object exposing `.read()`. URL credentials and query
strings are redacted from log payloads via `redact_source` so
secrets never leak into cron logs. `bind()` installs
`add_picture_with_fallback` on Slide / SlideShapes for the natural
chained call shape.

Public surface:
- `fallback_chain(target, sources, left, top, ...)` — list-first
- `add_picture_with_fallback(target, primary, fallbacks, ...)` —
  keyword-form mirroring the issue spec literal
- `bind()` — install Slide.add_picture_with_fallback (idempotent)
- `is_placeholder_picture(picture)` — audit-side helper
- `make_placeholder_blob()`, `redact_source(src)`,
  `AllSourcesFailedError`, `PLACEHOLDER_PNG_BYTES`, `ON_FAIL_MODES`,
  `RECOVERABLE_LOAD_ERRORS`, `DEFAULT_TIMEOUT`

64 unit tests covering all public callables, every `on_fail` branch,
all four source-shape branches, URL redaction (incl. userinfo +
query string), the bound-method binding, and end-to-end secret-
redaction in warn-mode log payloads.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.