Class: SwarmSDK::Tools::ImageExtractors::DocxImageExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/tools/image_extractors/docx_image_extractor.rb

Overview

Extracts images from DOCX documents DOCX files are ZIP archives with images stored in word/media/

Class Method Summary collapse

Class Method Details

.extract_images(doc, docx_path) ⇒ Array<String>

Extract all images from a DOCX document

Parameters:

  • The DOCX document instance

  • Path to the DOCX file

Returns:

  • Array of temporary file paths containing extracted images



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/swarm_sdk/tools/image_extractors/docx_image_extractor.rb', line 14

def extract_images(doc, docx_path)
  image_paths = []
  temp_dir = Dir.mktmpdir("docx_images_#{File.basename(docx_path, ".*")}")

  # DOCX files are ZIP archives with images in word/media/
  doc.zip.glob("word/media/*").each do |entry|
    next unless entry.file?

    # Check if it's an image by extension
    next unless entry.name.match?(/\.(png|jpe?g|gif|bmp|tiff?)$/i)

    output_path = File.join(temp_dir, File.basename(entry.name))

    File.open(output_path, "wb") do |f|
      f.write(doc.zip.read(entry.name))
    end

    image_paths << output_path
  end

  image_paths
rescue StandardError
  # If image extraction fails, don't fail the entire document read
  []
end