How to read image and display image from word document

Hi,
I am trying to create a script where it should read text and image from a word document. is there any package that i could use to get image and text for specified keys?
any help is appreciated.
Thanks.

Import the stuff into your Dynamo script first…

What have you got so far?

Hi Alien, I have imported the word document and its reading the text but not the images. i have an image at index 4 but it is showing empty. is there a way to display image.
Thanks.

That was fun :smiley:

I was meant to be doing other work but this may come in handy one day :smiley:

So I created a test word document :
image

Then got ChatGPT to help cos I’m lazy :smiley:

Without much more difficulty you could have a better name than, “image1”

3 Likes

Hi,

another python solution using OpenXml

python code (compatible all engines)

import sys
import clr
import System

clr.AddReference('System.Drawing')
import System.Drawing
from System.Drawing import Bitmap

clr.AddReference('DocumentFormat.OpenXml')
import DocumentFormat
from DocumentFormat.OpenXml import *
from DocumentFormat.OpenXml.Packaging import *
from DocumentFormat.OpenXml.Spreadsheet import *

my_path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)

filepath = IN[0]
out = []

wordProcessingDoc = WordprocessingDocument.Open(filepath, False)
imgParts = [x for x in wordProcessingDoc.MainDocumentPart.ImageParts]
for imagePart in wordProcessingDoc.MainDocumentPart.ImageParts:
    uri = imagePart.Uri
    filename = uri.ToString().split('/')[-1]
    stream = wordProcessingDoc.Package.GetPart(uri).GetStream()
    b = Bitmap(stream)
    b.Save(my_path + "\\" + filename)
    out.append([b, my_path + "\\" + filename])

wordProcessingDoc.Close()
OUT = out
2 Likes