Anonymize documents before sharing them with an AI, then restore the AI's output using a local encrypted vault.
Persona replaces sensitive values (people, companies, identifiers, addresses) with short placeholders such as [PERSONA_1] or [AZIENDA_2]. The mapping is stored in an encrypted per-project vault on your machine. Once an AI has worked on the anonymized text, persona restore puts the original values back.
document --anonymize--> [PERSONA_1] signed with [AZIENDA_2] --> AI --> edited text --restore--> usable document
| ^
+---------- encrypted per-project vault (stays on your machine) -----------+
Supported inputs: .docx, .xlsx, .txt, .md and text-based .pdf (read as extracted text, written as .txt).
- Short, readable placeholders (
[PERSONA_1],[AZIENDA_2],[IBAN_1]): the kind tells the AI what the thing is, so it can write around it naturally. - One vault per project: the same value is always the same placeholder, across all documents of a client. The vault is a single AES-256-GCM file (Argon2id key) holding the mapping and your glossary.
- Detection in layers: your glossary (highest recall: you know your clients), checksum-validated identifiers (IBAN, codice fiscale, P.IVA, e-mail), phone numbers, profile/web URLs, and heuristics for names (titles, a list of common Italian first names in either order,
Mario RossiorRossi Mario, labels likeCognome:, names hidden in e-mail addresses and profile URLs such as LinkedIn or GitHub), company suffixes, institutions (Liceo …,Cooperativa …) and street addresses. A value found once is masked everywhere, including in later documents of the project. - Column headers in tables: in XLSX sheets and DOCX tables, every non-empty cell below a header such as
Nome,Cognome,Cliente,Email,Telefono,Codice Fiscale,P.IVAorIndirizzo(or the English equivalentsName,Customer,Phone,Tax code,VAT,Address, ...) is masked whole with the matching kind, even when no detector recognises the value. - Review before masking (
--review): each distinct value is shown with its context; you can mask it, skip it this time, or mark it never mask (kept in the vault, so the same false positive never returns, seepersona allow). At the end you can type names the detectors missed and they go into the glossary.--exclude TEXTskips a value for one run. Values you chose to leave in do not trigger the safety check. - Safety check before sharing: the file that was actually written is re-opened and re-scanned for every known value and for anything the detectors still find. If something is left, nothing is written (
--forceoverrides). - DOCX goes deep: body, tables, headers, footers, footnotes/endnotes, comments, text boxes, tracked deletions, field codes (
HYPERLINK "mailto:…"), hyperlink targets, image alt text, custom XML and document properties are all analysed. Formatting is kept: a placeholder inherits the run it starts in, even when the name was split across differently formatted runs. Who-made-the-file traces are scrubbed: author / last-modified-by / company, comment and revision authors, the page thumbnail and the people list. Scrubbed fields are not restored. - XLSX goes deep too: shared and inline strings (rich text included), formulas, cached results, sheet names and defined names (renamed consistently, bracket-less
AZIENDA_1because Excel forbids[ ]in sheet names; formulas pointing at them are updated the same way), hyperlinks, headers/footers, validation texts, legacy and threaded comments, table column names, document properties. Numbers stay numbers; layout, charts and styles are untouched because only text nodes are edited. Phonetic guides of masked strings are dropped (they would reveal the original). - Restore works on any text, not on a specific file: the AI never returns the file you sent. Placeholders are matched tolerantly (case, brackets, markdown escapes like
\[PERSONA\_1\]) and the report lists placeholders the AI invented, dropped or altered.
The pipeline is segments -> detectors -> propagate -> resolve overlaps -> (review) -> apply -> verify. Restore works on plain text, because the file an AI returns is not the file that was sent. Package layout:
persona/detect: glossary, validated identifiers, rule-based and heuristic detectors.persona/documents.py,persona/office_docx.py,persona/office_xlsx.py,persona/ooxml.py: file adapters.persona/engine.py,persona/review.py,persona/restore.py: analysis, interactive review, restore.persona/vault.py: encrypted vault (AES-256-GCM, Argon2id key).persona/cli.py: thepersonacommand.
Details and trade-offs: docs/design.md.
Requires Python 3.11 or later.
pip install -e . # use ".[pdf]" to enable PDF input
persona init -p acme # create the encrypted vault
persona glossary add "Tessitura Valdarno S.r.l." -k AZIENDA -a Tessitura -p acme
persona anonymize contract.md -p acme --dry-run # show what would be masked
persona anonymize contract.md -p acme --review # review each detection, add what was missed
persona anonymize contract.md -p acme # writes contract.anon.md after a safety check
# work on contract.anon.md with an AI, save its answer as answer.md
persona restore answer.md --sent contract.anon.md -p acme # writes answer.restored.mdPERSONA_PASSWORD: vault password. If unset, it is asked interactively.PERSONA_NEW_PASSWORD: new password forpersona rekey. If unset, it is asked interactively.PERSONA_HOME: base directory for vaults. Default:~/.persona. Vaults are stored in<PERSONA_HOME>/projects/<project>.vault.--vault PATH: use an explicit vault file instead of--project.
persona anonymize offer.docx -p acme # offer.anon.docx, formatting kept
persona anonymize clients.xlsx -p acme # clients.anon.xlsx
persona anonymize cv.pdf -p acme # cv.anon.txt
persona verify file.md -p acme # check a file for sensitive data before sharing
persona allow add "Aurora Borealis" -p acme # never mask this value
persona glossary list -p acme
persona rekey -p acme # change the vault passwordBatch and pipes:
persona anonymize contracts/ -p acme # every supported file in the folder
persona anonymize contracts/ -p acme -r -o shared/ # include subfolders, mirror them under shared/
persona anonymize contracts/ -p acme -r --json # machine-readable report on standard output
cat note.txt | persona anonymize --stdin -p acme > note.anon.txt
persona anonymize note.md -p acme --stdout # text inputs only (.txt, .md, .pdf)In a directory, hidden files and earlier outputs (*.anon.*, *.restored.*) are skipped, unsupported files are reported as skipped, and a file that fails the safety check does not stop the others. The exit code is 2 if any file was blocked by the safety check, 1 if any file could not be processed, 0 otherwise. The --json report contains paths, statuses and counts only, never the detected values; run without --json to see them. --review cannot be combined with --stdin or --json, and is ignored (with a warning) together with --dry-run.
Run persona --help and persona <command> --help for all options (--out, --exclude, --force, --review, --dry-run, --recursive, --json, --stdin, --stdout).
Text files are read as UTF-16/32 when their BOM says so, otherwise as UTF-8 (a BOM is accepted and written back) or, failing that, as Windows-1252. Line endings are preserved as they are. Output is always UTF-8. When --stdin reads from a pipe and no terminal is available (CI, cron), the vault password must come from PERSONA_PASSWORD: there is nowhere to ask for it.
anonymize_text, restore_text, verify and Vault are the stable Python API:
from persona import Vault, anonymize_text, restore_text, verify
vault = Vault.create("acme.vault", "password") # or Vault.open("acme.vault", "password")
censored, result = anonymize_text("Il Dott. Mario Rossi scrive a m.rossi@acme.example", vault)
vault.save() # store the mapping before sharing `censored`
assert verify({"text": censored}, vault) == [] # nothing sensitive left
report = restore_text("Risposta per [PERSONA_1].", vault)
print(report.text) # "Risposta per Mario Rossi."
print(report.invented, report.missing, report.altered)Measured on the synthetic corpus in tests/corpus (16 documents, 152 sensitive values; run pytest tests/test_corpus.py -s). Nine documents are Italian business texts (contracts, e-mail, minutes, invoice, CV…); seven are a "hard set" with e-mail threads, foreign invoices and addresses (DE/ES/UK), EU VAT numbers, a declaration with birth date / plate / ID number, minutes with surnames only, an English contract, a participant list with surname-first names and foreign phone numbers written without +.
| Without glossary | With glossary | |
|---|---|---|
| E-mail, phone (IT and international), IBAN, codice fiscale, VAT/P.IVA, addresses (IT/DE/ES/FR/UK), URLs, birth date / plate / ID number | 84/84 | 84/84 |
| Companies and institutions | 24/26 | 26/26 |
| People | 38/42 | 42/42 |
| Total | 146/152 | 152/152 |
What still needs the glossary: companies and schools without a legal suffix or known keyword (Officine Digitali Veronesi), and a bare first name with no surname beside it (Ciao Francesca) unless the full name appears elsewhere in the same project. Cities are deliberately not masked unless they are part of an address with a postcode.
How much to trust these numbers: the hard set scored 43/58 before its rules existed and 57/58 after, but the rules were written after looking at those misses, so that set is now seen data. The corpus is author-written; treat the numbers as a regression ratchet, not a guarantee. The only independent test so far is one real PDF CV (not committed): address, phone, e-mail, name, LinkedIn/GitHub URLs and a school were caught with no setup; four employers/schools without a suffix needed glossary entries. Always review before sending real data.
Known over-masking (by design, never the other way round): a capitalised first name followed by a capitalised word is treated as a person (Aurora Borealis, Victoria Station), Dr. House is a person, a real street name in running text is an address.
- Text inside images, charts, SmartArt, pivot tables, drawings/shapes, external links, data connections and macros is not anonymized. Persona warns when a DOCX/XLSX contains them; check them by hand.
- Identifiers stored as numbers in a spreadsheet (a phone or P.IVA typed as
3384567890) cannot be detected as text. Persona warns with the cells; mask them by hand. - Removing names does not remove identifiability from context ("the only supplier of X in Y").
- If the AI derives new forms (
M. Rossi, an e-mail built from a name) they are not in the vault and are not restored. - If the original text already contains something that looks like
[PERSONA_1], restore will treat it as a placeholder. - The same entity written in different ways (
ACME S.R.L./Acme S.r.l.) gets different placeholders, so restore gives back exactly what was written. - DOCX/XLSX output was checked with python-docx / openpyxl, XML well-formedness and zip integrity, not with Word or Excel themselves: open the result once before relying on it.
The vault is encrypted at rest, and anonymize re-reads the written file and checks it before declaring it shareable. Detection is heuristic and not exhaustive: review the output before sending real data. See SECURITY.md for the threat model and how to report vulnerabilities.
pip install -e ".[dev]"
pytest
pytest tests/test_corpus.py -s
ruff check .CI runs ruff check and the test suite on Linux and Windows. See CONTRIBUTING.md.
Early rewrite (version 0.2.0). The engine and CLI work for the formats listed above.
Planned:
- Optional local-LLM / NER detector for names (asked for text, never offsets), measured against the corpus.
- OCR for scanned PDFs and images. PDF output stays out of scope: the AI never needs a PDF back.
Built with AI-assisted workflows; design decisions and review by the author.
MIT. See LICENSE.