12

Is there a (Linux) command-line tool to extract all the images from a MS Word document, (preferably one that could handle the .docx format)?

2
  • Is this a linux version with a UI or Command Line Only?
    – Jeff F.
    May 23, 2011 at 20:23
  • @Jeff - A command-line utility would be useful for large batch mode operations.
    – Hooked
    May 23, 2011 at 21:29

3 Answers 3

27

Since docx files are zip files you can unzip the docx file and then pick out the image files.

I have no Microsoft Office to test so I downloaded some random docx files from the internet. It seems that the images are always stored in a word/media directory in the archive.

This command will extract all files from the media directory from the archive:

unzip foo.docx "word/media/*"

This command will extract only *.jpeg files:

unzip foo.docx "*.jpeg"

Note that you have to specify "*.jpg" if the files are saved as jpg instead of jpeg. I assume that it is also possible that images are stored using a different format. I have no idea whether images can be stored in another location other than the word/media directory. You can use unzip -l to list the contents of the archive.

2
  • Well thats useful! Do you know if this holds for the older .doc formats?
    – Hooked
    May 23, 2011 at 21:31
  • 4
    The older doc format was not zipped. It was either a monolithic XML or a binary blob. You can read more about it here.
    – Lesmana
    May 23, 2011 at 21:55
1

I wrote an open source Python program called ofc_media that basically does the unzipping mentioned in lesmana's answer, but automates the search process a bit. It also works on OpenDocument format documents, can limit the extraction to certain file extensions, etc.

0

Saving a Word document as a web page is a technique used on Windows to extract all the images into a folder: http://support.microsoft.com/kb/555171

It might be long winded, but perhaps you could control Open Office on Linux from the command line to extract the images, possibly by converting to a web page and ending up with the images in the supporting folder it would create.

1
  • 1
    I found this problematic. Some of my images became EMZ files - compressed EMF I believe. Noting on my Windows 10 system could open the EMZs (did not try it on my Linux box). However, renaming the *.docx to *.zip and opening it provided access to the EMF files which I could then readily use. Jun 27, 2018 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .