Last active
August 29, 2015 14:21
-
-
Save victerryso/020ce722da6c0c31b553 to your computer and use it in GitHub Desktop.
Extracting Images From DOCX File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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