Python integration¶
Imparfait can be driven from a Python script on your computer: open a project, read its documents and chapters, write, search and replace, undo, export. The script goes through the same parts of the app as its screens, so what it does is exactly what you would have done by hand. Scripting in Python lists everything a script can do; this page installs it and runs a first script.
For people who write code
This is for writers and helpers at ease with a terminal and Python. Nothing here is needed to use Imparfait.
What you need¶
- A Mac, with Apple silicon or Intel, macOS 11 or later. Windows and Linux: coming.
- uv, the tool that installs Python and its packages:
curl -LsSf https://astral.sh/uv/install.sh | sh, orbrew install uv. See uv's documentation. - Python 3.12 or later: uv installs it for you when the commands below ask for it.
Install it¶
In a terminal, make a folder for your scripts and add Imparfait to it:
uv init --python 3.12 my-script
cd my-script
uv add imparfait --index imparfait=https://imparfait.io/documentation/python/simple/
uv picks the package made for your Mac and remembers where it comes from. To get a newer one later, from the same
folder: uv sync --upgrade-package imparfait.
Try it on a demo book¶
A script changes a project as you would, and the changes reach your other devices. Try it first on a demo book, made in
the folder of your scripts (en, fr, es, de or it, the language of the book):
uv run python -c "import imparfait; imparfait.demo_book('en', 'Demo book')"
Save this as demo.py in the same folder:
import imparfait
with imparfait.open() as project:
print("Project:", project.title)
print("Documents:")
for document in project.documents:
print(" ", document.kind, "-", document.name)
print("Chapters:")
for chapter in project.chapters():
print(" ", chapter.level, "-", chapter.title)
note = project.add_note("From a script", "Written by demo.py.")
print("A new note:", note.name)
book = project.manuscript
print("'lighthouse' appears", len(project.search("lighthouse")), "times")
replaced = book.replace_all("lighthouse", "beacon")
print("Replaced:", replaced.count)
book.undo()
print("Undone: 'lighthouse' appears", len(project.search("lighthouse")), "times")
print("Exported:", book.export("Export/book.pdf", format="pdf"))
print("Exported:", book.export("Export/book-markdown", format="md"))
Then run it on the demo book:
IMPARFAIT_PROJECT="Demo book" uv run demo.py
It prints the documents and chapters of the book, adds a note, replaces lighthouse everywhere then undoes it, and
exports the manuscript as a PDF and as Markdown into the folder Export.
Which project a script works on¶
- The folder given to
imparfait.open():imparfait.open("~/Documents/Imparfait Manuscripts/My book"). A project is the folder that holds a hidden.imparfaitfolder. - Otherwise, the folder
IMPARFAIT_PROJECTnames, as above: the same script then works on any project. - To find one,
imparfait.projects()lists the projects in the folder where the app keeps them (Imparfait Manuscripts, in your Documents), the latest changed first, each with itstitleandpath:
uv run python -c "import imparfait; [print(p.title, '-', p.path) for p in imparfait.projects()]"
With no folder and no IMPARFAIT_PROJECT, imparfait.open() stops with a NotFoundError that lists them too.
Close the project in the app first¶
A project is written by one Imparfait at a time on a device. While the app has it open, a script asking for it is refused
with OpenElsewhereError (the project is open in Imparfait), and nothing is written. Close it in the app (or quit the
app), run the script, then open it again: the app reads what the script changed.
Windows and Linux¶
Coming. For now the package is made for macOS only.