Last active
September 23, 2022 16:08
-
-
Save aspose-com-kb/1d5f357376fed0a1004110bda45e7e25 to your computer and use it in GitHub Desktop.
Code to Extract All Images from Word Document in Python. For more information: https://kb.aspose.com/words/python/how-to-extract-all-images-from-word-document-in-python/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.words as aw | |
# Load the license | |
wordProtected = aw.License() | |
wordProtected.set_license("Aspose.Total.lic") | |
# Load a document | |
wordDocument = aw.Document("WordFileWithImages.docx") | |
# Get shapes collection | |
allShapes = wordDocument.get_child_nodes(aw.NodeType.SHAPE, True) | |
# Declare counter for images | |
index = 0 | |
# Iterate through all the shapes to detect and save images | |
for shape in allShapes: | |
# Type cast the node object to shape | |
shape = shape.as_shape() | |
if(shape.has_image): | |
index = index + 1 | |
# Prepare file name using the image counter and image type in the shape object | |
image_file_name = f"File.extract_images.{index}{aw.FileFormatUtil.image_type_to_extension(shape.image_data.image_type)}" | |
# Save the extracted image on the disk | |
shape.image_data.save(image_file_name) | |
print ("Images extracted successfully from the Word file") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment