Convertire documenti con Python
in data FOSS Informatica Python
Convertire documenti con Python
Convertire documenti con Python
Versione italiana
Di seguito vedremo come convertire documenti in modo facile e veloce con Pyhton!
1. LibreOffice/UNO (unoconv) – La soluzione più completa
<a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb1-1"></a># Installazione su Ubuntu/Debian <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb1-2"></a>sudo apt update <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb1-3"></a>sudo apt install libreoffice python3-uno <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb1-4"></a> <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb1-5"></a># Installazione su macOS (con Homebrew) <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb1-6"></a>brew install libreoffice <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb1-7"></a> <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb1-8"></a># Installazione su Windows: <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb1-9"></a># Scarica LibreOffice da https://www.libreoffice.org/download/download/ <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb1-10"></a> <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb1-11"></a># Installa il wrapper Python <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb1-12"></a>pip install unoconv
Esempio d’uso:
<a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb2-1"></a>import subprocess <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb2-2"></a> <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb2-3"></a># Converti DOCX a PDF <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb2-4"></a>subprocess.run(['unoconv', '-f', 'pdf', 'documento.docx']) <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb2-5"></a> <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb2-6"></a># Converti ODT a DOCX <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb2-7"></a>subprocess.run(['unoconv', '-f', 'docx', 'documento.odt'])
2. Pandoc – Per documenti semplici (Markdown, LaTeX, ecc.)
<a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb3-1"></a># Installazione <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb3-2"></a>sudo apt install pandoc # Linux <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb3-3"></a>brew install pandoc # macOS <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb3-4"></a># Windows: https://pandoc.org/installing.html <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb3-5"></a> <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb3-6"></a>pip install pypandoc
Esempio d’uso:
<a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb4-1"></a>import pypandoc <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb4-2"></a> <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb4-3"></a># Converti Markdown a DOCX <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb4-4"></a>pypandoc.convert_file('input.md', 'docx', outputfile='output.docx') <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb4-5"></a> <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb4-6"></a># Converti LaTeX a PDF <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb4-7"></a>pypandoc.convert_file('input.tex', 'pdf', outputfile='output.pdf')
3. Pure Python Libraries (Zero dipendenze esterne)
a) Per DOCX/ODT:
<a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb5-1"></a>pip install python-docx odfpy
Esempio conversione ODT → DOCX:
<a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb6-1"></a>from odfpy import OpenDocument, load <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb6-2"></a>from docx import Document <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb6-3"></a> <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb6-4"></a>def odt_to_docx(input_path, output_path): <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb6-5"></a> doc = Document() <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb6-6"></a> odt = load(input_path) <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb6-7"></a> <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb6-8"></a> for para in odt.getElementsByType('paragraph'): <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb6-9"></a> doc.add_paragraph(para.getAttribute('text')) <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb6-10"></a> <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb6-11"></a> doc.save(output_path) <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb6-12"></a> <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb6-13"></a>odt_to_docx('input.odt', 'output.docx')
b) Per PDF:
<a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb7-1"></a>pip install pdf2docx
Esempio PDF → DOCX:
<a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb8-1"></a>from pdf2docx import Converter <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb8-2"></a> <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb8-3"></a>cv = Converter('input.pdf') <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb8-4"></a>cv.convert('output.docx', start=0, end=None) <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb8-5"></a>cv.close()
4. Soluzioni alternative specifiche
a) Per fogli di calcolo (CSV/XLSX/ODS):
<a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb9-1"></a>pip install pandas pyexcel pyexcel-xlsx pyexcel-ods
Esempio CSV → ODS:
<a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb10-1"></a>import pandas as pd <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb10-2"></a>df = pd.read_csv('input.csv') <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb10-3"></a>df.to_excel('output.ods', engine='odf')
b) Per presentazioni (PPT → PDF):
<a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb11-1"></a>pip install python-pptx
Esempio PPTX → PDF (richiede
unoconv
):<a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb12-1"></a>import subprocess <a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb12-2"></a>subprocess.run(['unoconv', '-f', 'pdf', 'presentazione.pptx'])
5. Docker per ambienti isolati
Se vuoi evitare installazioni di sistema:
<a href="https://magnetimo.blogspot.com/2025/05/convertire-documenti-con-python.html?m=1#cb13-1"></a>docker run -v $(pwd):/convert -it docker.io/libreoffice/headless unoconv -f pdf /convert/documento.docx
Tabella riassuntiva delle alternative:
Formato Libreria Consigliata Comando Installazione DOCX ↔︎ ODT odfpy
+python-docx
pip install odfpy python-docx
PDF ↔︎ DOCX pdf2docx
pip install pdf2docx
XLSX ↔︎ ODS pandas
pip install pandas pyexcel-ods
Presentazioni unoconv
(via Docker)docker pull libreoffice/headless
Consigli finali:
- Per massima compatibilità: usa LibreOffice/UNO (anche via Docker)
- Per documenti semplici: Pandoc o soluzioni pure Python
- Per ambienti senza GUI: prediligi
pdf2docx
,python-docx
,odfpy
- Evita
pywin32
o librerie Windows-specifiche se vuoi cross-platform
Pages: 1 2