3.6 KiB
3.6 KiB
LibreOffice CLI -- Standard Operating Procedure
Overview
The LibreOffice CLI harness provides command-line document editing for Writer (word processor), Calc (spreadsheet), and Impress (presentations). It produces real ODF files (ZIP archives with XML) that can be opened by LibreOffice and other ODF-compatible applications. It can also import existing ODF and Office files into the harness JSON project model for editing.
Architecture
cli/
__init__.py
__main__.py # python3 -m cli.libreoffice_cli entry point
libreoffice_cli.py # Click CLI with groups and REPL
core/
__init__.py
document.py # create/open/save/info/profiles
importer.py # import existing ODF/Office files into project JSON
writer.py # paragraphs, headings, lists, tables, page breaks
calc.py # sheets, cells, formulas
impress.py # slides, elements
styles.py # named styles, apply to content
export.py # ODF, HTML, text export
session.py # undo/redo state management
utils/
__init__.py
odf_utils.py # ODF XML generation and ZIP packaging
tests/
__init__.py
test_core.py # 60+ unit tests
test_full_e2e.py # 40+ E2E tests with ODF validation
Project Format
Documents are stored as JSON project files (.lo-cli.json) with this structure:
- Writer:
{"type": "writer", "content": [...], "styles": {...}, "settings": {...}} - Calc:
{"type": "calc", "sheets": [...], "styles": {...}, "settings": {...}} - Impress:
{"type": "impress", "slides": [...], "styles": {...}, "settings": {...}}
Key Design Decisions
- Python stdlib only (plus click): No Pillow, numpy, or LibreOffice needed
- Real ODF output: ZIP files with proper mimetype, content.xml, styles.xml, meta.xml, manifest.xml
- Undo/redo: Deep-copy snapshots before every mutation
- Multi-format: Same CLI handles Writer, Calc, and Impress
- JSON mode:
--jsonflag for agent consumption
Running
cd /root/cli-anything/libreoffice/agent-harness
pip install click
python3 -m cli.libreoffice_cli --help
python3 -m pytest cli/tests/ -v
Common Workflows
Create a Writer document and export to ODF
python3 -m cli.libreoffice_cli document new --type writer -n "Report" -o report.json
python3 -m cli.libreoffice_cli --project report.json writer add-heading -t "Title" -l 1
python3 -m cli.libreoffice_cli --project report.json writer add-paragraph -t "Body text"
python3 -m cli.libreoffice_cli --project report.json export render report.odt -p odt --overwrite
Import an existing document, edit it, and export it
python3 -m cli.libreoffice_cli document import existing.docx -o existing.json
python3 -m cli.libreoffice_cli --project existing.json writer add-paragraph -t "Added by CLI"
python3 -m cli.libreoffice_cli --project existing.json export render existing-updated.docx -p docx --overwrite
Create a spreadsheet
python3 -m cli.libreoffice_cli document new --type calc -o budget.json
python3 -m cli.libreoffice_cli --project budget.json calc set-cell A1 "Revenue" --type string
python3 -m cli.libreoffice_cli --project budget.json calc set-cell B1 "50000" --type float
python3 -m cli.libreoffice_cli --project budget.json export render budget.ods -p ods --overwrite
Create a presentation
python3 -m cli.libreoffice_cli document new --type impress -o deck.json
python3 -m cli.libreoffice_cli --project deck.json impress add-slide -t "Welcome" -c "Hello"
python3 -m cli.libreoffice_cli --project deck.json export render deck.odp -p odp --overwrite