Skip to content

Instantly share code, notes, and snippets.

@Drunkar
Forked from andete/gist:71b531b081fbc3ac0671
Last active June 30, 2017 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drunkar/0b1ab1f8df6bbe28cbef4c79420ec753 to your computer and use it in GitHub Desktop.
Save Drunkar/0b1ab1f8df6bbe28cbef4c79420ec753 to your computer and use it in GitHub Desktop.
KiCad csv BOM generter plugin
#!/usr/bin/env python
# (c) 2015 Productize <joost@productize.be>
# edited by Drunkar <drunkars.p@gmail.com>
import os
import sys
import copy
import collections
import codecs
from bs4 import BeautifulSoup
xml_file = sys.argv[1]
with codecs.open(sys.argv[1], encoding='utf-8') as f:
soup = BeautifulSoup(f, "lxml")
date = soup.design.date.contents[0]
comp = {}
comp_nc = {}
standard_fields = ['Reference', 'Identifier', 'Package',
'Value', 'Tolerance', 'Voltage', 'Current', 'Power', 'PN']
extra_fields = []
i = 1
for c in soup.components.find_all("comp"):
new_comp = collections.defaultdict(str)
new_comp['Reference'] = [c['ref']]
new_comp['Value'] = c.value.contents[0]
new_comp['Identifier'] = ''
new_comp['Package'] = ''
new_comp['Tolerance'] = ''
new_comp['Voltage'] = ''
new_comp['Current'] = ''
new_comp['Power'] = ''
new_comp['PN'] = ''
for f in c.find_all("field"):
new_comp[f['name']] = f.contents[0]
if f['name'] not in standard_fields:
if f['name'] not in extra_fields:
extra_fields.append(f['name'])
found = None
new_comp_no_ref = {}
for k in new_comp.keys():
if k != 'Reference':
new_comp_no_ref[k] = new_comp[k]
for c2 in comp.values():
c2_no_ref = {}
for k in c2.keys():
if k != 'Reference':
c2_no_ref[k] = c2[k]
if str(c2_no_ref) == str(new_comp_no_ref):
found = c2
c2['Reference'].append(c['ref'])
break
if found == None:
comp[i] = new_comp
i += 1
csv_file = os.path.splitext(xml_file)[0] + "_bom.csv"
with codecs.open(csv_file, "w", encoding='utf-8') as f:
f.write(
"Reference,Quantity,Identifier,Package,Value,Tolerance,Voltage,Current,Power,PN")
for c in extra_fields:
f.write("," + c)
f.write('\n')
ordered = []
for k in comp.keys():
c = comp[k]
ref = ';'.join(c['Reference'])
l = [ref, str(len(c['Reference'])), c['Identifier'], c['Package'], c['Value'],
c['Tolerance'], c['Voltage'], c['Current'], c['Power'], c['PN']]
for y in extra_fields:
l.append(c[y])
# for y in c.keys():
# if y not in standard_fields:
# l.append
l = ['"' + x + '"' for x in l]
f.write(','.join(l))
f.write('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment