Skip to content

Commit ec82a12

Browse files
jrubioh1jrubiohturulomio
authored
Created ODS.addImageToCell method and helpers.sheet_photos_from_lod (#210)
* added functions to add images in ods doc and update * refact comments and examples * Improved helper documentation. In helper blob can be empty_bytes or None * Updated dependencies * If image can't be loaded shows a warning (configurable) in sheet * Updated translations. Fixed pytest error * Improved github action to force test in pullrequest * Refactorizado helper a sheet_photos_from_lod --------- Co-authored-by: jrubioh <jrubioh@guardiacivil.es> Co-authored-by: turulomio <turulomio@yahoo.es>
1 parent 25afd06 commit ec82a12

17 files changed

Lines changed: 1209 additions & 1085 deletions

File tree

.github/workflows/python-app.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ name: Python application
22

33
on:
44
push:
5-
branches: [ "main" ]
5+
branches: [ "main", "master" ]
66
pull_request:
7-
branches: [ "main" ]
7+
branches: [ "main", "master" ]
8+
types: [opened, synchronize, reopened]
89

910
permissions:
1011
contents: write
@@ -17,7 +18,7 @@ jobs:
1718
steps:
1819
- uses: actions/checkout@v4
1920
- name: Set up Python
20-
uses: actions/setup-python@v5
21+
uses: actions/setup-python@v6
2122
- name: Install LibreOffice and uno dependencies
2223
run: |
2324
sudo apt-get update
@@ -29,4 +30,4 @@ jobs:
2930
.venv/bin/pip install pytest
3031
- name: Run Tests
3132
run: |
32-
.venv/bin/pytest -v # Correct venv path and remove 'sudo'
33+
.venv/bin/pytest -v

GEMINI.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ New tests have been added to `tests/test_unogenerator.py` to protect these fixes
5353
- **Parallel Performance:** This allows workers using independent servers (different ports) to run at full parallel speed without waiting for each other.
5454
- **Shared-Server Stability:** Workers sharing the same `LibreofficeServer` instance will correctly share the same lock, ensuring serialized access to the single connection and maintaining stability.
5555
- **Internal Protection:** Global UNO bridge calls (like `getComponentContext`) remain protected by a global `_uno_bridge_lock` to ensure the process-wide PyUNO state is not corrupted during initialization.
56+
57+
### 9. Test Execution Scope (Performance Optimization)
58+
**Decision:**
59+
- **Targeted Testing for AI Assistant:** The AI assistant must only run targeted test files or specific test functions relevant to the modified code (e.g., `poetry run pytest tests/test_helpers.py`), and NOT run the complete global `pytest` suite. The full global test suite is executed exclusively by the user to optimize task execution times.

HELPERS.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,46 @@ LOD writer with hierarchical sub-headers.
128128
- `row_of_totals` (bool, default=False): Generate totals at the bottom.
129129
- `freezeandselect` (Coord or str, default=None): Auto-freeze coordinate.
130130
- `key` (str, default="#SUM"): Formula key.
131+
### `sheet_photos_from_lod`
132+
Creates a photo catalog table in an ODS spreadsheet from a List of Dictionaries. Binary image blobs (`bytes` or `bytearray`) are automatically detected and anchored to cells, with row heights and column widths auto-adjusted to fit the image dimensions.
133+
- `doc` (ODS): The ODS document object.
134+
- `coord_start` (Coord or str): Starting coordinate.
135+
- `lod_photos` (list): List of dictionaries containing data and photo blobs.
136+
- **Claves de imagen binary (blobs)**: Cualquier clave con datos `bytes` or `bytearray` (p. ej. `'photo_blob'`, `'photo'`) es autodetectada como imagen y anclada a su celda correspondiente.
137+
- **Claves de datos de texto/numéricos**: Claves estándar (p. ej. `'name'`, `'id'`, `'description'`) que se insertan como celdas de datos habituales.
138+
- **`width`** *(float, opcional)*: Ancho personalizado de la imagen en cm para ese elemento (sobrescribe `default_width`). Se excluye automáticamente de las columnas de datos cuando `keys=None`.
139+
- **`height`** *(float, opcional)*: Alto personalizado de la imagen en cm para ese elemento (sobrescribe `default_height`). Se excluye automáticamente de las columnas de datos cuando `keys=None`.
140+
- **`name`** / **`nombre`** *(str, opcional)*: Nombre asignado internamente al objeto/figura gráfica UNO (`GraphicObjectShape`).
141+
- `headers` (list, default=None): Optional list of header labels. If `None` (default), no header row is written.
142+
- `keys` (list, default=None): Specific dictionary keys to include/order. If `None`, auto-detects all keys (excluding `width` and `height`).
143+
- `default_width` (float, default=2.5): Default image width in cm.
144+
- `default_height` (float, default=2.5): Default image height in cm.
145+
- `title` (str, default=None): Optional merged title for the block.
146+
- `color_row_header` (int, default=ColorsNamed.Orange): Color for header row.
147+
- `styles` (list or str, default=None): Style(s) for data cells.
131148
- `word_wrap` (bool, default=True): Enable text wrapping.
149+
- `on_error_str` (str, default=None): Fallback string to set in the image cell if `addImageToCell` fails (e.g. invalid bytes), or if the photo value is `None`, `b""`, or not a blob. If `None` (default), uses `_("Image couldn't be loaded")`.
150+
151+
**Ejemplo de uso:**
152+
```python
153+
lod_photos = [
154+
{"name": "Imagen 1", "photo_blob": image_bytes_1, "width": 3.0, "height": 2.0},
155+
{"name": "Imagen 2", "photo_blob": image_bytes_2}, # Ancho/alto por defecto (2.5 cm)
156+
{"name": "Imagen Sin Foto", "photo_blob": None} # Celda vacía (sin figura de imagen)
157+
]
158+
159+
helpers.sheet_photos_from_lod(
160+
doc,
161+
"A1",
162+
lod_photos,
163+
headers=["Nombre del producto", "Fotografía"],
164+
title="Catálogo de Productos"
165+
)
166+
```
132167

133168
---
134169

170+
135171
## 5. Complete Sheet Helpers
136172

137173
### `sheet_from_lod`

0 commit comments

Comments
 (0)