To approve or reject each change during the redaction process, or to keep a full log of the changes, give RedactorSettings a redaction callback. It is the Python side of the IRedactionCallback interface: an object with an accept_redaction(description) method, or a function that takes description. It is called once for each change the engine is about to make, and returns True to make the change or False to skip it.
redaction_type and action_type: what kind of redaction it is and what it is about to do;
original_text: the matched text;
replacement: for a text replacement, the original and the replacement text;
image_area_replacement: for an image area, the fill options.
Log every change
The following callback prints each change and accepts it:
fromgroupdocs.redactionimportRedactorfromgroupdocs.redaction.optionsimportLoadOptions,RedactorSettingsfromgroupdocs.redaction.redactionsimportExactPhraseRedaction,ReplacementOptionsclassRedactionDump(object):defaccept_redaction(self,description):print("%s redaction, %s action, item %s."%(description.redaction_type.name,description.action_type.name,description.original_text))ifdescription.replacementisnotNone:print("Text %s is replaced with %s."%(description.replacement.original_text,description.replacement.replacement))# Return False here to skip this changereturnTruedefuse_redaction_callback():settings=RedactorSettings(RedactionDump())withRedactor("./sample.docx",LoadOptions(),settings)asredactor:redactor.apply(ExactPhraseRedaction("Emily Johnson",ReplacementOptions("[personal]")))result_path=redactor.save()print("Document redacted. Output saved to "+result_path)if__name__=="__main__":use_redaction_callback()
sample.docx is the sample file used in this example. Click here to download it.
RedactorSettings is the third argument of Redactor, after the load options.
Skip some of the changes
A plain function works as well. The one below accepts only the first match. The redaction’s entry in the change log then reports PARTIALLY_APPLIED, with the reason in its error message:
An exception raised in the callback does not stop apply(): the engine marks that redaction FAILED, and the message is in the result’s redaction log (result.redaction_log[i].result.error_message).
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.