You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, So I was trying to extract Images/smart art/diagrams as snapshots/screenshots and save in in .png format using below script.
import win32com.client as win32
from PIL import ImageGrab, Image
import os
import time
def screenshot_word_drawings_revised(doc_path, output_dir):
"""
Screenshots all drawing objects (both inline and floating) in a Word document.
Args:
doc_path (str): The full path to the Word document.
output_dir (str): The directory to save the screenshots in.
"""
# 1. --- Basic Path Validation ---
if not os.path.exists(doc_path):
print(f"ERROR: The document path does not exist: {doc_path}")
return
if not os.path.exists(output_dir):
print(f"Creating output directory: {output_dir}")
os.makedirs(output_dir)
word = None
try:
# 2. --- Start Word and Open Document ---
print("Starting MS Word...")
# Using EnsureDispatch helps prevent issues with different Word versions
word = win32.gencache.EnsureDispatch('Word.Application')
# Set Visible to True for debugging. You can see the script working!
word.Visible = True
doc = word.Documents.Open(doc_path)
print(f"Opened document: {os.path.basename(doc_path)}")
# --- Process both Inline and Floating Shapes ---
shape_collections = {
"InlineShape": doc.InlineShapes,
"Shape": doc.Shapes
}
total_shapes_found = 0
images_saved = 0
for shape_type, shapes in shape_collections.items():
if not shapes:
print(f"\nNo objects found in the '{shape_type}' collection.")
continue
print(f"\nProcessing {len(shapes)} objects in '{shape_type}' collection...")
total_shapes_found += len(shapes)
for i, shape in enumerate(shapes):
print(f" -> Found {shape_type} #{i + 1}. Attempting to copy...")
# 3. --- Copy Shape to Clipboard ---
shape.Select()
word.Selection.CopyAsPicture()
# Give the clipboard a moment to catch up
time.sleep(0.5)
# 4. --- Grab Image from Clipboard ---
img = ImageGrab.grabclipboard()
# 5. --- Save Image if it Exists ---
if isinstance(img, Image.Image):
output_filename = os.path.join(output_dir, f"{shape_type}_{i+1}.png")
img.save(output_filename, 'PNG')
print(f" ✅ Successfully saved to {output_filename}")
images_saved += 1
else:
print(" ⚠️ Clipboard did not contain a valid image. This object might not be a visible drawing.")
doc.Close(SaveChanges=False)
print("\n🎉 Processing complete.")
print(f"Found {total_shapes_found} total shape objects.")
print(f"Successfully saved {images_saved} images.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
import traceback
traceback.print_exc()
finally:
if word:
print("Closing MS Word application.")
word.Quit()
if __name__ == '__main__':
document_path = r"C:\Internship\Agenthix AI\Text extraction (question to PPT)\word-to-ppt\templates\LWHO2502505.docx"
output_folder = r"C:\Internship\Agenthix AI\Text extraction (question to PPT)\word-to-ppt\templates\word_drawings_output"
screenshot_word_drawings_revised(document_path, output_folder)
where I get the output as below:
Starting MS Word...
Opened document: LWHO2502505.docx
Processing 0 objects in 'InlineShape' collection...
Processing 5 objects in 'Shape' collection...
-> Found Shape #1. Attempting to copy...
Successfully saved to C:\Internship\Agenthix AI\Text extraction (question to PPT)\word-to-ppt\templates\word_drawings_output\Shape_1.png
-> Found Shape #2. Attempting to copy...
Clipboard did not contain a valid image. This object might not be a visible drawing.
-> Found Shape #3. Attempting to copy...
Clipboard did not contain a valid image. This object might not be a visible drawing.
-> Found Shape #4. Attempting to copy...
Successfully saved to C:\Internship\Agenthix AI\Text extraction (question to PPT)\word-to-ppt\templates\word_drawings_output\Shape_4.png
-> Found Shape #5. Attempting to copy...
Successfully saved to C:\Internship\Agenthix AI\Text extraction (question to PPT)\word-to-ppt\templates\word_drawings_output\Shape_5.png
Processing complete.
Found 5 total shape objects.
Successfully saved 3 images.
Closing MS Word application.
Also my diagrams from input word doc looks as below attached screenshots:
So, why is it failing sometimes. whereas before I tried it was working and the results change as many times I run this script.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, So I was trying to extract Images/smart art/diagrams as snapshots/screenshots and save in in .png format using below script.
where I get the output as below:
Also my diagrams from input word doc looks as below attached screenshots:
So, why is it failing sometimes. whereas before I tried it was working and the results change as many times I run this script.
Beta Was this translation helpful? Give feedback.
All reactions