New Design Review SpecLib/BOM scripts/spectrum_bom_farnell.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 FARNELL
"""
@package
Generate a comma delimited list (csv file type) in FARNELL style.
Components are sorted by reference and grouped by value.
Fields are (if exist)
'Part number', 'Quantity', 'Line Note'
Command line:
python "pathToFile/SpecLib/BOM scripts/spectrum_bom_farnell.py" "%I" "%O_FARNELL.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(['Part number', 'Quantity', 'Line Note'])
grouped = net.groupComponents()
for group in grouped:
refs = ", ".join(component.getRef() for component in group)
component = group[0]
if not component.getField("Farnell"):
continue
out.writerow([
component.getField("Farnell"),
len(group),
component.getDescription()
])