Skip to content

Instantly share code, notes, and snippets.

@me-suzy
Created May 6, 2025 21:31
Show Gist options
  • Save me-suzy/44896068087291e181453f0e19cd26da to your computer and use it in GitHub Desktop.
Save me-suzy/44896068087291e181453f0e19cd26da to your computer and use it in GitHub Desktop.
convert rtf to pdf 2.py
import os
import subprocess
import time
# Setează calea folderului
folder_path = r"D:\3"
# Calea către executabilul LibreOffice
libreoffice_path = r"D:\Program Files\LibreOffice\program\soffice.exe"
# Verifică dacă LibreOffice este instalat
if not os.path.exists(libreoffice_path):
print(f"Eroare: LibreOffice nu a fost găsit la {libreoffice_path}")
exit(1)
print("Inițializare conversie cu LibreOffice...")
# Contori pentru statistici
files_processed = 0
files_skipped = 0
files_failed = 0
# Parcurge toate fișierele din folder
for filename in os.listdir(folder_path):
if filename.lower().endswith(".rtf"):
# Calea completă a fișierului RTF
rtf_path = os.path.join(folder_path, filename)
# Calea pentru fișierul PDF
pdf_filename = os.path.splitext(filename)[0] + ".pdf"
pdf_path = os.path.join(folder_path, pdf_filename)
# Verifică dacă PDF-ul există deja
if os.path.exists(pdf_path):
print(f"\nSkip: {filename} - PDF-ul există deja")
files_skipped += 1
continue
print(f"\nÎncepe conversia pentru: {filename}")
# Construiește comanda pentru conversie
command = [
libreoffice_path,
"--headless",
"--convert-to", "pdf",
"--outdir", folder_path,
rtf_path
]
print("Rularea comenzii de conversie...")
start_time = time.time()
try:
result = subprocess.run(command, capture_output=True, text=True, check=True)
conversion_time = time.time() - start_time
print(f"Conversia finalizată în {conversion_time:.2f} secunde")
# Verifică dacă fișierul PDF a fost creat cu succes
if os.path.exists(pdf_path):
print(f"PDF creat cu succes: {pdf_filename}")
files_processed += 1
else:
print(f"ATENȚIE: Procesul a rulat dar PDF-ul nu a fost găsit!")
files_failed += 1
except subprocess.CalledProcessError as e:
print(f"Eroare la conversie: {e}")
print("Ieșire eroare:", e.stderr)
files_failed += 1
# Afișează statistici finale
print("\n--- SUMAR CONVERSIE ---")
print(f"Total fișiere RTF procesate: {files_processed}")
print(f"Fișiere sărite (PDF exista deja): {files_skipped}")
print(f"Fișiere cu erori: {files_failed}")
print("Conversia s-a finalizat!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment