If you have a corporate sensitive data removal policy as a list of redaction rules, you don’t need to specify them in your code. You can specify an XML document with a list of pre-configured redactions.
Below is an example of redaction policy XML file (code properties mapping is obvious):
You can use RedactionPolicy.save() method to create XML documents of this structure, configuring redactions in runtime.
The following example demonstrates how to save a RedactionPolicy to an XML file.
fromgroupdocs.redactionimportRedactionPolicyfromgroupdocs.redaction.redactionsimport(ExactPhraseRedaction,ReplacementOptions,RegexRedaction,DeleteAnnotationRedaction,EraseMetadataRedaction,MetadataFilters,)fromgroupdocs.pydrawingimportColordefcreate_redaction_policy():# Define the color of redactioncolor=Color.from_argb(255,220,20,60)# Configure the redactionsredactions=[ExactPhraseRedaction("Redaction",ReplacementOptions("[Product]")),RegexRedaction("\\d{2}\\s*\\d{2}[^\\d]*\\d{6}",ReplacementOptions(color)),DeleteAnnotationRedaction(),EraseMetadataRedaction(MetadataFilters.ALL),]# Create the policypolicy=RedactionPolicy(redactions)# Save the redaction policy to an XML filepolicy.save("./sample_policy.xml")print("Redactions policy saved to ./sample_policy.xml")if__name__=="__main__":create_redaction_policy()
You can have as many policies as you need, loading them to redact your documents.
The example below loads a redaction policy from an XML file and applies every rule it contains to a document in a single apply call:
fromgroupdocs.redactionimportRedactor,RedactionPolicyfromgroupdocs.redaction.optionsimportSaveOptionsdefuse_redaction_policy():# Load the redaction policy from an XML filepolicy=RedactionPolicy.load("./redaction_policy.xml")# Load the document and apply the whole policy in one callwithRedactor("./sample.docx")asredactor:redactor.apply(policy)# Keep the original format and append a suffix to the output namesave_options=SaveOptions()save_options.add_suffix=Truesave_options.rasterize_to_pdf=Falsesave_options.redacted_file_suffix="redacted"result_path=redactor.save(save_options)print(f"Redaction policy applied. Output saved to {result_path}.")if__name__=="__main__":use_redaction_policy()
redaction_policy.xml is the policy file used in this example. Click here to download it.
sample.docx is the document redacted in this example. Click here to download it.