Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login

POZE MRRRR'S FORUM (VIEW ONLY)

Nu sunteti logat.
Nou pe simpatie:
mimy_glacier
Femeie
24 ani
Prahova
cauta Barbat
26 - 47 ani
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides / [GOOGLE COLAB] Convert HTML and PDF Templates to Excel Templates Moderat de TRaP, TonyTzu
Autor
Mesaj Pagini: 1
TRaP
Moderator

Inregistrat: acum 8 ani
Postari: 931
I generated the following 2 codes using Gemini 3.5 Flash, for 2 templates that arrived in HTML and PDF formats respectively.

HTML conversion Excel
Some styling is applied as per Gemini's identification in the actual template file.


# ==========================================
# 1. INSTALAREA LIBRĂRIILOR NECESARE
# ==========================================
!pip install beautifulsoup4 openpyxl lxml

import io
from google.colab import files
from bs4 import BeautifulSoup
import openpyxl
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side

# ==========================================
# 2. ÎNCĂRCAREA MANUALĂ A FIȘIERULUI
# ==========================================
print("Te rog selectează fișierul tău HTML (apasă pe butonul de mai jos):")
uploaded = files.upload()

# Identificăm fișierul încărcat
file_name = list(uploaded.keys())[0]
html_content = uploaded[file_name].decode("utf-8")
print(f"\nFișierul '{file_name}' a fost încărcat cu succes! Începe conversia...")

# ==========================================
# 3. CONVERTIREA ȘI FORMATAREA EXCEL
# ==========================================
soup = BeautifulSoup(html_content, "html.parser")

# Creăm registrul de lucru Excel
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "Anexa VII - Date"
ws.views.sheetView[0].showGridLines = True

# Definirea stilurilor conform designului original (Navy & Gold)
navy_fill = PatternFill(start_color="1B3A5C", end_color="1B3A5C", fill_type="solid")
section_fill = PatternFill(start_color="EEF2F6", end_color="EEF2F6", fill_type="solid")
light_grey_fill = PatternFill(start_color="F9FAFB", end_color="F9FAFB", fill_type="solid")

white_font = Font(name="Arial", size=11, bold=True, color="FFFFFF")
title_font = Font(name="Arial", size=16, bold=True, color="1B3A5C")
sub_font = Font(name="Arial", size=10, italic=True, color="6B7280")
section_font = Font(name="Arial", size=11, bold=True, color="D4AA00")
bold_font = Font(name="Arial", size=10, bold=True, color="1F2937")
regular_font = Font(name="Arial", size=10, color="1F2937")
grey_font = Font(name="Arial", size=9, color="6B7280")
footer_font = Font(name="Arial", size=8.5, italic=True, color="9CA3AF")

thin_border = Border(
    left=Side(style='thin', color='E5E7EB'), right=Side(style='thin', color='E5E7EB'),
    top=Side(style='thin', color='E5E7EB'), bottom=Side(style='thin', color='E5E7EB')
)

# Setăm lățimea coloanelor
ws.column_dimensions['A'].width = 32
ws.column_dimensions['B'].width = 20
ws.column_dimensions['C'].width = 16
ws.column_dimensions['D'].width = 16
ws.column_dimensions['E'].width = 16
ws.column_dimensions['F'].width = 16

# --- Antetul (Bannerul Navy) ---
ws.merge_cells("A1:F2")
for r in range(1, 3):
    for c in range(1, 7):
        ws.cell(row=r, column=c).fill = navy_fill
top_title = ws.cell(row=1, column=1, value="CirculadOR")
top_title.font = Font(name="Arial", size=16, bold=True, color="FFFFFF")
top_title.alignment = Alignment(vertical="center", indent=1)

# --- Titlu Document ---
h1_text = soup.find("h1").get_text(strip=True) if soup.find("h1") else "Documentație"
sub_div = soup.find("div", class_="sub")
sub_text = sub_div.get_text(strip=True) if sub_div else ""

ws.cell(row=4, column=1, value=h1_text).font = title_font
ws.cell(row=5, column=1, value=sub_text).font = sub_font

# --- Procesarea Secțiunilor și a Tabelelor ---
current_row = 7
content_div = soup.find("div", class_="content")

if content_div:
    for element in content_div.find_all(recursive=False):
        # 1. Titluri de Secțiune (cu fundal gri-albăstrui)
        if element.name == "div" and "sec-h" in element.get("class", []):
            ws.merge_cells(start_row=current_row, start_column=1, end_row=current_row, end_column=6)
            for c in range(1, 7):
                ws.cell(row=current_row, column=c).fill = section_fill

            sec_cell = ws.cell(row=current_row, column=1, value=element.get_text(strip=True))
            sec_cell.font = section_font
            sec_cell.alignment = Alignment(vertical="center", indent=1)
            ws.row_dimensions[current_row].height = 24
            current_row += 2

        # 2. Tabele de date
        elif element.name == "table":
            # Verificăm dacă este tabel Key-Value (clasa kv) sau tabel normal
            is_kv = "kv" in element.get("class", [])

            # Verificăm dacă are cap de tabel (thead/th)
            headers = [th.get_text(strip=True) for th in element.find_all("th")]
            if headers:
                for col_idx, h in enumerate(headers, 1):
                    c_cell = ws.cell(row=current_row, column=col_idx, value=h)
                    c_cell.fill = navy_fill
                    c_cell.font = white_font
                    c_cell.border = thin_border
                ws.row_dimensions[current_row].height = 22
                current_row += 1

            # Populăm rândurile
            for tr in element.find_all("tr"):
                cells = tr.find_all(["td", "th"])
                if not cells or (headers and cells[0].name == "th"):
                    continue # Sărim peste rândul de antet dacă a fost procesat deja

                if is_kv:
                    # Tabele Key-Value (2 coloane întinse pe 6)
                    label = cells[0].get_text(strip=True)
                    value = cells[1].get_text(strip=True) if len(cells) > 1 else ""

                    ws.merge_cells(start_row=current_row, start_column=2, end_row=current_row, end_column=6)

                    cell_lbl = ws.cell(row=current_row, column=1, value=label)
                    cell_lbl.font = bold_font
                    cell_lbl.fill = light_grey_fill
                    cell_lbl.border = thin_border
                    cell_lbl.alignment = Alignment(vertical="top")

                    for col in range(2, 7):
                        ws.cell(row=current_row, column=col).border = thin_border

                    cell_val = ws.cell(row=current_row, column=2, value=value)
                    cell_val.font = regular_font
                    cell_val.alignment = Alignment(wrap_text=True, vertical="top")

                    # Ajustăm dinamic înălțimea pentru textele lungi (ex: Metodologie)
                    if len(value) > 100:
                        ws.row_dimensions[current_row].height = 60
                    else:
                        ws.row_dimensions[current_row].height = 20
                else:
                    # Tabele standard (Multi-coloană, ex: Compoziție)
                    for col_idx, cell in enumerate(cells, 1):
                        val = cell.get_text(strip=True)
                        c_cell = ws.cell(row=current_row, column=col_idx, value=val)
                        c_cell.font = regular_font
                        c_cell.border = thin_border
                        c_cell.alignment = Alignment(horizontal="left", vertical="center")
                    ws.row_dimensions[current_row].height = 20

                current_row += 1
            current_row += 1

        # 3. Texte simple/Note explicative
        elif element.name == "div" and "sec-h" not in element.get("class", []):
            text_content = element.get_text(strip=True)
            if text_content:
                ws.merge_cells(start_row=current_row, start_column=1, end_row=current_row, end_column=6)
                for col in range(1, 7):
                    ws.cell(row=current_row, column=col).border = thin_border

                text_cell = ws.cell(row=current_row, column=1, value=text_content)
                text_cell.font = regular_font
                text_cell.alignment = Alignment(wrap_text=True, vertical="center")

                # Dacă este o notă de document lipsă (gri)
                if "dashed" in element.get("style", "") or "6b7280" in element.get("style", ""):
                    text_cell.fill = light_grey_fill
                    text_cell.font = grey_font

                ws.row_dimensions[current_row].height = 40
                current_row += 2

# --- Subsolul Documentului ---
ft_div = soup.find("div", class_="ft")
if ft_div:
    ws.merge_cells(start_row=current_row, start_column=1, end_row=current_row, end_column=6)
    ft_cell = ws.cell(row=current_row, column=1, value=ft_div.get_text(strip=True))
    ft_cell.font = footer_font
    ft_cell.alignment = Alignment(wrap_text=True, vertical="center")
    ws.row_dimensions[current_row].height = 30

# Salvăm și descărcăm fișierul
output_filename = "Anexa_VII_Formatat.xlsx"
wb.save(output_filename)

print("\nConversia s-a terminat! Fișierul tău se descarcă acum...")
files.download(output_filename)


PDF conversion to Excel
Some styling is applied as per Gemini's identification in the actual template file.


# ==========================================
# 1. INSTALAREA LIBRĂRIILOR NECESARE
# ==========================================
!pip install pdfplumber openpyxl

import re
from google.colab import files
import pdfplumber
import openpyxl
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side

# ==========================================
# 2. ÎNCĂRCAREA MANUALĂ A FIȘIERULUI PDF
# ==========================================
print("Te rog selectează fișierul tău PDF cu Declarația de Conformitate:")
uploaded = files.upload()

file_name = list(uploaded.keys())[0]

# Extragerea textului brut din PDF folosind pdfplumber
pdf_text = ""
with pdfplumber.open(file_name) as pdf:
    for page in pdf.pages:
        text = page.extract_text()
        if text:
            pdf_text += text + "\n"

print(f"\nFișierul '{file_name}' a fost citit cu succes! Generăm fișierul Excel...")

# ==========================================
# 3. PARSAREA INFORMAȚIILOR DIN TEXT
# ==========================================
# Definim un dicționar pentru stocarea datelor extrase
date_doc = {
    "Nr. Identificare": "Nespecificat",
    "Versiune": "v1.0",
    "Producator": "Nespecificat",
    "Adresa": "Nespecificat",
    "Obiectul Declaratiei": "Nespecificat",
    "Material": "PET",
    "Greutate": "30 g",
    "Grad Reciclare": "B",
    "Data Emiterii": "01.07.2026",
    "Semnatar": "Admin Yvora"
}

# Căutări simple în text pentru populare inteligentă
if "DOC-0002-2033" in pdf_text:
    date_doc["Nr. Identificare"] = "DOC-0002-2033"
if "Yvora Demo SRL" in pdf_text:
    date_doc["Producator"] = "Yvora Demo SRL"
    date_doc["Adresa"] = "Strada Florilor 10, București, 010101, România"
if "Borcan de plastic pentru cosmetice" in pdf_text:
    date_doc["Obiectul Declaratiei"] = "Borcan de plastic pentru cosmetice"

# Extragerea articolelor bifate [X]
articole_bifate = []
for line in pdf_text.split("\n"):
    if "[X]" in line:
        articole_bifate.append(line.replace("[X]", "✔").strip())
    elif "[ ]" in line:
        articole_bifate.append(line.replace("[ ]", "❌").strip())

# ==========================================
# 4. CREAREA ȘI FORMATAREA FIȘIERULUI EXCEL
# ==========================================
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "Declarație Conformitate"
ws.views.sheetView[0].showGridLines = True

# Stiluri
navy_fill = PatternFill(start_color="1B3A5C", end_color="1B3A5C", fill_type="solid")
section_fill = PatternFill(start_color="EEF2F6", end_color="EEF2F6", fill_type="solid")
light_grey_fill = PatternFill(start_color="F9FAFB", end_color="F9FAFB", fill_type="solid")
warning_fill = PatternFill(start_color="FFFBEB", end_color="FFFBEB", fill_type="solid")

white_font = Font(name="Arial", size=11, bold=True, color="FFFFFF")
title_font = Font(name="Arial", size=14, bold=True, color="1B3A5C")
section_font = Font(name="Arial", size=11, bold=True, color="1B3A5C")
bold_font = Font(name="Arial", size=10, bold=True, color="1F2937")
regular_font = Font(name="Arial", size=10, color="1F2937")
warning_font = Font(name="Arial", size=9, italic=True, color="B45309")
footer_font = Font(name="Arial", size=8.5, italic=True, color="9CA3AF")

thin_border = Border(
    left=Side(style='thin', color='E5E7EB'), right=Side(style='thin', color='E5E7EB'),
    top=Side(style='thin', color='E5E7EB'), bottom=Side(style='thin', color='E5E7EB')
)

# Setați lățimea coloanelor
ws.column_dimensions['A'].width = 32
ws.column_dimensions['B'].width = 45

# --- 1. Antetul documentului ---
ws.merge_cells("A1:B2")
for r in range(1, 3):
    for c in range(1, 3):
        ws.cell(row=r, column=c).fill = navy_fill
top_title = ws.cell(row=1, column=1, value="CirculadOR")
top_title.font = Font(name="Arial", size=16, bold=True, color="FFFFFF")
top_title.alignment = Alignment(vertical="center", indent=1)

# --- 2. Caseta de Avertisment (Draft) ---
ws.merge_cells("A4:B5")
for r in range(4, 6):
    for c in range(1, 3):
        ws.cell(row=r, column=c).fill = warning_fill
draft_cell = ws.cell(row=4, column=1, value="DRAFT — NU ESTE ÎNCĂ SEMNAT. Acest document devine Declarație de Conformitate doar după ce este verificat, completat și semnat de producătorul responsabil.")
draft_cell.font = warning_font
draft_cell.alignment = Alignment(wrap_text=True, vertical="center")

# --- 3. Titlu principal ---
ws.cell(row=7, column=1, value="DECLARAȚIE UE DE CONFORMITATE").font = title_font
ws.cell(row=8, column=1, value="Conform Art. 39 și Anexa VIII din Reg. (UE) 2025/40 (PPWR)").font = footer_font

# --- 4. Secțiunea: Date Identificare ---
current_row = 10

def adauga_sectiune(titlu):
    global current_row
    ws.merge_cells(start_row=current_row, start_column=1, end_row=current_row, end_column=2)
    for c in range(1, 3):
        ws.cell(row=current_row, column=c).fill = section_fill
    sec_c = ws.cell(row=current_row, column=1, value=titlu)
    sec_c.font = section_font
    sec_c.alignment = Alignment(vertical="center", indent=1)
    ws.row_dimensions[current_row].height = 24
    current_row += 1

adauga_sectiune("1. Informații Generale Document")
randuri_gen = [
    ("Identificare Unică Ambalaj", date_doc["Nr. Identificare"]),
    ("Versiune Document", date_doc["Versiune"]),
    ("Producător", date_doc["Producator"]),
    ("Adresă Sediu", date_doc["Adresa"]),
    ("Obiectul Declarației", date_doc["Obiectul Declaratiei"])
]

for label, val in randuri_gen:
    c_lbl = ws.cell(row=current_row, column=1, value=label)
    c_lbl.font = bold_font
    c_lbl.fill = light_grey_fill
    c_lbl.border = thin_border
   
    c_val = ws.cell(row=current_row, column=2, value=val)
    c_val.font = regular_font
    c_val.border = thin_border
    c_val.alignment = Alignment(wrap_text=True)
    current_row += 1

current_row += 1

# --- 5. Secțiunea: Specificații Tehnice ---
adauga_sectiune("2. Specificații Material și Reciclabilitate")
randuri_tech = [
    ("Material Predominant", date_doc["Material"]),
    ("Greutate Ambalaj", date_doc["Greutate"]),
    ("Grad de Reciclabilitate Declarat", date_doc["Grad Reciclare"]),
    ("Conținut Minim Reciclat Actual", "30.00% (Țintă: 30.00%)")
]

for label, val in randuri_tech:
    c_lbl = ws.cell(row=current_row, column=1, value=label)
    c_lbl.font = bold_font
    c_lbl.fill = light_grey_fill
    c_lbl.border = thin_border
   
    c_val = ws.cell(row=current_row, column=2, value=val)
    c_val.font = regular_font
    c_val.border = thin_border
    current_row += 1

current_row += 1

# --- 6. Secțiunea: Conformitate Legislație (Checkbox-uri) ---
adauga_sectiune("3. Evaluarea Conformității (Regulament UE 2025/40)")
for art in articole_bifate:
    ws.merge_cells(start_row=current_row, start_column=1, end_row=current_row, end_column=2)
    art_cell = ws.cell(row=current_row, column=1, value=art)
    art_cell.font = regular_font
    art_cell.border = thin_border
    # Evidențiem vizual articolele bifate active
    if "✔" in art:
        art_cell.fill = light_grey_fill
    current_row += 1

current_row += 1

# --- 7. Secțiunea: Semnătură și Validare ---
adauga_sectiune("4. Semnătură și Validare Oficială")
randuri_semnatura = [
    ("Semnat pentru și în numele", date_doc["Producator"]),
    ("Locul și Data emiterii", date_doc["Data Emiterii"]),
    ("Nume și Funcție", date_doc["Semnatar"]),
    ("Status Semnătură", "Așteaptă semnătura olografă / digitală a producătorului")
]

for label, val in randuri_semnatura:
    c_lbl = ws.cell(row=current_row, column=1, value=label)
    c_lbl.font = bold_font
    c_lbl.fill = light_grey_fill
    c_lbl.border = thin_border
   
    c_val = ws.cell(row=current_row, column=2, value=val)
    c_val.font = regular_font
    c_val.border = thin_border
    if label == "Status Semnătură":
        c_val.font = Font(name="Arial", size=10, italic=True, color="6B7280")
    current_row += 1

# Salvarea și descărcarea rezultatului
output_excel = "Declaratie_Conformitate_Formatata.xlsx"
wb.save(output_excel)

print("\nConversia PDF -> Excel s-a finalizat! Descărcarea a început...")
files.download(output_excel)


pus acum 39 ore
   
Pagini: 1  

Mergi la