Skip to content

Instantly share code, notes, and snippets.

@victerryso
Last active August 29, 2015 14:21
Show Gist options
  • Save victerryso/020ce722da6c0c31b553 to your computer and use it in GitHub Desktop.
Save victerryso/020ce722da6c0c31b553 to your computer and use it in GitHub Desktop.
Extracting Images From DOCX File
require 'zip'
def extract_images(file_path, destination = nil)
destination = File.basename(file_path, '.*') unless destination
file_path = File.join(Dir.pwd, file_path)
Zip::File.open(file_path) do |zip_file|
zip_file.each do |file|
next unless file.name =~ /word\/media/
new_file = File.join(destination, File.basename(file.name))
FileUtils.mkdir_p(File.dirname(new_file))
zip_file.extract(file, new_file) unless File.exist?(new_file)
end
end
end
extract_images('word.docx')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment