Extract Data from Attachments and ZIP Archives

Use container extraction to open archive-like formats (ZIP, RAR, TAR), Outlook stores (PST/OST), PDF portfolios, and email attachments, then parse their contents.

Extract and parse attachments

from groupdocs.parser import Parser
from groupdocs.parser.exceptions import UnsupportedDocumentFormatException

with Parser("./archive.zip") as parser:
    attachments = parser.get_container()
    if attachments is None:
        print("Container extraction isn't supported for this format.")
    else:
        for item in attachments:
            print(item.file_path)
            try:
                with item.open_parser() as attachment_parser:
                    reader = attachment_parser.get_text()
                    print(reader.read_to_end() if reader is not None else "No text available.")
            except UnsupportedDocumentFormatException:
                print("Item format is not supported.")

The following sample file is used in this example: archive.zip

Steps

  1. Instantiate Parser for the container file.
  2. Call get_container() to list ContainerItem entries.
  3. For each item, use open_parser() to create a Parser over the embedded content and reuse standard extraction methods (get_text, get_images, get_metadata, etc.).
  4. Handle unsupported formats with UnsupportedDocumentFormatException.

For working with attachments in complex scenarios (nested containers, load options), see the advanced containers guide.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.