Merging Table Cells Dynamically

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Assembly MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-assembly -- dnx GroupDocs.Assembly.Mcp --yes
Folders, licensing and more →
Note
The example reads the shop data described in The Business Layer.

Using GroupDocs.Assembly for Python via .NET, you can merge table cells with equal textual contents within your reports dynamically using cellMerge tags. Syntax of a cellMerge tag is defined as follows:

<<cellMerge -horz>>
Warning
A horz switch is optional. If the switch is present, it denotes a cell merging operation in a horizontal direction. If the switch is missing, it means that a cell merging operation is to be performed in a vertical direction (the default).

For two or more successive table cells to be merged dynamically in either direction by the engine, the following requirements must be met:

  • Each of the cells must contain a cellMerge tag denoting a cell merging operation in the same direction.
  • Each of the cells must not be already merged in another direction.
  • The cells must have equal textual contents (ignoring leading and trailing whitespaces).

Consider the following template:

...

...

...

...

<<cellMerge>><<[Variable1]>>

...

...

<<cellMerge>><<[Variable2]>>

...

...

...

...

Warning
The cellMerge tag works only if Variable1 and Variable2 have the same values.

Merging Table Cells Dynamically in Word Processing Document

The Code

import json

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

with open("customer.json", encoding="utf-8") as f:
    customer = json.load(f)

assembler = DocumentAssembler()
assembler.assemble_document("merging-table-cells-dynamically.docx", "merging-table-cells-dynamically-report.docx", DataSourceInfo(customer, "customer"))

The following sample file is used in this example: merging-table-cells-dynamically.docx

The following sample file is used in this example: customer.json

Binary file (DOCX, 10 KB)

Download full output

Download

Merging Table Cells Dynamically in Presentation Document

The Code

import json

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

with open("customer.json", encoding="utf-8") as f:
    customer = json.load(f)

assembler = DocumentAssembler()
assembler.assemble_document("merging-table-cells-dynamically.pptx", "merging-table-cells-dynamically-report.pptx", DataSourceInfo(customer, "customer"))

The following sample file is used in this example: merging-table-cells-dynamically.pptx

The following sample file is used in this example: customer.json

Binary file (PPTX, 33 KB)

Download full output

Download

Merging Table Cells Dynamically in Spreadsheet Document

The Code

import json

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

with open("customer.json", encoding="utf-8") as f:
    customer = json.load(f)

assembler = DocumentAssembler()
assembler.assemble_document("merging-table-cells-dynamically.xlsx", "merging-table-cells-dynamically-report.xlsx", DataSourceInfo(customer, "customer"))

The following sample file is used in this example: merging-table-cells-dynamically.xlsx

The following sample file is used in this example: customer.json

Binary file (XLSX, 9 KB)

Download full output

Download

Merging Table Cells Dynamically in Email Document

The Code

import json
from datetime import datetime

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

def parse_dates(item):
    # JSON has no dates: turn the order dates into datetime objects for the template
    for key in ("OrderDate", "ShippingDate"):
        if key in item:
            item[key] = datetime.strptime(item[key], "%Y-%m-%dT%H:%M:%S")
    return item

with open("customer.json", encoding="utf-8") as f:
    customer = json.load(f, object_hook=parse_dates)

assembler = DocumentAssembler()
assembler.assemble_document("merging-table-cells-dynamically.msg", "merging-table-cells-dynamically-report.msg", [
    DataSourceInfo(customer, "customer"),
    DataSourceInfo("Harbor Lane Provisions <orders@harborlane.example>", "sender"),
    DataSourceInfo(["Quayside Bistro <orders@quaysidebistro.example>", "jonas.weber@harborlane.example"], "recipients"),
    DataSourceInfo("amara.osei@harborlane.example", "cc"),
    DataSourceInfo("Merging table cells dynamically", "subject"),
])

The following sample file is used in this example: merging-table-cells-dynamically.msg

The following sample file is used in this example: customer.json

Binary file (MSG, 36 KB)

Download full output

Download