Batch Change Material Link Type

Hi, I need some help here.

I’ve noticed that my objects have material linked to “object” and I want to change this to “data” Is there a way to change this apply this change to all objects? Maybe in python console?
It’ll took forever to change this for around 5,000 objects.

Thanks in advance.

Just an update for anyone with the same issue.

Thanks to bdancer for giving the solution!
Use text editor to paste this, then press “Run Script” or Alt+P.

import bpy

for ob in
    for slot in ob.material_slots:
        if slot.material:
            ma= slot.material
   = 'DATA'
            slot.material = ma