Last active
July 28, 2024 04:13
-
-
Save Kremilly/0bc4e9c222c6bce697e9c750da857543 to your computer and use it in GitHub Desktop.
run_python.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use pyo3::prelude::*; | |
use std::error::Error; | |
fn main() -> Result<(), Box<dyn Error>> { | |
// Inicializa o Python | |
Python::with_gil(|py| { | |
// Define o script Python | |
let code = r#" | |
import pymupdf # PyMuPDF | |
def extract_first_page_to_png(pdf_path, output_path): | |
# Abre o documento PDF | |
document = pymupdf.open(pdf_path) | |
# Verifica se o documento tem pelo menos uma página | |
if document.page_count < 1: | |
raise ValueError("O documento PDF não contém páginas.") | |
# Obtém a primeira página | |
page = document.load_page(0) | |
# Renderiza a página como uma imagem | |
pix = page.get_pixmap() | |
# Salva a imagem como PNG | |
pix.save(output_path) | |
return f"Cover output saved as {output_path}" | |
"#; | |
// Cria um novo contexto Python | |
let ctx = PyModule::from_code_bound(py, code, "extractor", "extractor")?; | |
// Define os argumentos | |
let pdf_path = "file.pdf"; | |
let output_path = "first_page.png"; | |
// Executa a função Python e captura a saída | |
let result: String = ctx | |
.getattr("extract_first_page_to_png")? | |
.call1((pdf_path, output_path))? | |
.extract()?; | |
// Exibe o resultado | |
println!("{}", result); | |
Ok(()) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment