1

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.

1
  • Python can read files, process binary values and write files so of course it can do this. However, perhaps you are asking if python has a pre-built library that knows how to parse this data format? This seems like three questions in one when I look at it to try and help. What’s your experience with python and how far has your research gotten you opening a pages document in python? The pages app uses pretty standard data types so if you have your easy script ready, we might get you over the top chaining it to a tool to help unzip the file.
    – bmike
    May 16 '20 at 12:29
2

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.

1
  • 1
    I don't want to add a python tool named appscript which I haven't yet tested to this good answer, but stackoverflow.com/questions/1694060/… may be relevant if the OP wants to script the app in python as opposed to modify the files directly. I will add details on the iwa format now in use - it's compressed, streamed, encoded data for a few layers now in Pages.
    – bmike
    May 16 '20 at 14:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .