For anyone stumbling across this in 2025, I've used the above code and it worked brilliantly (thanks Antoine), but there a couple of small changes required for Godot 4.3 (I've yet to upgrade to 4.4 so can't comment there):
### This
var reference_frames: SpriteFrames = $AnimatedSprite.frames
### is now
var reference_frames: SpriteFrames = $AnimatedSprite.sprite_frames
### And this
var updated_texture: AtlasTexture = reference_frames.get_frame(animation, i).duplicate()
### is now
var updated_texture: AtlasTexture = reference_frames.get_frame_texture(animation, i).duplicate()
I'm pleased I found this post as a lot of posts I found simply said to use a Sprite2D with animation player, but I was stubborn and knew there must be a way of swapping out sprite sheets in code for AnimatedSprite2D.