Skip to content

Instantly share code, notes, and snippets.

@me-suzy
Created May 17, 2025 17:25
Show Gist options
  • Save me-suzy/90a39f616f450319af15e49da60c634b to your computer and use it in GitHub Desktop.
Save me-suzy/90a39f616f450319af15e49da60c634b to your computer and use it in GitHub Desktop.
afasfasfff.py
import os
from docx import Document
import PyPDF2
# Definim calea directorului
source_dir = r"E:\De pus pe FTP 2\Test"
output_dir = os.path.join(source_dir, "Converted")
# Creăm directorul de ieșire dacă nu există
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Funcție pentru a crea un fișier .docx din text
def create_docx(text, output_path):
doc = Document()
doc.add_paragraph(text)
doc.save(output_path)
# Funcție pentru a extrage text dintr-un fișier PDF
def extract_text_from_pdf(pdf_path):
text = ""
with open(pdf_path, "rb") as file:
reader = PyPDF2.PdfReader(file)
for page in reader.pages:
text += page.extract_text() + "\n"
return text
# Parcurgem toate fișierele din director
for filename in os.listdir(source_dir):
file_path = os.path.join(source_dir, filename)
# Verificăm dacă este fișier (nu director)
if os.path.isfile(file_path):
# Numele fișierului de ieșire (fără extensie)
output_filename = os.path.splitext(filename)[0] + ".docx"
output_path = os.path.join(output_dir, output_filename)
try:
# Procesăm fișiere .txt
if filename.lower().endswith(".txt"):
with open(file_path, "r", encoding="utf-8") as file:
text = file.read()
create_docx(text, output_path)
print(f"Convertit: {filename} -> {output_filename}")
# Procesăm fișiere .pdf
elif filename.lower().endswith(".pdf"):
text = extract_text_from_pdf(file_path)
create_docx(text, output_path)
print(f"Convertit: {filename} -> {output_filename}")
else:
print(f"Ignorat: {filename} (format nesuportat)")
except Exception as e:
print(f"Eroare la procesarea {filename}: {str(e)}")
print("Conversia s-a încheiat!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment