Unable to use API to call bpy.ops.render.opengl() Ubuntu 18.04 with Blender 2.79

The goal is to load a 3D Model into Blender and create/save a .PNG file.  

Here is my script:

def preprocess(file):                                                                                   

    # remove mesh Cube                                                                                  
    if "Cube" in bpy.data.meshes:                                                                       
        mesh = bpy.data.meshes["Cube"]                                                                  
        print("removing mesh", mesh)                                                                    
        bpy.data.meshes.remove(mesh)                                                                    

    # import the 3D object                                                                              
    imported_object = bpy.ops.import_scene.obj(filepath=file)                                           

    # change the view to make certain the entire 3D object is viewable                                  
    for area in bpy.context.screen.areas:                                                               
        if area.type == 'VIEW_3D':                                                                      
            for region in area.regions:                                                                 
                if region.type == 'WINDOW':                                                             
                    override = {'area': area, 'region': region} 
                    bpy.ops.view3d.view_all(override)                                                   

    # Deselect all so selection highlights are not passed to .png                                                                                      
    bpy.ops.object.select_all(action='TOGGLE')                                                          


    bpy.ops.render.opengl()                                                                             

    bpy.data.images['Render Result'].save_render(filepath="scripted.png")                               


This script works until I get to the line:

bpy.ops.render.opengl()


This line appears to have no effect whatsoever.

When I am run the above command from the console manually by going to the Render -> OpenGL Render Image menu, Blender renders the object as expected and the console view is updated with a 'Render Result' view.

From what I have learned about the API, some commands must be executed within a certain context.  It seems like I need to let the script know which viewport is the active viewport, but this is purely speculation on my part.  

Any advice on how to render this 3D object via script would be appreciated.
The operator works correctly and will render your image. However with the default arguments it won't switch to the UV/Image Editor. If you intend to replicate the behavior of Render > OpenGL Render Image then you will have to change the execution context. In general buttons in the user interface use 'INVOKE_DEFAULT' as execution context.

bpy.ops.render.opengl('INVOKE_DEFAULT')


Additionally you can use the write_still=True argument to save the file. It will write the file to the path specified in bpy.context.scene.render.filepath. The last line in your script could then be removed.