You’re already almost there- just copy this line and change the parameters for a different node: Matout = matnodes['Material Output'].inputs[0]
For example,
Here is my scene. I am actually trying to bake lighting/texture for my object
I needed to add image texture to all existing materials on my object then bake (the code in the image). I then removed unused nodes (hence only one material remaining in the scene)
Alt that is left is to connect the image texture to the output.