Source code for viiapackage.reporting.helper_functions.viia_append_report

### ===================================================================================================================
###   FUNCTION: Extending functionality for word document
### ===================================================================================================================
# Copyright ©VIIA 2024

### ===================================================================================================================
###   1. Import modules
### ===================================================================================================================

# General imports
from copy import deepcopy
from typing import Optional


### ===================================================================================================================
###   2. Function to delete a paragraph in the document
### ===================================================================================================================

[docs]def del_paragraph_in_word( bsc, init_text: str, final_text: Optional[str] = None, final_word: Optional[str] = None) -> int: """ Delete certain paragraph in word document. The paragraph is retrieved based on te initial text. If no end string is provided only this paragraph will be removed. The final text or final word (in that order) are checked to find the end of the paragraph to be removed. Input: - bsc (obj): Object of word document. - init_text (str): Initial text of the paragraph to be deleted. - final_text (str): Optional final text of the paragraph to be deleted. Default value is None. - final_word (str): Final word of the paragraph to be deleted. Default value is None. Output: - Removes the paragraph from the Word document. - Returns the index as integer of last paragraph in the BSC. """ # Get the index of the paragraphs from BSC that needs to be deleted i, j, k = 0, 0, 0 for i, par in enumerate(bsc.paragraphs): if par.text == init_text: j = i elif final_text: if final_text in par.text: k = i+1 break elif final_word: if par.text.split(' ')[-1] == final_word: k = i break # Delete the paragraphs index = j - 1 diff = k - j while diff > 0: p = bsc.paragraphs[j]._element p.getparent().remove(p) p._p = p.element = None diff -= 1 return index
### =================================================================================================================== ### 3. Function to add a paragraph in the document ### ===================================================================================================================
[docs]def add_paragraph_in_word( bsc, tva, index_to_add: int, init_text: str, add_table: bool, index_for_table: Optional[int] = None, table: Optional = None, caption_for_table: Optional = None, final_text: Optional = None, final_word: Optional = None): """ Add paragraph in the Word document from another word document. The paragraph is retrieved based on te initial text. If no end string is provided only this paragraph will be transferred. The final text or final word (in that order) are checked to find the end of the paragraph to be transferred. Input: - bsc (obj): Object of BSC word document in which paragraph should be added. - tva (obj): Object of TVA word document from which paragraph is taken. - index_to_add (int): Index for BSC word document in order to add the paragraph before this. - init_text (str): Initial text from the paragraph in TVA document that is to be added. - final_text (str): Final text from the paragraph in TVA document that is to be added. - final_word (str): Final word from the paragraph in TVA document that is to be added. - add_table (bool): Boolean to add table. - index_for_table (int): Index for BSC word document to add table before that - table (obj): Object from word document with table from the TVA document that needs to be added. - caption_for_table (str): Caption of the table to be added. Output: - The paragraph is added to the Word document. """ # Get the index of the paragraphs from TVA that needs to be added to the BSC document i, j, k = 0, 0, 0 for i, par in enumerate(tva.paragraphs): if par.text == init_text: j = i - 1 elif final_text: if final_text in par.text: k = i + 1 break elif final_word: if par.text.split(' ')[-1] == final_word: k = i + 1 break # Add the paragraph and table to the BSC document while k > j: if add_table: if tva.paragraphs[k].text == caption_for_table: tbl, p = table._tbl, bsc.paragraphs[index_for_table]._p tbl_add = deepcopy(tbl) p.addnext(tbl_add) add_table = False bsc.paragraphs[index_to_add].insert_paragraph_before( text=tva.paragraphs[k].text, style=tva.paragraphs[k].style) k -= 1
### =================================================================================================================== ### 4. End of script ### ===================================================================================================================