Skip to content

Scripting Imparfait in Python

Everything Imparfait does to a project can be done without its window, from Python: read and write the documents, comment, suggest, lock, tag dates and names, decide what the app proposes, make sheets and notes, save versions, go back to one, export. The screens are one way to do it; a script is another, and it goes through the same parts of the app, so a project worked on by a script is exactly as if you had done it by hand.

For people who write code

This is for writers and helpers who are at ease with Python. Nothing here is needed to use Imparfait.

Before you start

  • Close the project in the app. A project is written by one Imparfait at a time on a device: a script asking for a project the app has open is refused, with the project is open in Imparfait, and nothing is written.
  • Work on a copy first. A script changes your project as you would, and the changes reach your other devices. Try it on a demo book: imparfait.demo_book("en", "~/Desktop/Demo") builds one, in English, French, Spanish, German or Italian.

Install it

On a Mac, Python integration installs it with uv in three commands and runs a first script. From the Imparfait source, it is built with:

cd rust
make imparfait-python          # builds it into apps/imparfait/python/.venv
make imparfait-python-demo BOOK=en

A first script

import imparfait

with imparfait.open("~/Documents/Imparfait Manuscripts/My book") as project:
    book = project.manuscript
    for chapter in project.chapters():
        print(chapter.level, chapter.title)

    passage = book.find("came along the shore road")
    passage.comment("Do we know why she came back?", on="came along")
    passage.suggest("walked", on="came")

When the with block ends, the project is left as the app leaves it: everything is saved, and the app reads what changed the next time it opens it.

What a script can do

In the app In Python
The documents of a project project.documents, project.manuscript, project.notes, document.passages
Typing passage.replace(text, on="words"), passage.insert(text, at=0), document.append(text, kind="title")
Undo and redo document.undo(), document.redo(), project.undo_world()
Search, Replace all project.search(words), project.replace_all(words, with_), document.replace_all(...)
A new note project.add_note("Ideas", "Its first line")
Comments passage.comment(text, on=...), comment.reply(text), comment.resolve()
Suggestions passage.suggest(text, on=...), document.suggestions, suggestion.accept(), .refuse()
Locks passage.lock_it(title, reason), project.locks, lock.unlock()
Sheets of the World project.sheets, project.add_sheet("Odette", "character"), sheet.set("born", "1931"), sheet.add_facet(...)
Links and relations passage.link(sheet, on="Odette"), project.add_relation(odette, "parent", yann)
Dates passage.tag_date(on="3 May 1950", propose=True), tag.accept()
To check project.proposals(), then proposal.validate(...), .ignore() or .refuse(), each with its .undo()
Versions project.save_version(title), project.versions(), document.go_back(version)
Exporting document.export(path, format="pdf"): pdf, epub, docx, html, md, txt
Who has access with account=True: project.people, project.give_access(email, "can-write"), person.change("can-read"), person.remove(), project.leave()
Feedback to the team imparfait.feedback().send(text, files=[...]), .tickets, ticket.messages, ticket.reply(text)

Words are found by what they say (on="Odette", and occurrence=1 for the second time), or by where they are, in characters (at=(0, 6)).

When something cannot be done

A script is told the way the app would tell you, as an error it can catch:

  • imparfait.OpenElsewhereError: the project is open in the app.
  • imparfait.LockedError: what would be written stands in a lock. Nothing was written.
  • imparfait.NotFoundError: no document, passage, sheet or version goes by that name.
  • imparfait.InvalidError: what was asked does not read as it should (words that are not a date, a format the app does not export).
try:
    passage.insert("One more word. ")
except imparfait.LockedError as refused:
    print("Locked:", refused)

Your account

A script opens a project as a device with no account, unless asked: imparfait.open(path, account=True) starts your account as the app does, which a project kept online needs to send what the script wrote. project.account says whether you are signed in, and never shows your password or anything that would let someone in.