ab52d26b99
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
284 lines
14 KiB
Plaintext
284 lines
14 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "ozeCGy0fwA4R"
|
|
},
|
|
"source": [
|
|
"# Conversione PDF con MinerU"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "zyMHLIPh3sCk"
|
|
},
|
|
"source": [
|
|
"[](https://github.com/opendatalab/MinerU)\n",
|
|
"[](https://github.com/opendatalab/MinerU)\n",
|
|
"[](https://github.com/opendatalab/MinerU/issues)\n",
|
|
"[](https://github.com/opendatalab/MinerU/issues)\n",
|
|
"[](https://pypi.org/project/mineru/)\n",
|
|
"[](https://pypi.org/project/mineru/)\n",
|
|
"[](https://pepy.tech/project/mineru)\n",
|
|
"[](https://pepy.tech/project/mineru)\n",
|
|
"[](https://mineru.net/OpenSourceTools/Extractor?source=github)\n",
|
|
"[](https://huggingface.co/spaces/opendatalab/MinerU)\n",
|
|
"[](https://www.modelscope.cn/studios/OpenDataLab/MinerU)\n",
|
|
"[](https://colab.research.google.com/gist/myhloli/a3cb16570ab3cfeadf9d8f0ac91b4fca/mineru_demo.ipynb)\n",
|
|
"[](https://arxiv.org/abs/2409.18839)\n",
|
|
"[](https://arxiv.org/abs/2509.22186)\n",
|
|
"[](https://deepwiki.com/opendatalab/MinerU)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "F9dnYLSDwQyR"
|
|
},
|
|
"source": [
|
|
"## 1.Install"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "Rw-hwakFwaeY"
|
|
},
|
|
"source": [
|
|
"### Install via pip"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 1000
|
|
},
|
|
"id": "_3EDplqlxKB_",
|
|
"outputId": "a4c96ba2-6dea-4ef0-aac7-955dbfac0598"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install --upgrade pip\n",
|
|
"!pip install \"mineru[all]>=3.1.3\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "rSfS77AisXH8"
|
|
},
|
|
"source": [
|
|
"### Check verison"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "MSyCKHmqCp_j",
|
|
"outputId": "eb0aad89-3261-4388-9dfc-2b6ce29a3030"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"mineru, version 3.1.3\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"!mineru -v"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "Qh8YKU19CrQY"
|
|
},
|
|
"source": [
|
|
"## 2.Usage"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "3dZlkl3jDNKL"
|
|
},
|
|
"source": [
|
|
"### 2.1 Command line"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "mVRMM_FaDi2d"
|
|
},
|
|
"source": [
|
|
"#### **PDF**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "Hyoz1B-tPXcM"
|
|
},
|
|
"source": [
|
|
"#### **Converti un singolo PDF**\n",
|
|
"\n",
|
|
"Su Google Colab il file system è temporaneo: ogni volta che avvii una nuova sessione la cartella `/content/` è vuota.\n",
|
|
"Devi quindi **caricare il tuo PDF prima di eseguire la conversione**.\n",
|
|
"\n",
|
|
"**Come caricare il file:**\n",
|
|
"\n",
|
|
"1. Nel pannello di sinistra clicca sull'icona **File**.\n",
|
|
"2. Trascina il PDF nella cartella `/content/` (radice della sessione), oppure clicca il pulsante *Carica* (icona freccia su).\n",
|
|
"3. Attendi che il caricamento sia completato: il file comparirà nell'elenco.\n",
|
|
"\n",
|
|
"In alternativa puoi caricare via codice:\n",
|
|
"\n",
|
|
"```python\n",
|
|
"from google.colab import files\n",
|
|
"uploaded = files.upload() # apre il selettore file\n",
|
|
"```\n",
|
|
"\n",
|
|
"Una volta caricato, il PDF si trova in `/content/<nome-file>.pdf`.\n",
|
|
"Sostituisci `mio_documento.pdf` nella cella seguente con il nome del tuo file:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"PDF_NAME = \"mio_documento.pdf\" # ← sostituisci con il nome del file che hai caricato\n",
|
|
"import os\n",
|
|
"STEM = os.path.splitext(os.path.basename(PDF_NAME))[0]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"collapsed": true,
|
|
"id": "6RivAWteLeYM",
|
|
"outputId": "dc56bcc0-6e84-432a-8511-e461ff605a51"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"!mineru -p {PDF_NAME} -o ./output -b pipeline"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/",
|
|
"height": 807
|
|
},
|
|
"id": "F6K4E8CfH6_V",
|
|
"outputId": "136175e0-c1b0-4c80-84c4-ce7e7c78e656"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os, zipfile\n",
|
|
"\n",
|
|
"output_root = './output'\n",
|
|
"stems = [d for d in os.listdir(output_root) if os.path.isdir(os.path.join(output_root, d))] if os.path.isdir(output_root) else []\n",
|
|
"\n",
|
|
"if not stems:\n",
|
|
" print(f'Nessuna cartella trovata in {output_root}')\n",
|
|
"else:\n",
|
|
" for stem in sorted(stems):\n",
|
|
" candidates = [\n",
|
|
" f'{output_root}/{stem}/hybrid_auto',\n",
|
|
" f'{output_root}/{stem}/auto',\n",
|
|
" f'{output_root}/{stem}',\n",
|
|
" ]\n",
|
|
" output_dir = next((p for p in candidates if os.path.isdir(p)), None)\n",
|
|
"\n",
|
|
" if output_dir is None:\n",
|
|
" print(f'[{stem}] cartella output non trovata, saltato')\n",
|
|
" continue\n",
|
|
"\n",
|
|
" zip_path = f'./{stem}_output.zip'\n",
|
|
" with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:\n",
|
|
" for root, dirs, files in os.walk(output_dir):\n",
|
|
" for file in files:\n",
|
|
" full_path = os.path.join(root, file)\n",
|
|
" arcname = os.path.relpath(full_path, start=os.path.dirname(output_dir))\n",
|
|
" zf.write(full_path, arcname)\n",
|
|
" size_mb = os.path.getsize(zip_path) / (1024 * 1024)\n",
|
|
" print(f'[{stem}] ZIP creato: {zip_path} ({size_mb:.1f} MB)')\n",
|
|
"\n",
|
|
" try:\n",
|
|
" from google.colab import files\n",
|
|
" files.download(zip_path)\n",
|
|
" except ImportError:\n",
|
|
" print('(download automatico disponibile solo su Google Colab)')\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "t53WETM90pKr"
|
|
},
|
|
"source": [
|
|
"#### **Converti più PDF nella stessa cartella**\n",
|
|
"\n",
|
|
"Converte tutti i PDF presenti in `/content/` in un'unica esecuzione.\n",
|
|
"\n",
|
|
"Carica i file prima di eseguire la cella (trascina i PDF nel pannello File a sinistra oppure usa `files.upload()`).\n",
|
|
"\n",
|
|
"L'output di ogni PDF viene salvato in `./output/<nome-file>/auto/`.\n",
|
|
"\n",
|
|
"> Usa questa cella al posto di quella precedente se hai più documenti da convertire."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "4dqBGZiz0orA",
|
|
"outputId": "8233269d-d912-4dd3-9332-d26bd464db7c"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"!mineru -p ./ -o ./output -b pipeline"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"accelerator": "GPU",
|
|
"colab": {
|
|
"gpuType": "T4",
|
|
"provenance": []
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|