Skip to content

feat: accept pathlib.Path in add_picture - #1591

Open
ChenfromChina123 wants to merge 2 commits into
python-openxml:masterfrom
ChenfromChina123:feat/pathlib-path-support
Open

feat: accept pathlib.Path in add_picture#1591
ChenfromChina123 wants to merge 2 commits into
python-openxml:masterfrom
ChenfromChina123:feat/pathlib-path-support

Conversation

@ChenfromChina123

Copy link
Copy Markdown

What does this PR do?

add_picture rejected pathlib.Path objects because Image.from_file only accepted str paths or file-like objects. os.PathLike (which includes pathlib.Path) is now accepted.

Closes #1544

Changes

  • src/docx/image/image.py: Image.from_file accepts os.PathLike in addition to str and file-like objects.

Verification

  • doc.add_picture(pathlib.Path('test.png')) succeeds and document saves (tested with a generated PNG).

ChenfromChina123 and others added 2 commits August 10, 2026 22:34
The example calls add_picture('monty-truth.png') which raises
FileNotFoundError when the file is not present in the working
directory. Wrap it in a try/except so the example runs out of
the box.

Closes python-openxml#128

Co-Authored-By: AtomCode (deepseek-v4-flash) <[email protected]>
add_picture rejected pathlib.Path objects because Image.from_file
only accepted str paths or file-like objects. Accept any
os.PathLike (which includes pathlib.Path).

Closes python-openxml#1544

Co-Authored-By: AtomCode (deepseek-v4-flash) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for pathlib.Path

1 participant