Component Declaration Status Python Script Generator (BOM CSV vs Supplier Declarations)
Not every team can query the ERP database directly. If you can export a BOM and a declarations list to CSV, this generator gives you a small script that finds missing, non compliant and expired declarations without any extra libraries.
Fill in
check_declarations.py
import csv
from datetime import date, datetime
BOM_FILE = "bom_export.csv"
DECL_FILE = "declarations.csv"
OUT_FILE = "compliance_gaps.csv"
PART_COL = "part_number"
STATUS_COL = "status"
DATE_COL = "declaration_date"
GOOD_VALUE = "Compliant"
MAX_AGE_DAYS = 730
def load_declarations(path):
result = {}
with open(path, newline="", encoding="utf-8-sig") as handle:
for row in csv.DictReader(handle):
part = row[PART_COL].strip().upper()
result[part] = row
return result
def age_in_days(text):
try:
return (date.today() - datetime.strptime(text.strip(), "%Y-%m-%d").date()).days
except ValueError:
return None
def main():
declarations = load_declarations(DECL_FILE)
gaps = []
with open(BOM_FILE, newline="", encoding="utf-8-sig") as handle:
for row in csv.DictReader(handle):
part = row[PART_COL].strip().upper()
decl = declarations.get(part)
if decl is None:
gaps.append([part, "MISSING", ""])
elif decl[STATUS_COL].strip() != GOOD_VALUE:
gaps.append([part, "NOT COMPLIANT", decl[DATE_COL]])
else:
age = age_in_days(decl[DATE_COL])
if age is None:
gaps.append([part, "BAD DATE", decl[DATE_COL]])
elif age > MAX_AGE_DAYS:
gaps.append([part, "EXPIRED", decl[DATE_COL]])
with open(OUT_FILE, "w", newline="", encoding="utf-8") as handle:
writer = csv.writer(handle)
writer.writerow(["part_number", "gap_reason", "declaration_date"])
writer.writerows(gaps)
print("Gaps found:", len(gaps), "written to", OUT_FILE)
if __name__ == "__main__":
main()Expected input layout
# BOM CSV header: part_number,description,qty # Declarations CSV header: part_number,status,declaration_date # Dates must be YYYY-MM-DD. Status text must match: Compliant
Uses only the Python standard library (3.8 or newer). Both CSV files need the part number column under the same name. Dates must be ISO format. Test on a small sample first and review the output manually before acting on it.
Automate declaration checks from ERP
Optional: we can discuss automating this check against live ERP and supplier data.
Optional. The tool and every export work without this. No spam.
How to use the script
Fill in the file names, the column header names in your exports, the text that means compliant and the maximum age in days. Copy the generated code into a file named check_declarations.py, place it in the same folder as the two CSV files and run it with Python. The result is a CSV that you can open in Excel and send to buyers or suppliers as a chase list.
Logic behind the checks
The script builds a dictionary of declarations keyed by upper case part number, then walks the BOM. A part with no entry is MISSING. A part whose status differs from your compliant text is NOT COMPLIANT. A compliant part whose date is older than the limit is EXPIRED, and a date that cannot be read is flagged as BAD DATE so bad data is never silently accepted. Normalising case and spaces avoids false gaps from inconsistent part numbers.
Extending it
Common additions are a second key for manufacturer part numbers, one declaration file per regulation, and an exemption column that lists the RoHS exemption relied on. You can also email the output to buyers or write it back through the ERP API. If you do automate writing back, keep a human approval step so a data error cannot change item status.
Common mistakes
Mismatched part numbers cause most false results: leading zeros lost in Excel, revision suffixes, and supplier versus internal numbering. Open CSVs in a text editor to check encoding, and keep a lookup of cross references. Also remember that a clean report only shows that a record exists, not that the supplier data is correct, so keep a risk based sampling and test program.
Frequently Asked Questions
Do I need to install any package?
No. The script uses the csv and datetime modules that ship with Python 3.8 and later, so it runs on a locked down workstation without installing anything from the internet.
What if my dates are in another format?
Change the format string in the age_in_days function, for example to %d/%m/%Y for day first dates. It is best to export dates in ISO format, which also sorts correctly.
Can it handle several regulations?
Run it once per regulation with a separate declarations file, or add a regulation column and filter on it. Keeping separate outputs makes it easier to assign follow up to different owners.
Is the output proof of compliance?
No. It only shows which parts have a declaration on file that meets your rule. Compliance still depends on the content and reliability of those declarations, supported by your verification and testing plan.
For a version that reads directly from your ERP and supplier portals, with logging and approvals, Netray can help design it.
Related Tools
BOM Compliance Declaration Gap SQL Query Generator
Builds a SQL query and a summary query that list BOM components without a current compliance declaration for a chosen regulation.
Compliance & ContractsRoHS Restricted Substances and Exemptions Reference
Searchable lookup of RoHS 2 substance limits, widely used Annex III exemptions and equipment categories.
Compliance & ContractsProduct Compliance Program Maturity Assessment
Ten questions that rate how well your manufacturing company manages product regulatory compliance, from ownership and data to change control and records.
Compliance & ContractsArticle Substance Mass Roll-Up Worksheet (REACH 0.1% w/w)
Enter each component mass, quantity and substance concentration to get total substance mass and the percentage of the whole article.
More Developer Tools tools
See all- SAP Sales Order ABAP SELECT Generator (VBAK, VBAP, VBFA)
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
- SAP ABAP Open Items SELECT Generator
- SAP ACDOCA SQL Query Generator
- SAP QM Inspection Lot ABAP SELECT Generator
- SAP Quality Notification Pareto SQL Query Generator
- SAP QM Usage Decision BAPI Skeleton
- ABAP Custom Exception Class Generator
- ABAP Unit Test Class Generator