示例#1
0
    def create_TOC(document):
        # Snippet from:
        # https://github.com/python-openxml/python-docx/issues/36
        paragraph = document.add_paragraph()
        run = paragraph.add_run()
        fldChar = OxmlElement('w:fldChar')  # creates a new element
        fldChar.set(qn('w:fldCharType'), 'begin')  # sets attribute on element
        instrText = OxmlElement('w:instrText')
        instrText.set(qn('xml:space'), 'preserve')  # sets attribute on element
        instrText.text = 'TOC \\o "1-1" \\h \\z'   # change 1-3 depending on heading levels you need

        fldChar2 = OxmlElement('w:fldChar')
        fldChar2.set(qn('w:fldCharType'), 'separate')
        fldChar3 = OxmlElement('w:t')
        fldChar3.text = "Right-click to update field."
        fldChar2.append(fldChar3)

        fldChar4 = OxmlElement('w:fldChar')
        fldChar4.set(qn('w:fldCharType'), 'end')

        r_element = run._r
        r_element.append(fldChar)
        r_element.append(instrText)
        r_element.append(fldChar2)
        r_element.append(fldChar4)
        p_element = paragraph._p
示例#2
0
    def generate_toc(self):
        self.generate_structural_heading(self.settings.get(
            'tocTitle', 'Содержание'),
                                         is_toc=True)

        paragraph = self.document.add_paragraph()
        run = paragraph.add_run()
        fldChar = OxmlElement('w:fldChar')  # creates a new element
        fldChar.set(qn('w:fldCharType'), 'begin')  # sets attribute on element
        instrText = OxmlElement('w:instrText')
        instrText.set(qn('xml:space'), 'preserve')  # sets attribute on element
        instrText.text = 'TOC \\o "1-3" \\h \\z \\u'  # change 1-3 depending on heading levels you need

        fldChar2 = OxmlElement('w:fldChar')
        fldChar2.set(qn('w:fldCharType'), 'separate')
        fldChar3 = OxmlElement('w:t')
        fldChar3.text = "НАЖМИТЕ СЮДА ПРАВОЙ КНОПКОЙ И ОБНОВИТЕ"
        fldChar2.append(fldChar3)

        fldChar4 = OxmlElement('w:fldChar')
        fldChar4.set(qn('w:fldCharType'), 'end')

        r_element = run._r
        r_element.append(fldChar)
        r_element.append(instrText)
        r_element.append(fldChar2)
        r_element.append(fldChar4)
        p_element = paragraph._p
示例#3
0
    def render(self, doc: docx.document.Document) -> None:
        if self.titre:
            doc.add_paragraph(self.titre, "Illustration Index Heading")
        paragraph = doc.add_paragraph()
        run = paragraph.add_run()
        fldChar = OxmlElement('w:fldChar')  # creates a new element
        fldChar.set(qn('w:fldCharType'), 'begin')  # sets attribute on element
        instrText = OxmlElement('w:instrText')
        instrText.set(qn('xml:space'), 'preserve')  # sets attribute on element
        instrText.text = self.command  # change 1-3 depending on heading levels you need

        fldChar2 = OxmlElement('w:fldChar')
        fldChar2.set(qn('w:fldCharType'), 'separate')
        fldChar3 = OxmlElement('w:t')
        fldChar3.text = "Right-click to update field."
        fldChar2.append(fldChar3)

        fldChar4 = OxmlElement('w:fldChar')
        fldChar4.set(qn('w:fldCharType'), 'end')

        r_element = run._r
        r_element.append(fldChar)
        r_element.append(instrText)
        r_element.append(fldChar2)
        r_element.append(fldChar4)
        p_element = paragraph._p
示例#4
0
def make_toc(doc):
    '''
    this function creates a table of contents object within a docx object, which will be called when the master files are created

    it indexes any text with "heading styles"

    if article "sections" are heading 1, and article titles are heading 2, it will take care of all of the proper formatting/indenting
    '''
    paragraph = doc.add_paragraph()
    run = paragraph.add_run()
    fldChar = OxmlElement('w:fldChar')  # creates a new element
    fldChar.set(qn('w:fldCharType'), 'begin')  # sets attribute on element
    instrText = OxmlElement('w:instrText')
    instrText.set(qn('xml:space'), 'preserve')  # sets attribute on element
    instrText.text = 'TOC \\o "1-3" \\h \\z \\u'  # change 1-3 depending on heading levels you need

    fldChar2 = OxmlElement('w:fldChar')
    fldChar2.set(qn('w:fldCharType'), 'separate')
    fldChar3 = OxmlElement('w:t')
    fldChar3.text = "Right-click to update field."
    fldChar2.append(fldChar3)

    fldChar4 = OxmlElement('w:fldChar')
    fldChar4.set(qn('w:fldCharType'), 'end')

    r_element = run._r
    r_element.append(fldChar)
    r_element.append(instrText)
    r_element.append(fldChar2)
    r_element.append(fldChar4)
    p_element = paragraph._p

    return doc
def add_field(self: Run, field: str):
    """
    Adds a Word Field to the Paragraph Run by modifying the document's xml
    This feature is not part of python-docx yet and thus a custom function is needed.
    Method is added dynamically to the Run class.
    :param self: Location (run) in document to add Field
    :param field: Specific field-type to add
    """
    fld_char_begin = OxmlElement('w:fldChar')
    fld_char_begin.set(qn('w:fldCharType'), 'begin')
    instr_text = OxmlElement('w:instrText')
    instr_text.set(qn('xml:space'), 'preserve')
    instr_text.text = field

    fld_char_separate = OxmlElement('w:fldChar')
    fld_char_separate.set(qn('w:fldCharType'), 'separate')
    fld_char_text = OxmlElement('w:t')
    fld_char_text.text = 'Right-click to update field.'

    fld_char_end = OxmlElement('w:fldChar')
    fld_char_end.set(qn('w:fldCharType'), 'end')

    r_element = self._r
    r_element.append(fld_char_begin)
    r_element.append(instr_text)
    r_element.append(fld_char_separate)
    r_element.append(fld_char_text)
    r_element.append(fld_char_end)
示例#6
0
def add_lof(doc):
    paragraph = doc.add_paragraph()
    run = paragraph.add_run()
    # creates a new element
    fldChar = OxmlElement('w:fldChar')
    # sets attribute on element
    fldChar.set(qn('w:fldCharType'), 'begin')
    instrText = OxmlElement('w:instrText')
    # sets attribute on element
    instrText.set(qn('xml:space'), 'preserve')
    # change 1-3 depending on heading levels you need
    instrText.text = 'TOC \\h \\z \\t "Figure" \\c'

    fldChar2 = OxmlElement('w:fldChar')
    fldChar2.set(qn('w:fldCharType'), 'separate')
    fldChar3 = OxmlElement('w:t')
    fldChar3.text = "Right-click to update List of Figures."
    fldChar2.append(fldChar3)

    fldChar4 = OxmlElement('w:fldChar')
    fldChar4.set(qn('w:fldCharType'), 'end')

    r_element = run._r
    r_element.append(fldChar)
    r_element.append(instrText)
    r_element.append(fldChar2)
    r_element.append(fldChar4)
    p_element = paragraph._p
示例#7
0
def create_toc(document):
    """Helper method to set up the Table of Contents page."""
    add_custom_heading(document, 'A.2 Table of Contents', level=2)
    paragraph = document.add_paragraph()
    run = paragraph.add_run()
    # creates a new element
    fldChar = OxmlElement('w:fldChar')
    # sets attribute on element
    fldChar.set(qn('w:fldCharType'), 'begin')
    instrText = OxmlElement('w:instrText')
    # sets attribute on element
    instrText.set(qn('xml:space'), 'preserve')
    # change 1-3 depending on heading levels you need
    instrText.text = 'TOC \\o "1-3" \\h \\z \\u'

    fldChar2 = OxmlElement('w:fldChar')
    fldChar2.set(qn('w:fldCharType'), 'separate')
    fldChar3 = OxmlElement('w:t')
    fldChar3.text = "Right-click to update Table of Contents."
    fldChar2.append(fldChar3)

    fldChar4 = OxmlElement('w:fldChar')
    fldChar4.set(qn('w:fldCharType'), 'end')

    r_element = run._r
    r_element.append(fldChar)
    r_element.append(instrText)
    r_element.append(fldChar2)
    r_element.append(fldChar4)
    p_element = paragraph._p
示例#8
0
def add_toc_section(doc):
    section = doc.sections[-1]
    add_pagenum(section.header, "Table of Contents")
    #sectPr = section._sectPr
    #cols = sectPr.xpath('./w:cols')[0]
    # cols.set(qn('w:num'),'2')

    paragraph = doc.paragraphs[-1]
    paragraph.text = "Table of Contents"
    paragraph.style = "Heading TOC"
    run = paragraph.add_run()
    fldChar = OxmlElement('w:fldChar')  # creates a new element
    fldChar.set(qn('w:fldCharType'), 'begin')  # sets attribute on element
    instrText = OxmlElement('w:instrText')
    instrText.set(qn('xml:space'), 'preserve')  # sets attribute on element
    # change 1-3 depending on heading levels you need
    instrText.text = 'TOC \\o "1-3" \\h \\z \\u'
    fldChar2 = OxmlElement('w:fldChar')
    fldChar2.set(qn('w:fldCharType'), 'separate')
    fldChar3 = OxmlElement('w:t')
    fldChar3.text = ""
    fldChar2.append(fldChar3)

    fldChar4 = OxmlElement('w:fldChar')
    fldChar4.set(qn('w:fldCharType'), 'end')

    r_element = run._r
    r_element.append(fldChar)
    r_element.append(instrText)
    r_element.append(fldChar2)
    r_element.append(fldChar4)
示例#9
0
def add_pagenum(doc, section):
    doc.is_linked_to_previous = False
    paragraph = doc.paragraphs[-1]
    paragraph.text = f"{section}\t"
    paragraph.style = "header top"
    paragraph.paragraph_format.tab_stops.add_tab_stop(Inches(4.8), 2)
    run = paragraph.add_run()
    fldChar = OxmlElement('w:fldChar')
    fldChar.set(qn('w:fldCharType'), 'begin')
    instrText = OxmlElement('w:instrText')
    instrText.set(qn('xml:space'), 'preserve')
    instrText.text = 'PAGE \\*Arabic \\* MERGEFORMAT'
    fldChar2 = OxmlElement('w:fldChar')
    fldChar2.set(qn('w:fldCharType'), 'separate')
    fldChar3 = OxmlElement('w:t')
    fldChar3.text = ""
    fldChar2.append(fldChar3)

    fldChar4 = OxmlElement('w:fldChar')
    fldChar4.set(qn('w:fldCharType'), 'end')

    r_element = run._r
    r_element.append(fldChar)
    r_element.append(instrText)
    r_element.append(fldChar2)
    r_element.append(fldChar4)
示例#10
0
def add_index_section(doc):
    section = doc.add_section(4)
    sectPr = section._sectPr
    cols = sectPr.xpath('./w:cols')[0]
    cols.set(qn('w:num'), '2')

    add_pagenum(section.header, "Index")

    doc.add_paragraph("Index", 'Heading 1')
    section = doc.add_section(0)
    sectPr = section._sectPr
    cols = sectPr.xpath('./w:cols')[0]
    cols.set(qn('w:num'), '2')

    paragraph = doc.add_paragraph()
    run = paragraph.add_run()
    fldChar = OxmlElement('w:fldChar')  # creates a new element
    fldChar.set(qn('w:fldCharType'), 'begin')  # sets attribute on element
    instrText = OxmlElement('w:instrText')
    instrText.set(qn('xml:space'), 'preserve')  # sets attribute on element
    instrText.text = 'INDEX \\h "A" \\c "3" \\z "1033"'
    fldChar2 = OxmlElement('w:fldChar')
    fldChar2.set(qn('w:fldCharType'), 'separate')
    fldChar3 = OxmlElement('w:t')
    fldChar3.text = ""
    fldChar2.append(fldChar3)

    fldChar4 = OxmlElement('w:fldChar')
    fldChar4.set(qn('w:fldCharType'), 'end')

    r_element = run._r
    r_element.append(fldChar)
    r_element.append(instrText)
    r_element.append(fldChar2)
    r_element.append(fldChar4)
示例#11
0
    def add_in_word_report(self):
        #self.logger(level="debug", message="Adding " + self.__class__.__name__ + " in word")
        # https://stackoverflow.com/questions/51360649/how-to-update-table-of-contents-in-docx-file-with-python-on-linux
        paragraph = self.report.document.add_paragraph(self.title + ' ')
        paragraph.runs[0].font.size = Pt(18)
        paragraph.runs[
            0].font.color.theme_color = MSO_THEME_COLOR_INDEX.ACCENT_1
        run = paragraph.add_run()
        fldChar = OxmlElement('w:fldChar')  # creates a new element
        fldChar.set(ns.qn('w:fldCharType'),
                    'begin')  # sets attribute on element
        instrText = OxmlElement('w:instrText')
        instrText.set(ns.qn('xml:space'),
                      'preserve')  # sets attribute on element
        instrText.text = 'TOC \\o "1-3" \\h \\z \\u'  # change 1-3 depending on heading levels you need

        fldChar2 = OxmlElement('w:fldChar')
        fldChar2.set(ns.qn('w:fldCharType'), 'separate')
        fldChar3 = OxmlElement('w:t')
        fldChar3.text = "Right-click to update field."
        fldChar2.append(fldChar3)

        fldChar4 = OxmlElement('w:fldChar')
        fldChar4.set(ns.qn('w:fldCharType'), 'end')

        r_element = run._r
        r_element.append(fldChar)
        r_element.append(instrText)
        r_element.append(fldChar2)
        r_element.append(fldChar4)
        p_element = paragraph._p
示例#12
0
def append_page_number_with_pages(paragraph, separator=' of '):
    run = paragraph.add_run()

    # page number
    # creates a new element
    fldCharBegin1 = OxmlElement('w:fldChar')
    # sets attribute on element
    fldCharBegin1.set(qn('w:fldCharType'), 'begin')
    instrText1 = OxmlElement('w:instrText')
    # sets attribute on element
    instrText1.set(qn('xml:space'), 'preserve')
    # page number
    instrText1.text = 'PAGE \* MERGEFORMAT'

    fldCharSeparate1 = OxmlElement('w:fldChar')
    fldCharSeparate1.set(qn('w:fldCharType'), 'separate')

    fldCharEnd1 = OxmlElement('w:fldChar')
    fldCharEnd1.set(qn('w:fldCharType'), 'end')

    fldCharOf = OxmlElement('w:t')
    fldCharOf.set(qn('xml:space'), 'preserve')
    fldCharOf.text = separator

    # number of pages
    # creates a new element
    fldCharBegin2 = OxmlElement('w:fldChar')
    # sets attribute on element
    fldCharBegin2.set(qn('w:fldCharType'), 'begin')
    instrText2 = OxmlElement('w:instrText')
    # sets attribute on element
    instrText2.set(qn('xml:space'), 'preserve')
    # page number
    instrText2.text = 'NUMPAGES \* MERGEFORMAT'

    fldCharSeparate2 = OxmlElement('w:fldChar')
    fldCharSeparate2.set(qn('w:fldCharType'), 'separate')

    fldCharEnd2 = OxmlElement('w:fldChar')
    fldCharEnd2.set(qn('w:fldCharType'), 'end')

    r_element = run._r
    r_element.append(fldCharBegin1)
    r_element.append(instrText1)
    r_element.append(fldCharSeparate1)
    r_element.append(fldCharEnd1)
    r_element.append(fldCharOf)
    r_element.append(fldCharBegin2)
    r_element.append(instrText2)
    r_element.append(fldCharSeparate2)
    r_element.append(fldCharEnd2)
    p_element = paragraph._p
def add_page_number(parag, position=''):
    """ Add page numbers to the footer of the document"""
    run = parag.add_run()
    fldChar1 = OxmlElement('w:fldChar')
    create_attribute(fldChar1, 'w:fldCharType', 'begin')

    instrText = OxmlElement('w:instrText')
    create_attribute(instrText, 'xml:space', 'preserve')
    instrText.text = "PAGE"

    fldChar2 = OxmlElement('w:fldChar')
    create_attribute(fldChar2, 'w:fldCharType', 'end')

    # Set the position for the numbers
    if position != '':
        jc = OxmlElement('w:jc')
        create_attribute(jc, 'w:val', position)
        p = parag._p
        pPr = p.get_or_add_pPr()
        pPr.append(jc)

    # Append the new create elements to the r tag
    run._r.append(fldChar1)
    run._r.append(instrText)
    run._r.append(fldChar2)
示例#14
0
def append_page_number_only(paragraph):
    run = paragraph.add_run()

    # page number
    # creates a new element
    fldCharBegin1 = OxmlElement('w:fldChar')
    # sets attribute on element
    fldCharBegin1.set(qn('w:fldCharType'), 'begin')
    instrText1 = OxmlElement('w:instrText')
    # sets attribute on element
    instrText1.set(qn('xml:space'), 'preserve')
    # page number
    instrText1.text = 'PAGE \* MERGEFORMAT'

    fldCharSeparate1 = OxmlElement('w:fldChar')
    fldCharSeparate1.set(qn('w:fldCharType'), 'separate')

    fldCharEnd1 = OxmlElement('w:fldChar')
    fldCharEnd1.set(qn('w:fldCharType'), 'end')

    r_element = run._r
    r_element.append(fldCharBegin1)
    r_element.append(instrText1)
    r_element.append(fldCharSeparate1)
    r_element.append(fldCharEnd1)
    p_element = paragraph._p
示例#15
0
    def add_seq_field(self, field_type, contents):
        paragraph = self.current_paragraph
        run = paragraph.add_run()
        r = run._r

        fldChar = OxmlElement('w:fldChar')
        fldChar.set(qn('w:fldCharType'), 'begin')
        r.append(fldChar)

        run = paragraph.add_run()
        r = run._r

        instrText = OxmlElement('w:instrText')
        instrText.set(qn('xml:space'), 'preserve')
        logger.info(' SEQ {} \* ARABIC'.format(field_type))
        instrText.text = ' SEQ {} \* ARABIC '.format(field_type)
        r.append(instrText)

        run = paragraph.add_run()
        r = run._r

        fldChar = OxmlElement('w:fldChar')
        fldChar.set(qn('w:fldCharType'), 'separate')
        r.append(fldChar)

        paragraph.add_run(str(contents))

        run = paragraph.add_run()
        r = run._r

        fldChar = OxmlElement('w:fldChar')
        fldChar.set(qn('w:fldCharType'), 'end')
        r.append(fldChar)
示例#16
0
 def generate_block_equation(self, obj):
     self.pad_if_needed('blockEquationsArePadded')
     mainTextElem = OxmlElement('m:t')
     if 'text' in obj:
         text = obj['text']
     else:
         text = self.aggregate_text(obj['children'])
     if text == '':
         mainTextElem.text = self.settings.get('equationPlaceholder',
                                               'ВВЕДИТЕ УРАВНЕНИЕ')
     else:
         mathml = latex2mathml.converter.convert(text)
         tree = etree.fromstring(mathml)
         new_tree = self.math_transform(tree)
         paragraph = self.document.add_paragraph()
         paragraph._p.append(new_tree.getroot())
         return
     runElem = OxmlElement('m:r')
     runElem.append(mainTextElem)
     mathElem = OxmlElement('m:oMath')
     mathElem.append(runElem)
     mathParaElem = OxmlElement('m:oMathPara')
     mathParaElem.append(mathElem)
     paragraph = self.document.add_paragraph()
     paragraph._p.append(mathParaElem)
     self.last_line_empty = False
     self.pad_if_needed('blockEquationsArePadded')
示例#17
0
def add_hyperlink(paragraph, link_to, text, is_external):
    ''' Adds a hyperlink within a paragraph to an internal bookmark
    or an external url '''

    part = paragraph.part

    hyperlink = OxmlElement('w:hyperlink')
    if is_external:
        r_id = part.relate_to(link_to, RT.HYPERLINK, is_external=is_external)
        hyperlink.set(
            qn('r:id'),
            r_id,
        )
    else:
        hyperlink.set(
            qn('w:anchor'),
            link_to,
        )

    new_run = OxmlElement('w:r')
    rPr = OxmlElement('w:rPr')

    new_run.append(rPr)
    new_run.text = text
    hyperlink.append(new_run)

    paragraph._p.append(hyperlink)
    def add_caption(self):
        """
        Add a caption of the form: 'Figure <figure number>: <caption text>, e.g. 'Figure 3: A medical device.'

        The caption will not appear in this form at the first time.
        It has to be updated by pressing Ctrl + A, and then F9.
        """

        # add the label of the caption
        caption_paragraph = self.report.add_paragraph(self.CAPTION_LABEL,
                                                      style=self.CAPTION_STYLE)

        # add XML elements and set their attributes so that the caption is considered as such and can be updated
        run = caption_paragraph.add_run()
        r_element = run._r

        fldChar = OxmlElement('w:fldChar')
        fldChar.set(qn('w:fldCharType'), 'begin')
        r_element.append(fldChar)

        instrText = OxmlElement('w:instrText')
        instrText.text = 'SEQ Figure \\* ARABIC'
        r_element.append(instrText)

        fldChar = OxmlElement('w:fldChar')
        fldChar.set(qn('w:fldCharType'), 'end')
        r_element.append(fldChar)

        # add the text of the caption
        caption_paragraph.add_run(': {}'.format(self.caption))

        # set space after the paragraph of the caption
        caption_paragraph.paragraph_format.space_after = self.space_after
示例#19
0
def add_list_of_table(run):
    fldChar = OE('w:fldChar')
    fldChar.set(qn('w:fldCharType'), 'begin')
    fldChar.set(qn('w:dirty'), 'true')
    instrText = OE('w:instrText')
    instrText.set(qn('xml:space'), 'preserve')
    instrText.text = 'TOC \\h \\z \\c "Table"'  #"Table" of list of table and "Figure" for list of figure
    fldChar2 = OE('w:fldChar')
    fldChar2.set(qn('w:fldCharType'), 'separate')
    fldChar3 = OE('w:t')
    fldChar3.text = "Right-click to update field."
    fldChar2.append(fldChar3)
    
    fldChar4 = OE('w:fldChar')
    fldChar4.set(qn('w:fldCharType'), 'end')
    
    run._r.append(fldChar)
    run._r.append(instrText)
    run._r.append(fldChar2)
    run._r.append(fldChar4)
    
示例#20
0
def Table(paragraph):
    run = run = paragraph.add_run()
    r = run._r
    fldChar = OxmlElement('w:fldChar')
    fldChar.set(qn('w:fldCharType'), 'begin')
    r.append(fldChar)
    instrText = OxmlElement('w:instrText')
    instrText.text = ' SEQ Table \* ARABIC'
    r.append(instrText)
    fldChar = OxmlElement('w:fldChar')
    fldChar.set(qn('w:fldCharType'), 'end')
    r.append(fldChar)
示例#21
0
文件: tools.py 项目: YoannDqr/PyDocx
def add_xref(paragraph, type):
    run = paragraph.add_run()
    r = run._r
    fldChar = OxmlElement('w:fldChar')
    fldChar.set(ns.qn('w:fldCharType'), 'begin')
    r.append(fldChar)
    instrText = OxmlElement('w:instrText')
    instrText.text = ' SEQ ' + type + ' \* ARABIC'
    r.append(instrText)
    fldChar = OxmlElement('w:fldChar')
    fldChar.set(ns.qn('w:fldCharType'), 'end')
    r.append(fldChar)
    def add_figures_list(report_document):
        """
        Add a list of figures.

        The list of figures will not appear at the first time.
        It has to be updated by pressing Ctrl + A, and then F9.

        Args:
            report_document: .docx file where the report is written.
        """

        # add the heading of the list of figures
        report_document.add_paragraph('List of figures', 'Heading 2')

        # access to XML run element <w:r>
        paragraph = report_document.add_paragraph()
        run = paragraph.add_run()
        r = run._r

        # create new XML elements, set their attributes and add them to the run element
        # so that the list of figure is considered as such and can be updated
        fldChar = OxmlElement('w:fldChar')
        fldChar.set(qn('w:fldCharType'), 'begin')
        r.append(fldChar)

        instrText = OxmlElement('w:instrText')
        instrText.set(qn('xml:space'), 'preserve')
        instrText.text = 'TOC \\h \\z \\c \"Figure\"'
        r.append(instrText)

        fldChar2 = OxmlElement('w:fldChar')
        fldChar2.set(qn('w:fldCharType'), 'separate')
        fldChar3 = OxmlElement('w:t')
        fldChar3.text = 'Press "Ctrl + A" to select everything and then "F9" to update fields.'
        fldChar2.append(fldChar3)
        r.append(fldChar2)

        fldChar4 = OxmlElement('w:fldChar')
        fldChar4.set(qn('w:fldCharType'), 'end')
        r.append(fldChar4)
    def write(report_document: Document):
        """
        Add a table of content.

        The table of content will not appear at the first time.
        It has to be updated by pressing Ctrl + A, and then F9.

        Args:
            report_document: .docx file where the report is written.
        """

        # add the heading of the table of content
        report_document.add_paragraph('Table of content', 'Table of content')

        # access to XML run element <w:r>
        paragraph = report_document.add_paragraph()
        run = paragraph.add_run()
        r = run._r

        # create new XML elements, set their attributes and add them to the run element
        # so that the table of content is considered as such and can be updated
        fldChar = OxmlElement('w:fldChar')
        fldChar.set(qn('w:fldCharType'), 'begin')
        r.append(fldChar)

        instrText = OxmlElement('w:instrText')
        instrText.set(qn('xml:space'), 'preserve')
        instrText.text = 'TOC \\o "1-2" \\h \\z \\u'  # "1-2" correspond to heading levels
        r.append(instrText)

        fldChar2 = OxmlElement('w:fldChar')
        fldChar2.set(qn('w:fldCharType'), 'separate')
        fldChar3 = OxmlElement('w:t')
        fldChar3.text = 'Press "Ctrl + A" to select everything and then "F9" to update fields.'
        fldChar2.append(fldChar3)
        r.append(fldChar2)

        fldChar4 = OxmlElement('w:fldChar')
        fldChar4.set(qn('w:fldCharType'), 'end')
        r.append(fldChar4)
示例#24
0
    def __init__(self, doc=None):
        try:
            if doc is None:
                raise Exception("Doc is None")

            doc.add_page_break()
            doc.add_heading('Inhoudsopgave', 1)

            paragraph = doc.add_paragraph()
            run = paragraph.add_run()
            fldChar = OxmlElement('w:fldChar')  # creates a new element
            fldChar.set(qn('w:fldCharType'),
                        'begin')  # sets attribute on element
            instrText = OxmlElement('w:instrText')
            instrText.set(qn('xml:space'),
                          'preserve')  # sets attribute on element
            instrText.text = 'TOC \\o "1-3" \\h \\z \\u'  # change 1-3 depending on heading levels you need

            fldChar2 = OxmlElement('w:fldChar')
            fldChar2.set(qn('w:fldCharType'), 'separate')
            fldChar3 = OxmlElement('w:t')
            fldChar3.text = "Right-click to update field."
            fldChar2.append(fldChar3)

            fldChar4 = OxmlElement('w:fldChar')
            fldChar4.set(qn('w:fldCharType'), 'end')

            r_element = run._r
            r_element.append(fldChar)
            r_element.append(instrText)
            r_element.append(fldChar2)
            r_element.append(fldChar4)
            p_element = paragraph._p
        except Exception as e:
            exc_type, exc_obj, exc_tb = sys.exc_info()
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            logging.warning(
                str(e) + " | " + str(exc_type) + " | " + str(fname) + " | " +
                str(exc_tb.tb_lineno))
            return None
示例#25
0
def Figure(paragraph):
    paragraph.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    run = run = paragraph.add_run()
    r = run._r
    fldChar = OxmlElement('w:fldChar')
    fldChar.set(qn('w:fldCharType'), 'begin')
    r.append(fldChar)
    instrText = OxmlElement('w:instrText')
    instrText.text = ' SEQ Figure \* ARABIC'
    r.append(instrText)
    fldChar = OxmlElement('w:fldChar')
    fldChar.set(qn('w:fldCharType'), 'end')
    r.append(fldChar)
示例#26
0
 def Figure(self, paragraph, text):
     run = paragraph.add_run()
     r = run._r
     fldChar = OxmlElement('w:fldChar')
     fldChar.set(qn('w:fldCharType'), 'begin')
     r.append(fldChar)
     instrText = OxmlElement('w:instrText')
     instrText.text = ' SEQ Figure \\* ARABIC'
     r.append(instrText)
     fldChar = OxmlElement('w:fldChar')
     fldChar.set(qn('w:fldCharType'), 'end')
     r.append(fldChar)
     run = paragraph.add_run(text)
示例#27
0
def add_toc(paragraph):
    run = paragraph.add_run()
    fld_char = OxmlElement('w:fldChar')
    fld_char.set(qn('w:fldCharType'), 'begin')
    insert_text = OxmlElement('w:instrText')
    insert_text.set(qn('xml:space'), 'preserve')
    insert_text.text = 'TOC \\o "1-2" \\h \\z \\u'

    fld_char_2 = OxmlElement('w:fldChar')
    fld_char_2.set(qn('w:fldCharType'), 'separate')
    fld_char_3 = OxmlElement('w:t')
    fld_char_3.text = "Right-click to update field."
    fld_char_2.append(fld_char_3)

    fld_char4 = OxmlElement('w:fldChar')
    fld_char4.set(qn('w:fldCharType'), 'end')

    r_element = run._r
    r_element.append(fld_char)
    r_element.append(insert_text)
    r_element.append(fld_char_2)
    r_element.append(fld_char4)
示例#28
0
def add_table_of_contents(paragraph: Paragraph) -> None:
    """Add a table of contents to the paragraph."""
    run = paragraph.add_run()
    fld_char = OxmlElement("w:fldChar")  # creates a new element
    fld_char.set(qn("w:fldCharType"), "begin")  # sets attribute on element
    instr_text = OxmlElement("w:instrText")
    instr_text.set(qn("xml:space"), "preserve")  # sets attribute on element
    instr_text.text = 'TOC \\o "1-3" \\h \\z \\u'  # change 1-3 depending on heading levels you need

    fld_char2 = OxmlElement("w:fldChar")
    fld_char2.set(qn("w:fldCharType"), "separate")
    fld_char3 = OxmlElement("w:t")
    fld_char3.text = "Right-click to update field."
    fld_char2.append(fld_char3)

    fld_char4 = OxmlElement("w:fldChar")
    fld_char4.set(qn("w:fldCharType"), "end")

    r_element = run._r  # pylint: disable=protected-access
    r_element.append(fld_char)
    r_element.append(instr_text)
    r_element.append(fld_char2)
    r_element.append(fld_char4)
示例#29
0
def add_hyperlink(paragraph, url, text):
    """Create a hyperlink within a paragraph object.

    Reference:

        https://github.com/python-openxml/python-docx/issues/74#issuecomment-215678765

    Args:
        paragraph (Paragraph): ``python-docx`` paragraph adding the hyperlink to.
        url (str): The required url.
        text (str): The text displayed for the url.

    Returns: 
        Run: A Run object containing the hyperlink.
    """

    # This gets access to the document.xml.rels file and gets a new relation id value
    part = paragraph.part
    r_id = part.relate_to(url, RELATIONSHIP_TYPE.HYPERLINK, is_external=True)

    # Create the w:hyperlink tag and add needed values
    hyperlink = OxmlElement('w:hyperlink')
    hyperlink.set(
        qn('r:id'),
        r_id,
    )
    hyperlink.set(qn('w:history'), '1')

    # Create a w:r element
    new_run = OxmlElement('w:r')

    # Create a new w:rPr element
    rPr = OxmlElement('w:rPr')

    # Create a w:rStyle element, note this currently does not add the hyperlink style as its not in
    # the default template, I have left it here in case someone uses one that has the style in it
    rStyle = OxmlElement('w:rStyle')
    rStyle.set(qn('w:val'), 'Hyperlink')

    # Join all the xml elements together add add the required text to the w:r element
    rPr.append(rStyle)
    new_run.append(rPr)
    new_run.text = text
    hyperlink.append(new_run)

    # Create a new Run object and add the hyperlink into it
    r = paragraph.add_run()
    r._r.append(hyperlink)

    return r
示例#30
0
def header(doc, title, tags):
    """
    Adds a heading to a .doc file
    """
    print(f" + Ess {title}")
    paragraph = doc.sections[0].header.add_paragraph(f"\t{tags['Lastname']} ")
    paragraph.paragraph_format.tab_stops.add_tab_stop(Inches(6.5), 2)
    run = paragraph.add_run()
    fldChar = OxmlElement('w:fldChar')  # creates a new element
    fldChar.set(qn('w:fldCharType'), 'begin')  # sets attribute on element
    instrText = OxmlElement('w:instrText')
    instrText.set(qn('xml:space'), 'preserve')  # sets attribute on element
    instrText.text = 'PAGE \\*Arabic \\* MERGEFORMAT'  # change 1-3 depending on heading levels you need
    fldChar2 = OxmlElement('w:fldChar')
    fldChar2.set(qn('w:fldCharType'), 'separate')
    fldChar3 = OxmlElement('w:t')
    fldChar3.text = "Right-click to update field."
    fldChar2.append(fldChar3)

    fldChar4 = OxmlElement('w:fldChar')
    fldChar4.set(qn('w:fldCharType'), 'end')

    r_element = run._r
    r_element.append(fldChar)
    r_element.append(instrText)
    r_element.append(fldChar2)
    r_element.append(fldChar4)

    doc.add_paragraph(tags['Author'], "head")

    doc.add_paragraph(tags['Prof'], "head")

    doc.add_paragraph(tags['Class'], "head")

    doc.add_paragraph(tags['Date'], "head")

    doc.add_paragraph(title, "til")