Well, after reading your comment that loading the image should only effect the layer it's loaded into, I realized I had just been assuming that was my problem, and hadn't actually confirmed it. Turns out it has nothing to do with loading the external image, or apparently anything else in my script. I can comment out everything except loading the initial base image and saving the png at the end, and I still get the enlarged transparent background behind the base image.
So I assume it must be the base image I'm using, the way I'm loading it, or the way I'm exporting at the end. I tried opening the image in the gui, and using the 'export to' function, and it created a png file as expected, so I don't think the base image is the cause.
In summary, the act of loading the xcf file from a script, then saving it to a png is enlarging the borders of the image. Do I need to specify the size when I'm saving or something?
Here's my code:
# Open file
im = pdb.gimp_file_load(fname_base, fname_base )
... script stuff ...
# Save
merged = pdb.gimp_image_merge_visible_layers(im, 0)
outname = "{}{}.png".format(prefix ,title_text.replace(' ', '_'))
full_output_filename = os.path.join(outdir, outname)
pdb.file_png_save_defaults(im, merged, full_output_filename, full_output_file
EDIT: Problem solved. It was the merge_visible command. I passed a 0 for the merge type, which expands as necessary. I had a one point manually pasted in an external image to the base image, so one of the layers must've gotten expanded to exactly it's size (which is why I thought that was what was happening in the script). It's not visible in the editor so I didn't notice it. Just changing the merge type to 1 fixed my problem. I should also probably fix the base image and figure out which layer is too big.