Edit pages document programmatically
Solution 1:
There are two approaches you can take.
OSA / AppleScript
Use macOS's Open Scripting Architecture (OSA) to interact with the Pages application.
Apple continue to support AppleScript within Pages. You may find this approach is enough, with iWork Automation's helpful examples.
Decompress, Edit, and Recompress
Alternatively, you can directly edit the Pages document using a custom script.
Pages documents used to be a compressed archive of XML and supporting files. More recent iWork documents (Pages, Numbers and Keynote) use iwa named files which are also documented but compressed versions of the data.
From the excellently documented open source project iWorkFileFormat comes this description:
Components are serialized into .iwa (iWork Archive) files, a custom format consisting of a Protobuf stream wrapped in a Snappy stream
If this sort of technical scripting is your idea, start by unzipping the file, apply your script to the resulting data files, and recompress.