Working with spreadsheet document attachments

Excel worksheets can embed file attachments (OLE objects). They are available through SpreadsheetContent.worksheets[i].attachments. Each attachment exposes alternative_text, is_link, source_full_name, content, and get_document_info(), and the document it contains can be opened in its own Watermarker. A linked attachment (is_link) stores only the path to its file in source_full_name; its content is None.

Extract attachments

from groupdocs.watermark import Watermarker
from groupdocs.watermark.options.spreadsheet import SpreadsheetLoadOptions

def extract_attachments():
    with Watermarker("./spreadsheet.xlsx", SpreadsheetLoadOptions()) as watermarker:
        content = watermarker.get_content()
        for i, worksheet in enumerate(content.worksheets):
            for attachment in worksheet.attachments:
                if attachment.is_link:
                    # A linked object stores only the path to its file, not the file itself
                    print(f"Worksheet {i}: {attachment.alternative_text!r} links to {attachment.source_full_name}")
                    continue
                info = attachment.get_document_info()
                print(f"Worksheet {i}: {attachment.alternative_text!r} type={info.file_type}")
                with open(f"./{attachment.alternative_text}", "wb") as f:
                    f.write(attachment.content)

if __name__ == "__main__":
    extract_attachments()

spreadsheet.xlsx is the sample file used in this example. Click here to download it.

Binary file (DOCX, 120 KB)

Download full output

Remove an attachment

The attachments collection supports remove_at(index) and remove(attachment). Iterate in reverse when removing by index. The example removes the linked attachments:

from groupdocs.watermark import Watermarker
from groupdocs.watermark.options.spreadsheet import SpreadsheetLoadOptions

def remove_attachment():
    with Watermarker("./spreadsheet.xlsx", SpreadsheetLoadOptions()) as watermarker:
        content = watermarker.get_content()
        for worksheet in content.worksheets:
            for i in range(len(worksheet.attachments) - 1, -1, -1):
                if worksheet.attachments[i].is_link:
                    worksheet.attachments.remove_at(i)
        watermarker.save("./output.xlsx")

if __name__ == "__main__":
    remove_attachment()

spreadsheet.xlsx is the sample file used in this example. Click here to download it.

Binary file (XLSX, 149 KB)

Download full output

Watermark the attached documents

Open each attachment’s bytes in its own Watermarker, add a watermark, and write the result back:

import io
from groupdocs.watermark import Watermarker
from groupdocs.watermark.watermarks import TextWatermark, Font, Color
from groupdocs.watermark.options.spreadsheet import SpreadsheetLoadOptions

def watermark_attached_documents():
    with Watermarker("./spreadsheet.xlsx", SpreadsheetLoadOptions()) as watermarker:
        content = watermarker.get_content()
        for worksheet in content.worksheets:
            for attachment in worksheet.attachments:
                if attachment.is_link:
                    continue  # the linked file is not stored in the workbook
                with Watermarker(io.BytesIO(attachment.content)) as inner:
                    wm = TextWatermark("CONFIDENTIAL", Font("Arial", 19.0))
                    wm.foreground_color = Color.red
                    inner.add(wm)
                    buffer = io.BytesIO()
                    inner.save(buffer)
                # write the watermarked attachment bytes back as needed
        watermarker.save("./output.xlsx")

if __name__ == "__main__":
    watermark_attached_documents()

spreadsheet.xlsx is the sample file used in this example. Click here to download it.

Binary file (XLSX, 150 KB)

Download full output

Close
Loading

Analyzing your prompt, please hold on...

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