Use redaction callback

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Redaction MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-redaction -- dnx GroupDocs.Redaction.Mcp --yes
Folders, licensing and more →

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.

description is a RedactionDescription, valid during the call:

  • 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:

from groupdocs.redaction import Redactor
from groupdocs.redaction.options import LoadOptions, RedactorSettings
from groupdocs.redaction.redactions import ExactPhraseRedaction, ReplacementOptions


class RedactionDump(object):
    def accept_redaction(self, description):
        print("%s redaction, %s action, item %s." % (
            description.redaction_type.name, description.action_type.name, description.original_text))
        if description.replacement is not None:
            print("Text %s is replaced with %s." % (
                description.replacement.original_text, description.replacement.replacement))
        # Return False here to skip this change
        return True


def use_redaction_callback():
    settings = RedactorSettings(RedactionDump())
    with Redactor("./sample.docx", LoadOptions(), settings) as redactor:
        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.

Binary file (PDF, 948 KB)

Download full output

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:

from groupdocs.redaction import Redactor
from groupdocs.redaction.options import LoadOptions, RedactorSettings
from groupdocs.redaction.redactions import ExactPhraseRedaction, ReplacementOptions


def skip_some_changes():
    accepted = []

    def accept_first_only(description):
        accepted.append(description.original_text)
        return len(accepted) == 1

    settings = RedactorSettings(callback=accept_first_only)
    with Redactor("./sample.docx", LoadOptions(), settings) as redactor:
        result = redactor.apply(ExactPhraseRedaction("Emily Johnson", ReplacementOptions("[personal]")))
        entry = result.redaction_log[0].result
        print("Matches offered: %d, status: %s (%s)" % (len(accepted), entry.status.name, entry.error_message))
        redactor.save()


if __name__ == "__main__":
    skip_some_changes()

sample.docx is the sample file used in this example. Click here to download it.

Binary file (PDF, 950 KB)

Download full output

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).