Load with file type

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

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

def load_stream_with_file_type():
    with open("./sample.docx", "rb") as stream_in:
        with Redactor(stream_in, LoadOptions(FileType.DOCX)) as redactor:
            redactor.apply(ExactPhraseRedaction("Emily Johnson", ReplacementOptions("[personal]")))

            # Save in the original format, to another stream
            options = SaveOptions()
            options.rasterize_to_pdf = False
            with open("./sample_redacted.docx", "wb") as stream_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.
Binary file (DOCX, 40 KB)

Download full output

Open a file whose extension is not its format

from groupdocs.redaction import FileType, Redactor
from groupdocs.redaction.options import LoadOptions
from groupdocs.redaction.options.drawing import Color
from groupdocs.redaction.redactions import ExactPhraseRedaction, ReplacementOptions

def load_file_with_file_type():
    # The file is a PDF, saved with a .bin extension
    with Redactor("./sample.bin", LoadOptions(FileType.PDF)) as redactor:
        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.
Binary file (PDF, 914 KB)

Download full output