kifield/kifield.py
def extract_part_fields_from_wb(
    wb, inc_field_names=None, exc_field_names=None, recurse=False
):
    """Return a dictionary of part fields extracted from an XLSX workbook."""

    wb = ungroup_wb(wb)

    part_fields = {}  # Start with an empty part dictionary.

    try:
        ws = wb.active  # Get the active worksheet from the workbook.

        # Find the header with the part field labels.
        header_row, header = find_header(ws)

        # Find the column with the part references.
        refs_c, refs_lbl = find_header_column(header, "refs")

        # Get the list of part references.
        refs = [r.value for r in list(ws.columns)[refs_c - 1][header_row:]]

        # Make a dict of spreadsheet column indexes keyed by their field name.
        field_cols = {c.value: c.column for c in header}

        # Get the field names.
        field_names = list(field_cols.keys())
        # Keep only the allowed field names.
        field_names.remove(refs_lbl)  # Remove the part reference field.
        cull_list(field_names, inc_field_names, exc_field_names)
        # Update the dictionary so it only has the allowed names.
        field_cols = {f: field_cols[f] for f in field_names}

        # Get the field values for each part reference.
        for row, ref in enumerate(refs, header_row + 1):
            if ref is None:
                continue  # Skip lines with no part reference.

            # Get the field values from the row of the current part reference.
            field_values = {}
            for field_name, col in list(field_cols.items()):
                value = ws.cell(row=row, column=col).value
                if value is not None:
                    field_values[field_name] = value
                else:
                    field_values[field_name] = ""

            # Explode the part reference into its individual references and
            # assign the field values to each part.
            for single_ref in explode(ref):
                part_fields[single_ref] = field_values

    except FindLabelError:
        logger.warn("No references column found.")
        raise FieldExtractionError

    if logger.isEnabledFor(DEBUG_DETAILED):
        print("Extracted Part Fields:")
        pprint(part_fields)

    return part_fields
Return a dictionary of part fields extracted from an XLSX workbook.
Return a dictionary of part fields extracted from an XLSX workbook.
Return a dictionary of part fields extracted from an XLSX workbook .
def extract_part_fields_from_wb(
    wb, inc_field_names=None, exc_field_names=None, recurse=False
):
    wb = ungroup_wb(wb)
    part_fields = {}  
    try:
        ws = wb.active  
        header_row, header = find_header(ws)
        refs_c, refs_lbl = find_header_column(header, "refs")
        refs = [r.value for r in list(ws.columns)[refs_c - 1][header_row:]]
        field_cols = {c.value: c.column for c in header}
        field_names = list(field_cols.keys())
        field_names.remove(refs_lbl)  
        cull_list(field_names, inc_field_names, exc_field_names)
        field_cols = {f: field_cols[f] for f in field_names}
        for row, ref in enumerate(refs, header_row + 1):
            if ref is None:
                continue  
            field_values = {}
            for field_name, col in list(field_cols.items()):
                value = ws.cell(row=row, column=col).value
                if value is not None:
                    field_values[field_name] = value
                else:
                    field_values[field_name] = ""
            for single_ref in explode(ref):
                part_fields[single_ref] = field_values
    except FindLabelError:
        logger.warn("No references column found.")
        raise FieldExtractionError
    if logger.isEnabledFor(DEBUG_DETAILED):
        print("Extracted Part Fields:")
        pprint(part_fields)
    return part_fields
def extract_part_fields_from_wb ( wb , inc_field_names = None , exc_field_names = None , recurse = False ) : wb = ungroup_wb ( wb ) part_fields = { } try : ws = wb . active header_row , header = find_header ( ws ) refs_c , refs_lbl = find_header_column ( header , "refs" ) refs = [ r . value for r in list ( ws . columns ) [ refs_c - 1 ] [ header_row : ] ] field_cols = { c . value : c . column for c in header } field_names = list ( field_cols . keys ( ) ) field_names . remove ( refs_lbl ) cull_list ( field_names , inc_field_names , exc_field_names ) field_cols = { f : field_cols [ f ] for f in field_names } for row , ref in enumerate ( refs , header_row + 1 ) : if ref is None : continue field_values = { } for field_name , col in list ( field_cols . items ( ) ) : value = ws . cell ( row = row , column = col ) . value if value is not None : field_values [ field_name ] = value else : field_values [ field_name ] = "" for single_ref in explode ( ref ) : part_fields [ single_ref ] = field_values except FindLabelError : logger . warn ( "No references column found." ) raise FieldExtractionError if logger . isEnabledFor ( DEBUG_DETAILED ) : print ( "Extracted Part Fields:" ) pprint ( part_fields ) return part_fields
Return a dictionary of part fields extracted from an XLSX workbook.
Return a dictionary of part fields extracted from an XLSX workbook .
"""Return a dictionary of part fields extracted from an XLSX workbook.""" # Start with an empty part dictionary. # Get the active worksheet from the workbook. # Find the header with the part field labels. # Find the column with the part references. # Get the list of part references. # Make a dict of spreadsheet column indexes keyed by their field name. # Get the field names. # Keep only the allowed field names. # Remove the part reference field. # Update the dictionary so it only has the allowed names. # Get the field values for each part reference. # Skip lines with no part reference. # Get the field values from the row of the current part reference. # Explode the part reference into its individual references and # assign the field values to each part.
wb
inc_field_names
exc_field_names
recurse