Skip to content

Instantly share code, notes, and snippets.

@tomo3141592653
Last active August 29, 2015 14:02
Show Gist options
  • Save tomo3141592653/f7ed13cdd672ecd35743 to your computer and use it in GitHub Desktop.
Save tomo3141592653/f7ed13cdd672ecd35743 to your computer and use it in GitHub Desktop.
convert .xlsx file to .tsv file
#! /usr/bin/ruby -Ku
#usage:
#type to terminal "ruby xlsx_to_tsv.rb hoge.xlsx" and then hoge.tsv will be made in the current folder.
#or you can also use wildcard ex "ruby xlsx_to_tsv.rb *.xlsx"
#YOU SHULD NOTE THAT *.tsv in current folder will be overwritten.
#data is assumed to be only in first sheet.
require 'rubygems'
require 'roo'
require "uri"
require "tempfile"
while file = ARGV.shift
tmp_file = Tempfile.new('_tmp.xlsx')
FileUtils.cp(file, '_tmp.xlsx')
file_base = File.basename(file,".xlsx")
fp = open(file_base+".tsv","w")
book = Roo::Spreadsheet.open('_tmp.xlsx')
sheet = book.sheet(0)
sheet.each do |row|
fp.puts row.join("\t")
end
tmp_file.close
#sheet.to_csv(file_base+'.csv')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment