xml2csv.rb : KiCad BOM generator in Ruby

In the previous topic where I explained how to create your BOM from the KiCad’s XML export, I mentioned my own script generator, written in Ruby, xml2csv.rb.

It’s nothing fancy at all and there are already tons of BOM generators out there, but it’s always fun to create your own tool, suiting your very own special needs.

In fact, my special need wasn’t that special. Just to be able to create a CSV file for import in my favorite electronic sellers webshops. And it was actually simpler to create a script from scratch than to modify an existing one.

Prerequisite

xml2csv.rb is a Ruby script, so it needs Ruby. On Linux, just search in your distro package manager.

  • sudo apt-get install ruby in Ubuntu and Debian.
  • Ruby Installer for Windows users.

Usage

# ruby xml2csv.rb -h
xml2csv version 20160823
(C) David Haillant

Usage: xml2csv [options]
-i, --input Kicad XML export file
-o, --output Generated CSV file. If omitted, .csv will be created
-g, --group_by Group components by property
-s, --separator CSV style separator character. Default is comma.

Below is a basic example command line for KiCad:
ruby "/home/david/kicad-xml2csv.rb/xml2csv.rb" -i "%I" -o "%O.csv"

%I and %O are replaced by the KiCad’s project name. We use them here as input with -i and output with -o parameters. Enclose in double quotes (“) to ensure files containing space characters will be well handled.

This is the output log:
Run command:
ruby "/home/david/kicad-xml2csv.rb/xml2csv.rb" -i "/home/david/MC34063-inverting.xml" -o "/home/david/MC34063-inverting.csv"

Success

Info messages:
xml2csv version 20160823
(C) David Haillant

Loading XML file /home/david/MC34063-inverting.xml.............. 11 components found

CSV output file: /home/david/MC34063-inverting.csv, using ',' as separator
Writing CSV file...
Done.

It’s a bit verbose but everything is fine. In the XML file, 11 components were found.

“group by” extra field

Sellers websites offer to upload CSV file containing components refs and the amount to order.

KiCad allows you to add, in component properties, extra fields. They are useful for adding comments, specific references, component tolerance, voltage, power, etc.
So I personally use those fields to specify the vendor component’s reference. This way, when I export my BOM, grouped by the vendor ref, using -g parameter, I get this kind of result:
"UPW1H102MHD",1
"CCK-220P",1
"UPW1E101MED",1
"1N5819/ST",1
"TB-5.08-P-2P/BL",2

The first value is the extra field, the second, the number of occurrences.

Example command line using “group by” parameter:
ruby "/home/david/kicad-xml2csv.rb/xml2csv.rb" -i "%I" -g "your-extra-field" -o "%O.csv"

Download

The code still needs to be cleaned and tidied up. Some bug may persist.
Please, feel free to comment, fork, modify…
Download the latest release
You can find all the source code on GitHub

Leave a Reply

Your email address will not be published. Required fields are marked *