You've already forked csv_to_excel
94 lines
2.9 KiB
Python
Executable File
94 lines
2.9 KiB
Python
Executable File
#! /usr/bin/env python3
|
|
|
|
# Generate a BOM from a PrjPcb/DSN/SDAX file.
|
|
# For more information, read the README file in this directory.
|
|
|
|
import argparse
|
|
import csv
|
|
import logging
|
|
import os
|
|
import yaml
|
|
import sys
|
|
from contextlib import ExitStack
|
|
import openpyxl
|
|
from openpyxl.styles import Font, Border, Side, PatternFill
|
|
from openpyxl.utils import get_column_letter, range_boundaries
|
|
from openpyxl import load_workbook
|
|
from openpyxl.utils.cell import coordinate_from_string, column_index_from_string
|
|
|
|
from allspice import AllSpice
|
|
from allspice.utils.bom_generation import generate_bom, ColumnConfig
|
|
|
|
logger = logging.getLogger(__name__)
|
|
handler = logging.StreamHandler(sys.stderr)
|
|
handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
|
|
logger.addHandler(handler)
|
|
|
|
|
|
def insert_csv_into_excel(input_csv_path, start_cell, output_excel_path):
|
|
# Load the header Excel workbook
|
|
# wb = load_workbook(header_excel_path)
|
|
wb = openpyxl.Workbook() # Create a new workbook if header is not provided
|
|
ws = wb.active
|
|
|
|
# Parse starting cell
|
|
col_letter, row_number = coordinate_from_string(start_cell)
|
|
start_col = column_index_from_string(col_letter)
|
|
start_row = row_number
|
|
|
|
# Read and write CSV into specified position
|
|
with open(input_csv_path, newline='', encoding='utf-8') as csvfile:
|
|
reader = csv.reader(csvfile)
|
|
for r_idx, row in enumerate(reader):
|
|
for c_idx, value in enumerate(row):
|
|
ws.cell(row=start_row + r_idx, column=start_col + c_idx, value=value)
|
|
|
|
# Save result
|
|
wb.save(output_excel_path)
|
|
print(f"✅ BOM added to: {output_excel_path}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(
|
|
prog="csv_to_excel", description="Generate a .xlsx file from a file.csv and header.yml"
|
|
)
|
|
parser.add_argument(
|
|
"--source_path",
|
|
help=(
|
|
"The path to the source .csv file used to generate the out_file.xlsx"
|
|
),
|
|
)
|
|
parser.add_argument(
|
|
"--start_cellname",
|
|
help=(
|
|
"The cell name in the .xlsx file where the CSV data will be inserted. "
|
|
"For example, 'A1' to start at the first cell."
|
|
),
|
|
default="A1",
|
|
)
|
|
parser.add_argument(
|
|
"--output_file",
|
|
help="The path to the output file. If absent, the CSV will be output to the command line.",
|
|
)
|
|
parser.add_argument(
|
|
"--log-level",
|
|
help="The log level for the logger. Defaults to INFO.",
|
|
default="INFO",
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
logger.setLevel(args.log_level.upper())
|
|
logger.info("Running generate-bom action.")
|
|
logger.debug("Arguments: %s", vars(args))
|
|
|
|
logger.info("Generating output .xlsx file...")
|
|
|
|
insert_csv_into_excel(
|
|
input_csv_path=args.source_path,
|
|
start_cell=args.start_cellname,
|
|
output_excel_path=args.output_file if args.output_file else "output.xlsx",
|
|
)
|
|
|
|
logger.info("Generated file.")
|