Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
from docx import Document
|
3 |
import os
|
|
|
4 |
import zipfile
|
5 |
-
import tempfile
|
6 |
|
7 |
def split_by_headers(file_path, headers_per_chunk=1):
|
8 |
doc = Document(file_path)
|
@@ -11,6 +11,7 @@ def split_by_headers(file_path, headers_per_chunk=1):
|
|
11 |
header_count = 0
|
12 |
|
13 |
for paragraph in doc.paragraphs:
|
|
|
14 |
if paragraph.style.name.startswith('Heading'):
|
15 |
header_count += 1
|
16 |
if header_count > headers_per_chunk:
|
@@ -18,6 +19,7 @@ def split_by_headers(file_path, headers_per_chunk=1):
|
|
18 |
current_chunk = Document()
|
19 |
header_count = 1
|
20 |
|
|
|
21 |
current_chunk.add_paragraph(paragraph.text, style=paragraph.style.name)
|
22 |
|
23 |
if len(current_chunk.paragraphs):
|
@@ -30,7 +32,7 @@ def split_by_pages(file_path, pages_per_chunk=1):
|
|
30 |
chunks = []
|
31 |
current_chunk = Document()
|
32 |
page_count = 0
|
33 |
-
estimated_chars_per_page = 3000
|
34 |
char_count = 0
|
35 |
|
36 |
for paragraph in doc.paragraphs:
|
@@ -53,90 +55,59 @@ def split_by_pages(file_path, pages_per_chunk=1):
|
|
53 |
|
54 |
return chunks
|
55 |
|
56 |
-
def save_chunks(chunks, original_filename
|
57 |
saved_files = []
|
58 |
-
base_name = os.path.splitext(
|
59 |
|
60 |
for i, chunk in enumerate(chunks, 1):
|
61 |
-
output_path =
|
62 |
chunk.save(output_path)
|
63 |
saved_files.append(output_path)
|
64 |
|
65 |
return saved_files
|
66 |
|
67 |
-
def
|
68 |
-
with zipfile.ZipFile(
|
69 |
-
for
|
70 |
-
zipf.write(
|
71 |
-
return
|
72 |
|
73 |
def process_document(file, split_type, headers_or_pages, download_type):
|
74 |
if headers_or_pages < 1:
|
75 |
-
return
|
76 |
|
77 |
try:
|
78 |
-
#
|
79 |
-
temp_dir = tempfile.mkdtemp()
|
80 |
-
|
81 |
-
# Procesar el documento
|
82 |
if split_type == "Encabezados":
|
83 |
chunks = split_by_headers(file.name, headers_or_pages)
|
84 |
else: # Páginas
|
85 |
chunks = split_by_pages(file.name, headers_or_pages)
|
86 |
|
87 |
-
|
88 |
-
saved_files = save_chunks(chunks, file.name, temp_dir)
|
89 |
|
|
|
90 |
if download_type == "ZIP":
|
91 |
-
|
92 |
-
zip_path
|
93 |
-
|
94 |
-
return
|
95 |
-
|
96 |
-
return saved_files, f"Documento dividido en {len(saved_files)} partes."
|
97 |
-
|
98 |
except Exception as e:
|
99 |
-
return
|
100 |
-
|
101 |
-
finally:
|
102 |
-
# Programar la limpieza del directorio temporal
|
103 |
-
# (Gradio se encargará de esto después de la descarga)
|
104 |
-
pass
|
105 |
|
106 |
# Interfaz Gradio
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
headers_pages = gr.Number(
|
120 |
-
value=1,
|
121 |
-
label="Número de encabezados/páginas por fragmento",
|
122 |
-
minimum=1
|
123 |
-
)
|
124 |
-
download_type = gr.Radio(
|
125 |
-
["Individual", "ZIP"],
|
126 |
-
label="Tipo de descarga",
|
127 |
-
value="ZIP"
|
128 |
-
)
|
129 |
-
|
130 |
-
process_btn = gr.Button("Procesar Documento")
|
131 |
-
output_text = gr.Text(label="Estado")
|
132 |
-
|
133 |
-
file_output = gr.File(label="Archivos Procesados")
|
134 |
-
|
135 |
-
process_btn.click(
|
136 |
-
fn=process_document,
|
137 |
-
inputs=[file_input, split_type, headers_pages, download_type],
|
138 |
-
outputs=[file_output, output_text]
|
139 |
-
)
|
140 |
|
141 |
if __name__ == "__main__":
|
142 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from docx import Document
|
3 |
import os
|
4 |
+
import shutil
|
5 |
import zipfile
|
|
|
6 |
|
7 |
def split_by_headers(file_path, headers_per_chunk=1):
|
8 |
doc = Document(file_path)
|
|
|
11 |
header_count = 0
|
12 |
|
13 |
for paragraph in doc.paragraphs:
|
14 |
+
# Verificar si el párrafo es un encabezado
|
15 |
if paragraph.style.name.startswith('Heading'):
|
16 |
header_count += 1
|
17 |
if header_count > headers_per_chunk:
|
|
|
19 |
current_chunk = Document()
|
20 |
header_count = 1
|
21 |
|
22 |
+
# Copiar el párrafo al chunk actual
|
23 |
current_chunk.add_paragraph(paragraph.text, style=paragraph.style.name)
|
24 |
|
25 |
if len(current_chunk.paragraphs):
|
|
|
32 |
chunks = []
|
33 |
current_chunk = Document()
|
34 |
page_count = 0
|
35 |
+
estimated_chars_per_page = 3000 # Esta es una estimación
|
36 |
char_count = 0
|
37 |
|
38 |
for paragraph in doc.paragraphs:
|
|
|
55 |
|
56 |
return chunks
|
57 |
|
58 |
+
def save_chunks(chunks, original_filename):
|
59 |
saved_files = []
|
60 |
+
base_name = os.path.splitext(original_filename)[0]
|
61 |
|
62 |
for i, chunk in enumerate(chunks, 1):
|
63 |
+
output_path = f"{base_name}_part{i}.docx"
|
64 |
chunk.save(output_path)
|
65 |
saved_files.append(output_path)
|
66 |
|
67 |
return saved_files
|
68 |
|
69 |
+
def zip_files(files, zip_name="document_parts.zip"):
|
70 |
+
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
71 |
+
for file in files:
|
72 |
+
zipf.write(file, os.path.basename(file))
|
73 |
+
return zip_name
|
74 |
|
75 |
def process_document(file, split_type, headers_or_pages, download_type):
|
76 |
if headers_or_pages < 1:
|
77 |
+
return "Por favor, especifique un número positivo de encabezados o páginas por fragmento."
|
78 |
|
79 |
try:
|
80 |
+
# Dividir el documento según el tipo seleccionado
|
|
|
|
|
|
|
81 |
if split_type == "Encabezados":
|
82 |
chunks = split_by_headers(file.name, headers_or_pages)
|
83 |
else: # Páginas
|
84 |
chunks = split_by_pages(file.name, headers_or_pages)
|
85 |
|
86 |
+
saved_files = save_chunks(chunks, os.path.basename(file.name))
|
|
|
87 |
|
88 |
+
# Si el usuario selecciona descargar en ZIP, comprimir los archivos
|
89 |
if download_type == "ZIP":
|
90 |
+
zip_path = zip_files(saved_files)
|
91 |
+
return zip_path # Devolver el archivo zip para descargar
|
92 |
+
else:
|
93 |
+
return saved_files # Devolver los archivos separados para descargar
|
94 |
+
|
|
|
|
|
95 |
except Exception as e:
|
96 |
+
return f"Error al procesar el documento: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
# Interfaz Gradio
|
99 |
+
iface = gr.Interface(
|
100 |
+
fn=process_document,
|
101 |
+
inputs=[
|
102 |
+
gr.File(label="Seleccione el archivo DOCX"),
|
103 |
+
gr.Radio(["Encabezados", "Páginas"], label="Método de división"),
|
104 |
+
gr.Number(value=1, label="Número de encabezados/páginas por fragmento", minimum=1),
|
105 |
+
gr.Radio(["Separados", "ZIP"], label="Tipo de descarga")
|
106 |
+
],
|
107 |
+
outputs=gr.File(label="Descargar archivo(s)"),
|
108 |
+
title="Divisor de Documentos DOCX",
|
109 |
+
description="Divida documentos DOCX por encabezados o páginas estimadas y descárguelos como archivos separados o en un archivo ZIP."
|
110 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
if __name__ == "__main__":
|
113 |
+
iface.launch()
|