Skip to content

Instantly share code, notes, and snippets.

@LongHairedHacker
Created March 8, 2017 16:08
Show Gist options
  • Select an option

  • Save LongHairedHacker/a2243512756576ba0350944e07f64334 to your computer and use it in GitHub Desktop.

Select an option

Save LongHairedHacker/a2243512756576ba0350944e07f64334 to your computer and use it in GitHub Desktop.
Quick and Dirty Kicad BOM
#!/usr/bin/env python3
import sys
import xml.etree.ElementTree as ET
DISTRIBUTER_FIELD = 'Mouser'
component_list = {}
def extra_fields_to_dict(fields):
extra_fields = {}
if fields != None:
for field in fields:
key = field.attrib['name']
value = field.text
extra_fields[key] = value
return extra_fields
def add_comp(comp):
ref = comp.attrib['ref']
value = comp.find("value").text
footprint = comp.find("footprint").text
libsrc = comp.find("libsource")
lib = libsrc.attrib['lib']
device = libsrc.attrib['part']
extra_fields = extra_fields_to_dict(comp.find("fields"))
dist_part = None
if DISTRIBUTER_FIELD in extra_fields.keys():
dist_part = extra_fields[DISTRIBUTER_FIELD]
hash_key = (lib, device, value, footprint, dist_part)
if not hash_key in component_list.keys():
component_list[hash_key] = []
component_list[hash_key] += [ref]
def main():
if len(sys.argv) != 3:
print("Usage %s input.xml output.csv", sys.argv[0])
sys.exit(-1)
root = ET.parse(sys.argv[1]).getroot()
components = root.find("components")
for comp in components.findall("comp"):
add_comp(comp)
csv_file = open(sys.argv[2], 'w')
csv_file.write("Count;Value;Device;%s;References;\n" % DISTRIBUTER_FIELD)
sorted_hash_keys = sorted(component_list.keys(), key=lambda k: k[1])
for hash_key in sorted_hash_keys:
refs = component_list[hash_key]
lib, device, value, footprint, dist_part = hash_key
ref_str = ", ".join(refs)
count = len(refs)
if dist_part == None:
dist_part = ""
csv_file.write("%d;%s;%s;%s;%s;\n" % (count, value, device, dist_part, ref_str))
csv_file.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment