How do I link asset library materials and replace local materials?

How do I loop over objects in a file and replace materials with corresponding asset library materials?

Setup:
I have a bunch of models in separate files where their materials are local.
I also have a material asset library, containing the same materials, but not yet tied to each model.
I now want to open each model file and run a script that finds e.g. that there is a ‘Red’ in the asset library, and replace the local ‘Red’ currently used by the model with a linked ‘Red’ that is in the material assets file.

Problem:
I am able to open my asset library, and I can see the names of the materials, but I cannot find a way to get slot.material = “linked material”. The material properties, in the GUI, has a LF Red, that I am unable to find in bpy.data.materials. On trying to import, blender complains about ID MARed is alread linked, but it only complains for some (one?) materials, even though several others are previously linked. It also does not seem to like how I go about getting materials to data_to from data_from.
Thus, I seem to have both LF Red and MARed, but formally I can only find Red - and am unable to tell my slot to change to my linked material. I am also stumped on how to better get materials from data_from to data_to, when all I have is strings of material names.

This is what I have so far:

import bpy
from pathlib import Path

# Find un-linked material slots
SlotsToChange = []
materialsToLink = []
for obj in bpy.data.objects:
    for slot in obj.material_slots:
        if not slot.material.library:
            SlotsToChange.append(slot)
            if slot.material.name not in materialsToLink:
                materialsToLink.append(slot.material.name)


# Load asset file, find corresponding indices to slots that don't have a linked material an try to bring those
assetFile = 'C:\Materials.blend'
with bpy.data.libraries.load(str(assetFile), assets_only = True, link = True) as (data_from, data_to):                
                idList = []
                for slot in SlotsToChange:
                    if slot.material.name in data_from.materials:
                        id = data_from.materials.index(slot.material.name)
                        if not id in idList:
                            idList.append(id)

                for mat in materialsToLink:
                    data_to.materials = [name for name in data_from.materials if name==mat]
                    
                #Here: How to set slot.material = data_from.materials[slot.material.name]???

ChatGPT to the rescue! Turns out that there is a parameter name_full in addition to just name that is exactly what is needed when materials with the same name belong to two different data blocks.
Local material Red can then be compared to linked material Red [ExternalFile.blend]

My code is messy, but to whomever it helps:
I wanted to specifically state what file and library to link from, and I wanted an exception list for materials I wanted to keep local to the file (i.e. adding Term1, Term2 or whatever is in the list to the material name will exclude them from linking). Material slots are then searched, corresponding materials are linked (with no care about whether they actually exist in the library or not) and the slots are again looped to have materials replaced.

import bpy
import os
from pathlib import Path

actFile = 'ExternalFile.blend'
actLib = 'MyLib'
exceptTerms = ['Term1', 'Term2', 'Term3']

prefs = bpy.context.preferences
filepaths = prefs.filepaths

def findPath(name, path):
    for root, dirs, files in os.walk(path):
        if name in files:
            return os.path.join(root, name)

# list material slots that use local materials
print('\n', 15*'=', ' List local materials ', 15*'=', sep='')
localSlots = []
nameList = []
for obj in bpy.data.objects:
    for slot in obj.material_slots:
        if not slot.material.library:
            for exc in exceptTerms:
                if exc in slot.material.name:
                    print('Skipping ', slot.material.name)
                    continue
                else:
                    print('Local mat ', slot.material.name, ' in obj ', obj.name, sep='')
                    localSlots.append(slot)
                    if slot.material.name not in nameList:
                        nameList.append(slot.material.name)
print('\n', len(nameList), ' local materials listed (', len(bpy.data.materials), ' materials total)', sep='')

# Link materials
if localSlots:
    counter = 0
    print('\n', 15*'=', ' Attempt to link materials ', 15*'=', sep='')
    asset_library = filepaths.asset_libraries.get(actLib)
    library_path = Path(asset_library.path)
    blendFile = findPath(actFile, library_path)
    with bpy.data.libraries.load(blendFile, assets_only = True, link = True) as (data_from, data_to):
        for mat in data_from.materials:
            if mat in nameList:
                print(mat)
                data_to.materials.append(mat)
                counter = counter + 1
    print('\nSuccessfully linked ', counter, ' materials (', len(bpy.data.materials), ' materials total)', sep='')

# Replace materials
if localSlots:
    print('\n', 15*'=', ' Replacing materials ', 15*'=', sep='')
    for slot in localSlots:
        for mat in bpy.data.materials:
            if (slot.material.name_full + ' [' + actFile + ']') == mat.name_full:
                print('Replacing Obj ',obj.name, ' material ',slot.material.name_full, ' with ', mat.name_full, sep='')
                slot.material = mat