Is it necessary to convert a python script from bpy.context / bpy.ops to bpy.data to run it withou gui via command-line? I would like to run the following script via command-line and no gui:

import bpy
import os

# switch on nodes
scene = bpy.context.scene
scene.node_tree.nodes.active = scene.node_tree.nodes['Composite']
scene.render.use_compositing = True
tree = bpy.context.scene.node_tree

# assign a color
bg_col = [1.0,0.0,0.0,1.0]

# change value
bpy.data.scenes['Scene'].node_tree.nodes["color-background"].outputs[0].default_value = bg_col
print("new background color set to "+ str(bg_col))

# ----- LOAD IMAGE -----
filepath = bpy.data.filepath
directory = os.path.dirname(filepath)
#print(directory)

# Load a new image
mat = bpy.data.materials['LogoMaterial']
nodes = mat.node_tree.nodes
material_output = nodes.get("Material Output")

logo_texture = nodes["logo-texture"]
#load new logo texture
logo_texture.image = bpy.data.images.load(directory+"/tex//logo-2k.png", check_existing=True)
print("new logo texture added")

#create a sequence editor
if not scene.sequence_editor:
    scene.sequence_editor_create()

#clear the VSE, then add an audio file
scene.sequence_editor_clear()
my_context = bpy.context.area.type
#print(str(my_context))
my_context = 'SEQUENCE_EDITOR'
#print(str(my_context))
bpy.ops.sequencer.sound_strip_add(filepath="//audio/0A1.wav", files=[{"name":"0A1.wav", "name":"0A1.wav"}], relative_path=True, frame_start=1, channel=1)
print("loaded new sound")
my_context = 'TEXT_EDITOR'


If it is necessary, how would I go about converting it to only using bpy.data instead of bpy.context and bpy.ops?

Below is a part where I try to replace an audio file in the Video Sequence Editor just using bpy.data, however it only removes the audio datablock, loads a new audio, but does not associate the audio to the audiostrip in the VSE timeline.

if "0A1.wav" in bpy.data.sounds:
    sound = bpy.data.sounds["0A1.wav"]
    print("removing sound ", sound)
    bpy.data.sounds.remove(sound)
    print("loading new sound ", sound)
    bpy.data.sounds.load("/audio//0B.wav")
bpy.context works perfectly fine from the command line. Here is a set of scripts for doing useful things with Blender from the command line, and a couple of them (render-batch and apply-compositor) make references to bpy.context.

Note also the technique of wrapping the Python code in a shell script, so the user can conveniently invoke the scripts as though they were commands in their own right.