How to automatically rotate an object on its local z axis to make one of its edges parallel to that of another object
Via a script.

Getting the angle is relatively simple here. The 3d vector v projected onto Z = 0 plane is simply v.xy ie its x and y coordinates.  The signed angle between two 2d vectors is
v1.angle_signed(v2)

Related Face edges angle - python re projecting into any plane, eg the face normal of target plane.
Once we have this angle can rotate the target mesh by it about its Z axis.
Example script.
Here is an example script with both objects in edit mode. The last selected edge also makes the object the target object to be rotated.  Note, this relies on the two objects NOT sharing a mesh.  (If they did there would be no way to select a different edge in each while both in edit mode)
import bpy
import bmesh
from mathutils import Matrix
from itertools import product
context = bpy.context

ob = context.object
bm = bmesh.from_edit_mesh(ob.data).copy()
bm.transform(ob.matrix_world)
e0 = bm.select_history.active
mw0 = ob.matrix_world
# find closest verts
for o  in context.selected_objects:
    if o is ob or o.type != 'MESH':
        continue
    bm1 = bmesh.from_edit_mesh(o.data).copy()
    bm1.transform(o.matrix_world)
    e1 = bm1.select_history.active
    # find the two closest verts 
    v0, v1 = sorted(
            product(e0.verts, e1.verts), 
            key= lambda e: (e[1].co - e[0].co).length
            ).pop()
    angle = (e0.other_vert(v0).co - v0.co).xy.angle_signed((v1.co - e1.other_vert(v1).co).xy)
    print(angle)
    # rotate angle about ob origin
    R = (
        Matrix.Translation(mw0.translation) @
        Matrix.Rotation(-angle, 4, 'Z') @
        Matrix.Translation(-mw0.translation)
    )
    ob.matrix_world = R @ mw0