From f9b09ebbed383c2634175d6a50b9373d29331058 Mon Sep 17 00:00:00 2001 From: Dmitriy Date: Thu, 27 Apr 2023 18:50:14 +0600 Subject: [PATCH] added support for pictures --- Cargo.toml | 2 +- python/python_calamine/_python_calamine.pyi | 1 + src/types/workbook.rs | 18 +++++++++++++++++- tests/test_base.py | 1 - 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6f7c238..3e66c78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ name = "python_calamine" crate-type = ["cdylib"] [dependencies] -calamine = {git = "https://github.com/tafia/calamine.git", rev = "a5b85d30", features = ["dates", "chrono"]} +calamine = {git = "https://github.com/tafia/calamine.git", rev = "a5b85d30", features = ["dates", "chrono", "picture"]} pyo3 = {version = "0.18.3", features = ["extension-module", "chrono"]} chrono = {version = "0.4.24", features = ["serde"]} pyo3-file = {git = "https://github.com/omerbenamram/pyo3-file.git", rev = "2bc4c1a0"} diff --git a/python/python_calamine/_python_calamine.pyi b/python/python_calamine/_python_calamine.pyi index 9b35e66..22b3e3a 100644 --- a/python/python_calamine/_python_calamine.pyi +++ b/python/python_calamine/_python_calamine.pyi @@ -66,6 +66,7 @@ class CalamineWorkbook: """ def get_sheet_by_name(self, name: str) -> CalamineSheet: ... def get_sheet_by_index(self, index: int) -> CalamineSheet: ... + def pictures(self) -> list[tuple[str, bytes]] | None: ... class CalamineError(Exception): ... diff --git a/src/types/workbook.rs b/src/types/workbook.rs index 4c6e47e..ef8ae11 100644 --- a/src/types/workbook.rs +++ b/src/types/workbook.rs @@ -4,7 +4,7 @@ use std::path::PathBuf; use calamine::{open_workbook_auto, open_workbook_auto_from_rs, Error, Reader, Sheets}; use pyo3::prelude::*; -use pyo3::types::{PyString, PyType}; +use pyo3::types::{PyBytes, PyString, PyType}; use pyo3_file::PyFileLikeObject; use crate::utils::err_to_py; @@ -42,6 +42,13 @@ impl SheetsEnum { SheetsEnum::FileLike(f) => f.worksheet_range_at(index), } } + + fn pictures(&self) -> Option)>> { + match self { + SheetsEnum::File(f) => f.pictures(), + SheetsEnum::FileLike(f) => f.pictures(), + } + } } #[pyclass] @@ -93,6 +100,15 @@ impl CalamineWorkbook { .map_err(err_to_py)?; Ok(CalamineSheet::new(name, range)) } + + fn pictures<'a>(&self, py: Python<'a>) -> PyResult>> { + Ok(self.sheets.pictures().map(|pictures| { + pictures + .into_iter() + .map(|(format, bytes)| (format, PyBytes::new(py, &bytes))) + .collect() + })) + } } impl CalamineWorkbook { diff --git a/tests/test_base.py b/tests/test_base.py index d7639f2..91925bd 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -9,7 +9,6 @@ def test_ods_read(): - # calamine not supported ods date/datetime parse names = ["Sheet1", "Sheet2"] data = [ ["", "", "", "", "", "", "", "", "", ""],