-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParagraph.py
More file actions
516 lines (410 loc) · 14.8 KB
/
Paragraph.py
File metadata and controls
516 lines (410 loc) · 14.8 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import re
from helpers.EnumFill import EnumFill
from typing import Union
class Paragraph:
"""
Parameters:
----------
__text - attribute specifies paragraph text
__countOfSpSbl - attribute specifies number of special characters in a paragraph
__countSbl - attribute specifies number of characters in a paragraph
__lowercase - attribute specifies lowercase text in the entire paragraph
__uppercase - attribute specifies uppercase text in the entire paragraph
__lastSbl - attribute specifies last paragraph character
__firstkey - attribute specifies first paragraph character
__prevEl - attribute specifies the class of the previous structural element
__curEl - attribute specifies the class of the current structural element
__nexEl - attribute specifies the class of the next structural element
__alignment - attribute specifies text alignment
__indent - attribute specifies indent from the red line
__mrgrg - attribute specifies indent from the right side of the page
__mrglf - attribute specifies indent from the left side of the page
__lineSpacing - attribute specifies paragraph line spacing
__mrgtop - attribute specifies attribute specifies indent from the top side of the page
__mrgbtm - attribute specifies attribute specifies indent from the bottom side of the page
__fontName - attribute specifies paragraph font
__bold - attribute specifies paragraph text boldness
__italics - attribute specifies paragraph text italics
__underlining - attribute specifies paragraph text underlining
__subText - attribute specifies
__superText - attribute specifies
__textSize - attribute specifies text size
__colorText - attribute specifies text color
__pageBreakBefore - attribute specifies start of a new page
__keepLinesTogether - attribute specifies keeping the line style together
__keepWithNext - attribute specifies keeping paragraphs together
__outlineLevel - attribute specifies paragraph type
__noSpaceBetweenParagraphsOfSameStyle - attribute specifies no extra space between paragraphs of the same style
__nochangeFontName - attribute specifies no change in text font inside a paragraph
__nochangeTextSize - attribute specifies no change in text size inside a paragraph
Methods
----------
getCountOfSpSbl(cls, text)
Counts and returns the number of special characters in a text
getCountSbl(cls, text)
Counts and returns the number of all characters in a text
getlowercase(cls, text)
Calculates whether the entire paragraph is lowercase
getuppercase(cls, text)
Calculates whether the entire paragraph is uppercase
getlastSbl(cls, text)
Calculates the last character of a paragraph
getfirstkey(cls, text)
Calculates the type of the first character of a paragraph
"""
def __init__(self, text, indent, lineSpacing, fontName, textSize, nochangeFontName, nochangeTextSize,
alignment=None, mrgrg=None, mrglf=None, mrgtop=None, mrgbtm=None, bold: Union[EnumFill, int] = None,
italics: Union[EnumFill, int] = None,
underlining: Union[EnumFill, int] = None, subText=None, superText=None, colorText="White",
keepLinesTogether=None, keepWithNext=None, outlineLevel=None,
noSpaceBetweenParagraphsOfSameStyle=None, pageBreakBefore=None):
"""
Constructor for Paragraph.
Args:
bold (Union[EnumFill, int]): An instance of EnumFill or int representing the value to set for bold.
italics (Union[EnumFill, int]): An instance of EnumFill or int representing the value to set for italics.
underlining (Union[EnumFill, int]): An instance of EnumFill or int representing the value to set for underlining.
Examples:
>>> para = Paragraph(
text='Some text here/n',
indent='1.5',
lineSpacing='1',
fontName='Time New Roman",
textSize='12',
nochangeFontName=True,
nochangeTextSize=False,
alignment=None,
mrgrg=None,
mrglf=None,
mrgtop=None,
mrgbtm=None,
bold=None,
italics=EnumFill(1),
underlining=2,
subText=None,
superText=None,
colorText="White",
keepLinesTogether=None,
keepWithNext=None,
outlineLevel=None,
noSpaceBetweenParagraphsOfSameStyle=None,
pageBreakBefore=None)
"""
self.__text = text
self.__countOfSpSbl = Paragraph.getCountOfSpSbl(text)
self.__countSbl = Paragraph.getCountSbl(text)
self.__lowercase = Paragraph.getlowercase(text)
self.__uppercase = Paragraph.getuppercase(text)
self.__lastSbl = Paragraph.getlastSbl(text)
self.__firstkey = Paragraph.getfirstkey(text)
self.__prevEl = None
self.__curEl = None
self.__nexEl = None
self.__alignment = alignment
self.__indent = indent
self.__mrgrg = mrgrg
self.__mrglf = mrglf
self.__lineSpacing = lineSpacing
self.__mrgtop = mrgtop
self.__mrgbtm = mrgbtm
self.__fontName = fontName
self.__bold = EnumFill(bold)
self.__italics = EnumFill(italics)
self.__underlining = EnumFill(underlining)
self.__subText = subText
self.__superText = superText
self.__textSize = textSize
self.__colorText = colorText
self.__pageBreakBefore = pageBreakBefore
self.__keepLinesTogether = keepLinesTogether
self.__keepWithNext = keepWithNext
self.__outlineLevel = outlineLevel
self.__noSpaceBetweenParagraphsOfSameStyle = noSpaceBetweenParagraphsOfSameStyle
self.__nochangeFontName = nochangeFontName
self.__nochangeTextSize = nochangeTextSize
@classmethod
def getCountOfSpSbl(cls, text):
"""
Counts and returns the number of special characters in a text
:param text: Paragraph text
:return: The number of special characters in the text, such as dots and commas
"""
return len(re.findall("[,.!?;:\'\"«»~]", text))
@classmethod
def getCountSbl(cls, text):
"""
Counts and returns the number of all characters in a text
:param text: Paragraph text
:return: The number of all characters in the text
"""
return len(text)
@classmethod
def getlowercase(cls, text):
"""
Calculates whether the entire paragraph is lowercase
:param text: Paragraph text
:return: True if all text is in lowercase
"""
return True if text.islower() else False
@classmethod
def getuppercase(cls, text):
"""
Calculates whether the entire paragraph is uppercase
:param text: Paragraph text
:return: True if all text is in uppercase
"""
return True if text.isupper() else False
@classmethod
def getlastSbl(cls, text):
"""
Calculates the last character of a paragraph
:param text: Paragraph text
:return: The last character of a paragraph
"""
if len(text) <= 1:
return None
if re.match(r'[A-Za-zА-Яа-я0-9()]', text[len(text) - 2]) is None:
return text[len(text) - 2]
else:
return None
@classmethod
def getfirstkey(cls, text):
"""
Calculates the type of the first character of a paragraph
:param text: Paragraph text
:return: The type of the first character of a paragraph
"""
import re
first_key = text.split(' ')[0]
if re.match(r'−', first_key) is None:
if re.match(r'(\d*)', first_key) is None:
if re.match(r'(\d*.\d*)', first_key) is None:
if re.match(r'(\d*.\d*(.\d*)*)', first_key) is None:
if re.match(r'Таблица', first_key) is None:
if re.match(r'[(Рисунок)(Рис)(Рис.)]', first_key) is None:
return ''
else:
return 'Рисунок'
else:
return 'Таблица'
else:
return 'TitleLevel23'
else:
return 'TitleLevel23'
else:
return 'TitleLevel1'
else:
return 'listLevel1'
@property
def prevEl(self):
return self.__prevEl
@prevEl.setter
def prevEl(self, prevEl):
self.__prevEl = prevEl
@property
def curEl(self):
return self.__curEl
@curEl.setter
def curEl(self, curEl):
self.__curEl = curEl
@property
def nextEl(self):
return self.__nextEl
@nextEl.setter
def fontName(self, nextEl):
self.__nextEl = nextEl
@property
def text(self):
return self.__text
@text.setter
def text(self, text):
self.__text = text
@property
def keepLinesTogether(self):
return self.__keepLinesTogether
@keepLinesTogether.setter
def keepLinesTogether(self, keepLinesTogether):
self.__keepLinesTogether = keepLinesTogether
@property
def outlineLevel(self):
return self.__outlineLevel
@outlineLevel.setter
def outlineLevel(self, outlineLevel):
self.__outlineLevel = outlineLevel
@property
def noSpaceBetweenParagraphsOfSameStyle(self):
return self.__noSpaceBetweenParagraphsOfSameStyle
@noSpaceBetweenParagraphsOfSameStyle.setter
def noSpaceBetweenParagraphsOfSameStyle(self, noSpaceBetweenParagraphsOfSameStyle):
self.__noSpaceBetweenParagraphsOfSameStyle = noSpaceBetweenParagraphsOfSameStyle
@property
def keepWithNext(self):
return self.__keepWithNext
@keepWithNext.setter
def keepWithNext(self, keepWithNext):
self.__keepWithNext = keepWithNext
@property
def indent(self):
return self.__indent
@indent.setter
def indent(self, indent):
self.__indent = indent
@property
def mrgrg(self):
return self.__mrgrg
@mrgrg.setter
def mrgrg(self, mrgrg):
self.__mrgrg = mrgrg
@property
def mrglf(self):
return self.__mrglf
@mrglf.setter
def mrglf(self, mrglf):
self.__mrglf = mrglf
@property
def mrgtop(self):
return self.__mrgtop
@mrgtop.setter
def mrgtop(self, mrgtop):
self.__mrgtop = mrgtop
@property
def mrgbtm(self):
return self.__mrgbtm
@mrgbtm.setter
def mrgbtm(self, mrgbtm):
self.__mrgbtm = mrgbtm
@property
def fontName(self):
return self.__fontName
@fontName.setter
def fontName(self, fontName):
self.__fontName = fontName
@property
def colorText(self):
return self.__colorText
@colorText.setter
def colorText(self, colorText):
self.__colorText = colorText
@property
def lineSpacing(self):
return self.__lineSpacing
@lineSpacing.setter
def lineSpacing(self, value):
if value >= 0:
self.__lineSpacing = value
else:
raise ValueError
@property
def bold(self):
return self.__bold
@bold.setter
def bold(self, b):
try:
if b is not None:
self.__bold = EnumFill(b)
except ValueError:
print(
"You must use one of this state: NO_APPLY = 0 ; APPLY_TO_ALL_ELEMENTS = 1 ; APPLY_TO_SOME_ELEMENTS = 2; IS_UNKNOWN = None")
@property
def italics(self):
return self.__italics
@italics.setter
def italics(self, i):
try:
if i is not None:
self.__bold = EnumFill(i)
except ValueError:
print(
"You must use one of this state: NO_APPLY = 0 ; APPLY_TO_ALL_ELEMENTS = 1 ; APPLY_TO_SOME_ELEMENTS = 2; IS_UNKNOWN = None")
@property
def underlining(self):
return self.__underlining
@underlining.setter
def underlining(self, u):
try:
if u is not None:
self.__bold = EnumFill(u)
except ValueError:
print(
"You must use one of this state: NO_APPLY = 0 ; APPLY_TO_ALL_ELEMENTS = 1 ; APPLY_TO_SOME_ELEMENTS = 2; IS_UNKNOWN = None")
@property
def subText(self):
return self.__subText
@subText.setter
def subText(self, s):
if s is True or s is False:
self.__subText = s
else:
raise ValueError
@property
def superText(self):
return self.__superText
@superText.setter
def superText(self, s):
if s is True or s is False:
self.__superText = s
else:
raise ValueError
@property
def textSize(self):
return self.__textSize
@textSize.setter
def textSize(self, s):
if s >= 8:
self.__textSize = s
else:
raise ValueError
@property
def countOfSpSbl(self):
return self.__countOfSpSbl
@countOfSpSbl.setter
def countOfSpSbl(self, countOfSpSbl):
self.__countOfSpSbl = countOfSpSbl
@property
def countSbl(self):
return self.__countSbl
@countSbl.setter
def countSbl(self, countSbl):
self.__countSbl = countSbl
@property
def lowercase(self):
return self.__lowercase
@lowercase.setter
def lowercase(self, lowercase):
self.__lowercase = lowercase
@property
def uppercase(self):
return self.__uppercase
@uppercase.setter
def uppercase(self, uppercase):
self.__uppercase = uppercase
@property
def lastSbl(self):
return self.__lastSbl
@lastSbl.setter
def lastSbl(self, lastSbl):
self.__lastSbl = lastSbl
@property
def firstkey(self):
return self.__firstkey
@firstkey.setter
def firstkey(self, firstkey):
self.__firstkey = firstkey
@property
def prevEl(self):
return self.__prevEl
@prevEl.setter
def prevEl(self, prevEl):
self.__prevEl = prevEl
@property
def curEl(self):
return self.__curEl
@curEl.setter
def curEl(self, curEl):
self.__curEl = curEl
@property
def nexEl(self):
return self.__nexEl
@nexEl.setter
def nexEl(self, nexEl):
self.__nexEl = nexEl