This guide shows how to set up GroupDocs.Redaction for Python via .NET and run your first redactions. The library finds sensitive text, metadata, comments and image areas in documents and removes or replaces them, so the saved document no longer contains them.
For best practices, use a virtual environment to manage dependencies in Python applications. Learn more about virtual environments at the Create and Use Virtual Environments documentation topic.
Create and Activate a Virtual Environment
Create a virtual environment:
py-mvenv.venv
python3 -m venv .venv
python3 -m venv .venv
Activate a virtual environment:
.venv\Scripts\activate
source .venv/bin/activate
source .venv/bin/activate
Install groupdocs-redaction-net Package
After activating the virtual environment, run the following command in your terminal to install the latest version of the package:
py-mpipinstallgroupdocs-redaction-net
python3 -m pip install groupdocs-redaction-net
python3 -m pip install groupdocs-redaction-net
Ensure the package is installed successfully. pip installs groupdocs-shared-net, the package of the shared groupdocs.pydrawing and groupdocs.pycore modules, with it. You should see the message
The complaint letter sample.docx names the customer, Emily Johnson, twice. The example replaces the name with [customer] and saves the letter as a Word document:
fromgroupdocs.redactionimportRedactorfromgroupdocs.redaction.optionsimportSaveOptionsfromgroupdocs.redaction.redactionsimportExactPhraseRedaction,ReplacementOptionsdefredact_phrase():withRedactor("sample.docx")asredactor:# Replace every occurrence of the name with a labelresult=redactor.apply(ExactPhraseRedaction("Emily Johnson",ReplacementOptions("[customer]")))print("Status:",result.status.name)# Keep the Word format and add a suffix to the file nameoptions=SaveOptions()options.add_suffix=Trueoptions.rasterize_to_pdf=Falseoptions.redacted_file_suffix="redacted"print("Saved to",redactor.save(options))if__name__=="__main__":redact_phrase()
sample.docx is the sample file used in this example. Click here to download it.
Redactor("sample.docx"): Opens the document. Use it in a with block, so the file is released when you are done.
ExactPhraseRedaction(phrase, ReplacementOptions(text)): Finds the phrase, ignoring case, and replaces it with the text. Pass True as the second argument for a case-sensitive search.
apply(...): Runs the redaction and returns a change log; its status is APPLIED, PARTIALLY_APPLIED, SKIPPED or FAILED.
SaveOptions: rasterize_to_pdf = False keeps the original format; add_suffix and redacted_file_suffix name the output sample_redacted.docx, next to the source.
Example 2: Black out account numbers in a PDF
The customer statement sample.pdf holds an account number such as 12 34 567890. A regular expression finds it, and a black box covers it:
fromgroupdocs.redactionimportRedactorfromgroupdocs.redaction.options.drawingimportColorfromgroupdocs.redaction.redactionsimportRegexRedaction,ReplacementOptionsdefredact_account_numbers():withRedactor("sample.pdf")asredactor:# Two digits, two digits, then six digits: "12 34 567890"pattern=r"\d{2}\s*\d{2}[^\d]*\d{6}"result=redactor.apply(RegexRedaction(pattern,ReplacementOptions(Color.black)))print("Status:",result.status.name)# The default save writes a PDF of page images, from which no text can be recoveredprint("Saved to",redactor.save())if__name__=="__main__":redact_account_numbers()
sample.pdf is the sample file used in this example. Click here to download it.
RegexRedaction(pattern, options): Redacts every match of a regular expression, in .NET syntax.
ReplacementOptions(Color.black): Draws a box of that colour over the match instead of replacing its text. Color comes from groupdocs.redaction.options.drawing, with Point, Size and Rectangle.
save(): With no options, saves a rasterized PDF with the _Redacted suffix, sample_Redacted.pdf. Each page becomes an image, so the covered text is gone from the file, not only hidden.
Example 3: Clean a spreadsheet of a name and its metadata
apply() also takes a list. This example replaces the customer’s name in the delivery log sample.xlsx and erases the workbook’s metadata, such as its author and company, in one call:
fromgroupdocs.redactionimportRedactorfromgroupdocs.redaction.optionsimportSaveOptionsfromgroupdocs.redaction.redactionsimport(EraseMetadataRedaction,ExactPhraseRedaction,MetadataFilters,ReplacementOptions,)defredact_spreadsheet():withRedactor("sample.xlsx")asredactor:log=redactor.apply([ExactPhraseRedaction("Emily Johnson",ReplacementOptions("[customer]")),EraseMetadataRedaction(MetadataFilters.ALL),])# One log entry per redactionforentryinlog.redaction_log:print(entry.result.status.name)# Keep the Excel format, with the "redacted" suffixprint("Saved to",redactor.save(SaveOptions(False,"redacted")))if__name__=="__main__":redact_spreadsheet()
sample.xlsx is the sample file used in this example. Click here to download it.