1

I'm looking for a tool that can extract detect and extract any image format from word documents in both .doc and .docx formats for an automatic document classification use case. It is nice to just detect the presence of images, but it infinitely more valuable to automatically carve the images out of the file through detecting the magic bytes and carving based on the file header.

I'm looking for something like oletools, but that toolkit doesn't support gathering images. If there aren't any tools or libraries that satisfy my use case, then any libraries or tools that could support my own development of such a tool would be appreciated. Even research papers or articles on the theory behind such a tool, or even the doc/docx file format would be appreciated. I would rather avoid building what I need for my use case from scratch.

I would prefer python based tools, but I'll take what I can get. To be clear, I want a command line based tool, or a tool that can be added to a program or a script.

My main difficulty is in working with the legacy ole format (.doc, .xls, .ppt) as the new .***x format is just a zip file that can be dealt with easily in a programmatic fashion. The old format can be opened as a zip, but the images are stored in a stream called "Data", and they're mashed in there with other things, I can't just read "Data" as an image as the image header is ~250 bytes in.

1 Answer 1

1

Python docx2txt is exactly what you're looking for.

Usage: docx2txt -i /tmp/img_dir file.docx

Multiple files may be strung together and processed, either with a BATCH (.bat) script, SHEBANG (.sh) script, or Python (.py) script.

On the other hand, if you're ok with Java, there's Apache POI -- https://poi.apache.org -- which could do that all in memory, via an API to interface with Microsoft Word documents. It includes the PicturesTable class (https://poi.apache.org/apidocs/dev/org/apache/poi/hwpf/model/PicturesTable.html) which supports a method for the extraction of a picture object, all in memory. This library is 100% compatible with .doc, .docx, as well as ole and XML. Even if you're not familiar with Java, it would be very straightforward to create a program with which to make this happen.

9
  • Thank you, I still will need a tool to extract images from legacy ole .doc files though, so I can't accept your answer... My main problem really is working with the legacy ole file format as the .***x file format is just a zip file.
    – solumnant
    Jun 5, 2019 at 14:16
  • This question (stackoverflow.com/questions/38468442/…) would help you convert those .doc/ole files to .docx, which you'd then be able to access using docx2txt.
    – user96931
    Jun 5, 2019 at 15:22
  • This is going to be a production tool I will need to propose to technical management, any additional processing or storage overhead makes it more likely that the tool will not be accepted. It would be easier to go in, scrape the data and get out. I will have multiple, skeptical technical eyes on this tool.
    – solumnant
    Jun 5, 2019 at 15:30
  • 1
    The Python script which would be the result wouldn't be more than 50 loc, and the only prerequisites is the Microsoft Office suite.
    – user96931
    Jun 5, 2019 at 15:34
  • The use of an external library that converts the entire document when all I need to do is extract data from a stream is a heavy increase in the overhead that is necessary, and using this tool requires that you write the converted files to disk before you continue processing them, which is also a heavy increase in the overhead that is necessary. My code would be simple, but the machine responsible would have to perform many more costly operations than what is needed, which is why I can't accept your solution.
    – solumnant
    Jun 5, 2019 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.