Edit pages document programmatically

automationpages

I want to know if it's possible to edit pages document using Python or other scripting tools.
I need to modify the paragraph style of lines containing a date (e.g. 2020/05/02), which can be easily done by a script. But I don't know how to do it in Pages.

Best Answer

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.