Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
American Express updated their online banking interface and provides an XML-based QFX format that the GnuCash updater cannot import. This Ruby script converts the new format back to the QFXSGML format that GnuCash knows how to import. Experimental; use at your own risk. It works for me.
#!/usr/bin/env ruby
# frozen_string_literal: true
# Convert new American Express QFX files (since June 2020) to older SGML format
# that GnuCash can import.
require 'nokogiri'
def show_usage
warn 'Usage: amex-ofx-downgrader.rb /path/to/source/file.qfx /path/to/output.qfx'
exit 1
end
show_usage unless ARGV.count == 2
source_file = ARGV.first
unless File.exist?(source_file)
warn "Input file #{source_file} does not exist."
show_usage
end
output_file = ARGV.last
if File.exist?(output_file)
warn "Output file #{output_file} ALREADY EXISTS! Wont overwrite."
show_usage
end
doc = Nokogiri::XML.parse(File.read(source_file))
File.open(output_file, 'wb') do |out|
out.puts <<~HEADER
OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:UTF8
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
HEADER
out.puts '<OFX>'
out.puts doc.xpath('//SIGNONMSGSRSV1').to_xml
out.puts doc.xpath('//CREDITCARDMSGSRSV1').to_xml
out.puts '</OFX>'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment