Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to extract .emf image from the .docx document #45

Open
sand1k opened this issue Mar 15, 2023 · 1 comment
Open

Fails to extract .emf image from the .docx document #45

sand1k opened this issue Mar 15, 2023 · 1 comment

Comments

@sand1k
Copy link

sand1k commented Mar 15, 2023

Tested on the 'Test Summary.docx' file from the following dataset https://catalog.data.gov/dataset/test-report-for-detonation-velocity-measurements-f22cf

Extracts 4 out of 5 images. The image that is not extracted has a .emf extension.

@cyy-2024
Copy link

It looks like python-docx2txt only supports extracting .png and .jpg files. To handle unsupported formats like .emf, you can add a check in the code and use an external tool (like ImageMagick) to convert .emf to .png. Here's an example modification:
import subprocess
from docx import Document

doc = Document("yourfile.docx")
for rel in doc.part.rels.values():
if "image" in rel.target_ref:
img_name = rel.target_ref.split("/")[-1]
if img_name.endswith(('.png', '.jpg')):
with open(img_name, "wb") as img_file:
img_file.write(rel.target_part.blob)
elif img_name.endswith('.emf'):
with open(img_name, "wb") as img_file:
img_file.write(rel.target_part.blob)
subprocess.run(["magick", img_name, img_name.replace(".emf", ".png")])
This way, .emf files can be detected and converted automatically.

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

No branches or pull requests

2 participants