Presidio Anonymizer package - replaces analyzed text with desired values.
—
The DeanonymizeEngine reverses anonymization operations when using reversible operators like encryption. It takes anonymized text and the original anonymization metadata to restore the original content.
Main deanonymization method that reverses anonymization operations using operator metadata.
def deanonymize(
self,
text: str,
entities: List[OperatorResult],
operators: Dict[str, OperatorConfig]
) -> EngineResult:
"""
Deanonymize text that was previously anonymized.
Parameters:
- text (str): The anonymized text to restore
- entities (List[OperatorResult]): Metadata from original anonymization
- operators (Dict[str, OperatorConfig]): Configuration for deanonymization operators
Returns:
EngineResult: Contains restored text and metadata about transformations
"""Usage Example:
from presidio_anonymizer import AnonymizerEngine, DeanonymizeEngine
from presidio_anonymizer.entities import RecognizerResult, OperatorConfig
# First, anonymize with reversible operators
anonymizer = AnonymizerEngine()
original_text = "My credit card is 4111-1111-1111-1111"
analyzer_results = [RecognizerResult("CREDIT_CARD", 18, 37, 0.9)]
# Use encryption for reversible anonymization
encrypt_config = OperatorConfig("encrypt", {"key": "my-secret-key-32-characters-long12"})
anonymize_result = anonymizer.anonymize(
text=original_text,
analyzer_results=analyzer_results,
operators={"CREDIT_CARD": encrypt_config}
)
print(f"Anonymized: {anonymize_result.text}")
# Now deanonymize
deanonymizer = DeanonymizeEngine()
decrypt_config = OperatorConfig("decrypt", {"key": "my-secret-key-32-characters-long12"})
deanonymize_result = deanonymizer.deanonymize(
text=anonymize_result.text,
entities=anonymize_result.items, # Use original anonymization metadata
operators={"CREDIT_CARD": decrypt_config}
)
print(f"Restored: {deanonymize_result.text}") # Original text restoredAdd or remove custom deanonymization operators at runtime.
def add_deanonymizer(self, deanonymizer_cls: Type[Operator]) -> None:
"""
Add a new deanonymizer to the engine.
Parameters:
- deanonymizer_cls (Type[Operator]): The deanonymizer class to add
"""
def remove_deanonymizer(self, deanonymizer_cls: Type[Operator]) -> None:
"""
Remove a deanonymizer from the engine.
Parameters:
- deanonymizer_cls (Type[Operator]): The deanonymizer class to remove
"""Usage Example:
from presidio_anonymizer.operators import Operator
class CustomDecrypter(Operator):
def operate(self, text, params):
# Custom decryption logic
return decrypt_with_custom_algorithm(text, params.get("key"))
deanonymizer = DeanonymizeEngine()
deanonymizer.add_deanonymizer(CustomDecrypter)Get list of all available deanonymization operators.
def get_deanonymizers(self) -> List[str]:
"""
Return a list of supported deanonymizers.
Returns:
List[str]: Names of available deanonymizer operators
"""Usage Example:
deanonymizer = DeanonymizeEngine()
available = deanonymizer.get_deanonymizers()
print(available) # ['decrypt', 'deanonymize_keep']Only certain operators support deanonymization:
Uses AES encryption with a secret key to enable full restoration.
# Anonymization
encrypt_config = OperatorConfig("encrypt", {
"key": "my-secret-key-32-characters-long12"
})
# Deanonymization
decrypt_config = OperatorConfig("decrypt", {
"key": "my-secret-key-32-characters-long12" # Must match
})Keeps text unchanged during both anonymization and deanonymization.
# Both operations use keep
keep_config = OperatorConfig("keep")
deanonymize_keep_config = OperatorConfig("deanonymize_keep")EngineResult.itemsitems) for later use# Step 1: Anonymize and save metadata
anonymize_result = anonymizer.anonymize(text, analyzer_results, operators)
anonymized_text = anonymize_result.text
anonymization_metadata = anonymize_result.items # Save this!
# Step 2: Later, restore original text
deanonymize_result = deanonymizer.deanonymize(
text=anonymized_text,
entities=anonymization_metadata, # Use saved metadata
operators=deanonymize_operators
)
original_text = deanonymize_result.textInstall with Tessl CLI
npx tessl i tessl/pypi-presidio-anonymizer