Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for pictures #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
1 change: 1 addition & 0 deletions python/python_calamine/_python_calamine.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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): ...

Expand Down
18 changes: 17 additions & 1 deletion src/types/workbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,6 +42,13 @@ impl SheetsEnum {
SheetsEnum::FileLike(f) => f.worksheet_range_at(index),
}
}

fn pictures(&self) -> Option<Vec<(String, Vec<u8>)>> {
match self {
SheetsEnum::File(f) => f.pictures(),
SheetsEnum::FileLike(f) => f.pictures(),
}
}
}

#[pyclass]
Expand Down Expand Up @@ -93,6 +100,15 @@ impl CalamineWorkbook {
.map_err(err_to_py)?;
Ok(CalamineSheet::new(name, range))
}

fn pictures<'a>(&self, py: Python<'a>) -> PyResult<Option<Vec<(String, &'a PyBytes)>>> {
Ok(self.sheets.pictures().map(|pictures| {
pictures
.into_iter()
.map(|(format, bytes)| (format, PyBytes::new(py, &bytes)))
.collect()
}))
}
}

impl CalamineWorkbook {
Expand Down
1 change: 0 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


def test_ods_read():
# calamine not supported ods date/datetime parse
names = ["Sheet1", "Sheet2"]
data = [
["", "", "", "", "", "", "", "", "", ""],
Expand Down