When you open a document from a stream, or when a file’s extension does not match its actual format, pass the format through LoadOptions. GroupDocs.Redaction then skips format detection and opens the document as the type you name.
Use a constant of the FileType class, such as FileType.DOCX or FileType.PDF: LoadOptions(FileType.DOCX), or load_options.file_type = FileType.DOCX. Without it, the format is detected as before.
Open a stream as a given type
fromgroupdocs.redactionimportFileType,Redactorfromgroupdocs.redaction.optionsimportLoadOptions,SaveOptionsfromgroupdocs.redaction.redactionsimportExactPhraseRedaction,ReplacementOptionsdefload_stream_with_file_type():withopen("./sample.docx","rb")asstream_in:withRedactor(stream_in,LoadOptions(FileType.DOCX))asredactor:redactor.apply(ExactPhraseRedaction("Emily Johnson",ReplacementOptions("[personal]")))# Save in the original format, to another streamoptions=SaveOptions()options.rasterize_to_pdf=Falsewithopen("./sample_redacted.docx","wb")asstream_out:redactor.save(stream_out,options)print("Output saved to sample_redacted.docx")if__name__=="__main__":load_stream_with_file_type()
sample.docx is the sample file used in this example. Click here to download it.
fromgroupdocs.redactionimportFileType,Redactorfromgroupdocs.redaction.optionsimportLoadOptionsfromgroupdocs.redaction.options.drawingimportColorfromgroupdocs.redaction.redactionsimportExactPhraseRedaction,ReplacementOptionsdefload_file_with_file_type():# The file is a PDF, saved with a .bin extensionwithRedactor("./sample.bin",LoadOptions(FileType.PDF))asredactor:redactor.apply(ExactPhraseRedaction("Emily Johnson",ReplacementOptions(Color.chocolate)))result_path=redactor.save()print("Output saved to "+result_path)if__name__=="__main__":load_file_with_file_type()
sample.bin is the sample file used in this example. Click here to download it.