New Design Review SpecLib/BOM scripts/spectrum_bom_digikey.py

48 lines
1.3 KiB
Python

# BOM generation script for KiCAD (6+)
# Last update: 26/03/2025
# Author: Jorian Zwerver
# This scripts formats the BOM to the format of DIGIKEY
"""
@package
Generate a comma delimited list (csv file type) in DIGIKEY style.
Components are sorted by reference and grouped by value.
Fields are (if exist)
'Digikey MPN', 'Customer reference', 'Quantity'
Command line:
python "pathToFile/SpecLib/BOM scripts/spectrum_bom_digikey.py" "%I" "%O_DIGIKEY.csv"
"""
# Import libraries
from libs import kicad_netlist_reader
import csv
import sys
net = kicad_netlist_reader.netlist(sys.argv[1])
try:
f = open(sys.argv[2], 'w')
except IOError:
e = "Can't open output file for writing: " + sys.argv[2]
print(__file__, ":", e, sys.stderr)
f = sys.stdout
# Generate new CSV file with header
out = csv.writer(f, lineterminator='\n', delimiter=',', quotechar='\"', quoting=csv.QUOTE_ALL)
out.writerow(['Digikey MPN', 'Customer reference', 'Quantity'])
grouped = net.groupComponents()
for group in grouped:
refs = ", ".join(component.getRef() for component in group)
component = group[0]
if not component.getField("Digikey"):
continue
out.writerow([
component.getField("Digikey"),
refs,
len(group)
])