Skip to content

command query

zmworm edited this page Mar 31, 2026 · 23 revisions

query

Query document elements using CSS-like selectors.

Synopsis

officecli query <file> <selector> [--json]

Description

Searches the document for elements matching a CSS-like selector expression. Returns all matching elements with their paths and properties. Read-only.

Arguments

Name Type Required Default Description
file FileInfo Yes - Office document path
selector string Yes - CSS-like selector expression

Options

Name Type Required Default Description
--json bool No false Output as structured JSON

Selector Syntax

Attribute Filters

Operator Meaning Example
= Exact match [style=Heading1]
!= Not equal [font!=Arial]
~= Contains text [text~=quarterly]
>= Greater or equal (numeric) [size>=24pt]
<= Less or equal (numeric) [size<=12pt]

Notes:

  • Can combine: shape[fill=FF0000][size>=24pt]
  • Color-aware: [fill=#FF0000] matches [fill=FF0000]
  • Dimension-aware: compares 2cm vs 1in correctly
  • Special keys text and type match node content and type
  • \!= accepted for zsh compatibility

Child Selector

parent[filter] > child[filter]

Generic XML Fallback

Unknown selectors match any XML element by local name (case-insensitive).

Output Format

The query command returns a list of DocumentNodes matching the selector. The output varies depending on whether --json is used.

Text Output (default)

The text output starts with a match count, followed by each matching node listed with its path, text content, and formatting attributes.

Matches: {count}
  /body/p[1]: Heading text
    style: Heading1
    alignment: center
  /body/p[3]: Another paragraph
    style: Normal

Example:

Matches: 3
  /body/p[1]: Introduction
    style: Heading1
    alignment: center
    font: Arial
  /body/p[4]: Chapter One
    style: Heading1
    alignment: left
  /body/p[9]: Chapter Two
    style: Heading1
    alignment: left

JSON Output (--json)

With --json, the output is an object containing the match count and an array of DocumentNode results.

{
  "Matches": 3,
  "Results": [
    {
      "Path": "/body/p[1]",
      "Type": "Paragraph",
      "Text": "Introduction",
      "Preview": null,
      "Style": "Heading1",
      "ChildCount": 1,
      "Format": {
        "alignment": "center",
        "font": "Arial"
      },
      "Children": []
    },
    {
      "Path": "/body/p[4]",
      "Type": "Paragraph",
      "Text": "Chapter One",
      "Preview": null,
      "Style": "Heading1",
      "ChildCount": 1,
      "Format": {
        "alignment": "left"
      },
      "Children": []
    }
  ]
}

Each element in the Results array is a DocumentNode with the same fields as described in get Output Format: Path, Type, Text, Preview, Style, ChildCount, Format, and Children.

Format-Specific References

See Also


Based on OfficeCLI v1.0.28

Clone this wiki locally