-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocxVersion.py
166 lines (142 loc) · 4.21 KB
/
DocxVersion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import PyE_tools as pye
import math
import decimal
import docx
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT, WD_TABLE_ALIGNMENT
data = []
dataToShow = []
exercise = []
documentName = "problem.txt"
resultDocumentName = "result.docx"
# Lectura de datos
try:
print(f'Abriendo: {documentName}...')
info = open(documentName, "r", -1, "utf-8").readlines()
except:
print("No se ha podido abrir el archivo problem.txt :(")
quit()
# Lecturas de documento problem.txt
isGivingData = False
isDescribing = False
print(f'Leyendo información de {documentName}...')
for i in info:
if i.strip() == "DE:":
isDescribing = True
isGivingData = False
continue
elif i.strip() == "DA:":
isDescribing = False
isGivingData = True
continue
if(isGivingData):
try:
data.append(decimal.Decimal(i.strip()))
dataToShow.append((i.strip()))
except:
print(f'No hemos podido convertir el siguiente dato: "{i.strip()}", lo omitiremos por ahora :)')
elif(isDescribing):
exercise.append(i.strip())
# Escribiendo el documento
document = docx.Document()
# Fuente
font = document.styles["Normal"].font
font.name = "Arial"
# Si no hay datos
if len(data) == 0:
paragraph = document.add_paragraph()
run = paragraph.add_run()
run.bold = True
run.add_text("¿Cómo se supone que se calcule algo que si no hay datos?")
print(f"Guardando en: {resultDocumentName}...")
document.save(resultDocumentName)
quit()
# Escritura del problema
if len(exercise) != 0:
paragraph = document.add_paragraph()
run = paragraph.add_run()
run.bold = True
run.add_text("Problema: ")
run = paragraph.add_run()
run.add_text("\n".join(exercise))
# Escritura de los primero datos
paragraph = document.add_paragraph()
run = paragraph.add_run()
run.bold = True
run.add_text("Datos: ")
run = paragraph.add_run()
run.add_text(", ".join(dataToShow) + ".")
# Escritura de los datos ordenados
print("Haciendo cálculos poderosos...")
dataSorted = pye.bubbleSort(data)
dataToShow = []
k = pye.klassesNumber(data)
r = pye.dataRange(dataSorted)
a = pye.dataAmplitudeByList(dataSorted, True)
uv = pye.variationUnit(dataSorted)
dataTable = pye.calculateFrecuencyByDataList(dataSorted,k,a,uv)
for i in dataSorted:
dataToShow.append(str(i))
paragraph = document.add_paragraph()
run = paragraph.add_run()
run.bold = True
run.add_text("Datos Ordenados: ")
run = paragraph.add_run()
run.add_text(", ".join(dataToShow) + ".")
# Número de clases
paragraph = document.add_paragraph()
run = paragraph.add_run()
run.bold = True
run.add_text("Número de Clases: ")
run = paragraph.add_run()
res = (1 + 3.322 * math.log10(len(data)))
res = res if type(res) is int else "%.3f" % res
run.add_text(
f"1 + 3.322 x log({len(dataSorted)}) = {res} ≈ {k}"
)
# Rango
paragraph = document.add_paragraph()
run = paragraph.add_run()
run.bold = True
run.add_text("Rango: ")
run = paragraph.add_run()
run.add_text(
f"{str(dataSorted[len(dataSorted)-1])} − {str(dataSorted[0])} = {str(dataSorted[len(dataSorted)-1] - dataSorted[0])} ≈ {r}"
)
# Amplitud
paragraph = document.add_paragraph()
run = paragraph.add_run()
run.bold = True
run.add_text("Amplitud: ")
run = paragraph.add_run()
res = r/k
res = res if type(res) is int else "%.3f" % res
run.add_text(f"{r} ÷ {k} = {res} ≈ {a}")
# Unidad de variacion
paragraph = document.add_paragraph()
run = paragraph.add_run()
run.bold = True
run.add_text("Unidad de variación: ")
run = paragraph.add_run()
run.add_text(str(uv))
# Tabla
print("Creando tabla...")
header = [
"Clase",
"Límite Inf.",
"Límite Sup.",
"Frec.",
"Marca d Clases",
"Lim Inf Exac",
"Lim Sup Exac",
]
table = document.add_table(rows=k + 1, cols=len(header))
table.autofit = True
for i in range (0,len(header)):
table.cell(0,i).vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
table.cell(0, i).text = header[i]
for i in range(0, len(dataTable)):
for k in range(0, len(dataTable[i])):
table.cell(i+1,k).vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
table.cell(i + 1, k).text = str(dataTable[i][k])
print(f"Guardando en: {resultDocumentName}...")
document.save(resultDocumentName)