如何使用 Python 读取 KiCAD XML BOM

此脚本使用 beautifulsoup4 读取 XML BOM(在 kicad-cli 中也称为 python-bom),并作为使用示例生成 RefDes 到封装名称的字典:

read_kicad_bom.py
from bs4 import BeautifulSoup

def extract_footprints_by_ref_from_xml(filename):
    footprints_by_ref = {} # e.g. "C3" => "Capacitor_SMD:C_0603_1608Metric"

    with open(filename, "r") as infile:
        soup = BeautifulSoup(infile, features="xml")

    for component in soup.export.components.findChildren("comp"):
        refdes = component.attrs["ref"]
        footprints_by_ref[refdes] = component.footprint.text

    return footprints_by_ref

Check out similar posts by category: KiCad, Python