| import docx |
|
|
| from src.domain.container import Container |
| from src.domain.paragraph import Paragraph |
| from src.domain.styles import Styles |
| import shutil |
| import os |
|
|
|
|
| class Doc: |
|
|
| def __init__(self, path='', id_=None): |
|
|
| self.xdoc = docx.Document(path) |
| self.title = path.split('/')[-1] |
| self.name = self.title.split('.')[0] |
| self.id_ = id(self) |
| self.path = path |
| paragraphs = [Paragraph(xp, self.id_, i) for (i, xp) in enumerate(self.xdoc.paragraphs)] |
| self.container = Container(paragraphs, father=self) |
| self.styles = Styles(self.xdoc.styles) |
| self.tasks = [c.get_fulltask(self.container.one_liner) for c in self.container.containers if c.task] |
|
|
| def copy(self, new_doc_path): |
| shutil.copyfile(self.path, new_doc_path) |
| new_doc = Doc(new_doc_path) |
| new_doc.save_as_docx(new_doc_path) |
| return new_doc |
|
|
| def clear(self): |
| os.remove(self.path) |
|
|
| def apply_template(self, template, add_front_pages=True): |
| if add_front_pages: |
| self.add_front_pages_from(template) |
| log = self.styles.apply_from(template.styles) |
| self.save_as_docx() |
| return log |
|
|
| def copy_one_style(self, src_style_name: str, dest_style_name: str, template): |
| style_dest = template.styles.get_style_from_name(dest_style_name) |
| src_style = self.styles.get_style_from_name(src_style_name) |
| log = self.styles.copy_one_style(src_style, style_dest) |
| return log |
|
|
| def get_different_styles_with_template(self, template): |
| different_styles = self.styles.get_different_styles(template.styles) |
| return different_styles |
|
|
| def save_as_docx(self, path: str = ''): |
| path = path if path else self.path |
| self.path = path |
| self.xdoc.save(path) |
|
|
| def add_front_pages_from(self, src_doc): |
| src_paragraphs = [p for p in src_doc.xdoc.paragraphs] |
| src_paragraphs.reverse() |
| for p in src_paragraphs: |
| self.xdoc.paragraphs[0].insert_paragraph_before(text=p.text, style=p.style) |
| paragraphs = [Paragraph(xp, self.id_, i) for (i, xp) in enumerate(self.xdoc.paragraphs)] |
| self.container = Container(paragraphs, father=self) |
|
|
| def get_blocks(self): |
|
|
| def from_list_to_str(index_list): |
| index_str = str(index_list[0]) |
| for el in index_list[1:]: |
| index_str += '.' + str(el) |
| return index_str |
|
|
| blocks = self.container.blocks |
| for block in blocks: |
| block.doc = self.title |
| if block.level == 0: |
| blocks.remove(block) |
| block.index = from_list_to_str(block.index) |
| return blocks |
|
|
|
|
| @property |
| def structure(self): |
|
|
| return self.container.structure |
|
|
| def replace_tasks(self, resolutions: [str]): |
| if len(resolutions) == len(self.tasks): |
| p_tasks = [p for p in self.get_paragraphs() if p.type == 'task'] |
| for p, r in zip(p_tasks, resolutions): |
| p.set_text(r) |
| else: |
| print(f"résolutions : {len(resolutions)} != {len(self.tasks)} tasks") |
| return self |
|
|
| def get_paragraphs(self): |
| return self.container.all_paragraphs |
|
|