import sys
from bs4 import BeautifulSoup
from docx import Document

def html_table_to_docx(html_path):
    with open(html_path, 'r', encoding='utf-8') as file:
        soup = BeautifulSoup(file, 'html.parser')

    table = soup.find('table')
    if not table:
        print("No se encontró una tabla en el HTML")
        return

    doc = Document()
    doc.add_heading("Tabla Pegada desde Excel", level=1)

    rows = table.find_all('tr')
    if not rows:
        return

    doc_table = doc.add_table(rows=0, cols=len(rows[0].find_all(['td', 'th'])))

    for row in rows:
        cells = row.find_all(['td', 'th'])
        doc_row = doc_table.add_row().cells
        for i, cell in enumerate(cells):
            doc_row[i].text = cell.get_text(strip=True)

    doc.save("tabla_generada.docx")
    print("Documento generado correctamente.")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Uso: python generar_doc.py archivo_html")
    else:
        html_table_to_docx(sys.argv[1])
