| import string |
| from src.tools.doc_tools import get_positions, convert_to_png |
| from docx.enum.text import WD_ALIGN_PARAGRAPH |
| import xml.etree.ElementTree as ET |
| from docx.oxml.ns import qn |
| import zipfile |
| import os |
| import re |
|
|
|
|
| INFINITE = 10000 |
|
|
| class Paragraph: |
|
|
| def __init__(self, xparagraph, doc_id: int, id_: int): |
|
|
| self.xparagraph = xparagraph |
| self.id_ = int(str(2) + str(doc_id) + str(id_)) |
| style_name = self.xparagraph.style.name |
| self.level = self.get_level_from_name(style_name) |
| self.is_structure = self.level < INFINITE |
| self.text = self.xparagraph.text |
| self.type, self.parsed_text = self.parse_text() |
|
|
|
|
| @property |
| def structure(self): |
| structure = {str(self.id_): { |
| 'index': str(self.id_), |
| 'canMove': True, |
| 'isFolder': False, |
| 'children': [], |
| 'title': self.text, |
| 'canRename': True, |
| 'data': {}, |
| 'level': self.level, |
| }} |
| return structure |
|
|
| @property |
| def blank(self): |
| """ |
| checks if the paragraph is blank: i.e. it brings some signal (it may otherwise be ignored) |
| """ |
| text = self.text.replace('\n', '') |
| return set(text).isdisjoint(string.ascii_letters) |
|
|
| @staticmethod |
| def get_level_from_name(style_name: str) -> int: |
| level = INFINITE |
| if '.Titre' in style_name: |
| suffix = style_name[-1] |
| try: |
| level = int(suffix) |
| except: |
| pass |
| return level |
|
|
| def parse_text(self) -> (str, str): |
|
|
| if self.is_structure: |
| return 'structure', self.text |
|
|
| startswith = {"?? ": "task", "++ ": "comment"} |
| for start in startswith.keys(): |
| split = self.text.rsplit(start) |
| if 1 < len(split): |
| return startswith[start], split[1] |
|
|
| return "normal", self.text |
|
|
| def set_text(self, text: str): |
| self.text = text |
| self.xparagraph.text = text |
| return self |
|
|
| def contains_image(self) -> bool: |
| return any("pic:pic" in run.element.xml for run in self.xparagraph.runs) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| |
| |
| def center_paragraph(self): |
| if self.contains_image(): |
| self.xparagraph.alignment = WD_ALIGN_PARAGRAPH.CENTER |
|
|
| def justify_paragraph(self): |
| if(self.xparagraph.style.name == "Normal"): |
| self.xparagraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| def get_styles_in_paragraph(self): |
| styles = [self.xparagraph.style.name] |
| for run in self.xparagraph.runs: |
| if run.style.name != "Default Paragraph Font": |
| styles.append(run.style.name) |
| return styles |
|
|
| |
|
|