예제 #1
0
    def insert_horizontal_border(paragraph: Paragraph):
        """
        Add an horizontal border under a paragraph.

        Args:
            paragraph: Paragraph under which you want to add an horizontal border.
        """

        # access to XML paragraph element <w:p> and its properties
        p = paragraph._p
        pPr = p.get_or_add_pPr()

        # create new XML element and insert it to the paragraph element
        pBdr = OxmlElement('w:pBdr')
        pPr.insert_element_before(
            pBdr, 'w:shd', 'w:tabs', 'w:suppressAutoHyphens', 'w:kinsoku',
            'w:wordWrap', 'w:overflowPunct', 'w:topLinePunct', 'w:autoSpaceDE',
            'w:autoSpaceDN', 'w:bidi', 'w:adjustRightInd', 'w:snapToGrid',
            'w:spacing', 'w:ind', 'w:contextualSpacing', 'w:mirrorIndents',
            'w:suppressOverlap', 'w:jc', 'w:textDirection', 'w:textAlignment',
            'w:textboxTightWrap', 'w:outlineLvl', 'w:divId', 'w:cnfStyle',
            'w:rPr', 'w:sectPr', 'w:pPrChange')

        # create new XML element, set its properties and add it to the pBdr element
        bottom = OxmlElement('w:bottom')
        bottom.set(qn('w:val'), 'single')
        bottom.set(qn('w:sz'), '6')
        bottom.set(qn('w:space'), '1')
        bottom.set(qn('w:color'), 'auto')
        pBdr.append(bottom)
예제 #2
0
def add_url_hyperlink(paragraph, url: str, text: str):
    """Add URL hyperlink in docx report. Adapted from https://stackoverflow.com/a/47666747/906385

    Args:
        paragraph: a docx paragraph object
        url (str): The URL
        text (str): The text to display
    """
    # 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,
                          constants.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)

    # Create a w:r element and a new w:rPr element
    new_run = OxmlElement("w:r")
    rPr = OxmlElement("w:rPr")

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

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

    # A workaround for the lack of a hyperlink style (doesn't go purple after using the link)
    # Delete this if using a template that has the hyperlink style in it
    run.font.color.theme_color = dml.MSO_THEME_COLOR_INDEX.HYPERLINK
    run.font.underline = True
예제 #3
0
def add_table_of_contents(document, label='Table of Contents'):
    document.add_heading(label, level=1)
    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 = r'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 document
예제 #4
0
    def remove_border(self, row, border=0):
        for cell in row:
            tcPr = cell.tcPr
            tcBorders = OxmlElement('w:tcBorders')
            top = OxmlElement('w:top')
            top.set(qn('w:val'), 'nil')

            left = OxmlElement('w:left')
            left.set(qn('w:val'), 'nil')

            bottom = OxmlElement('w:bottom')
            bottom.set(qn('w:val'), 'nil')
            bottom.set(qn('w:sz'), '4')
            bottom.set(qn('w:space'), '0')
            bottom.set(qn('w:color'), 'auto')

            right = OxmlElement('w:right')
            right.set(qn('w:val'), 'nil')

            if border == 1:
                tcBorders.append(top)
            if border == 2:
                tcBorders.append(bottom)
            if border == 0:
                tcBorders.append(top)
                tcBorders.append(bottom)
            tcPr.append(tcBorders)
예제 #5
0
def generate_pw_toc(document):
    global gl_font_size
    add_simple_par(document, "", gl_font_size, align=docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER, bold=False, breakpar=True)
    document.add_heading("Spis treści", 1)

    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-2" \\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 = "Kliknij prawym klawiszem myszki, aby zaktualizować spis treści."
    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
예제 #6
0
def add_hyperlink(paragraph, url, text, color='0000FF', underline=True):
    """ places a hyperlink within a paragraph object
    """
    part = paragraph.part
    r_id = part.relate_to(url, RELATIONSHIP_TYPE.HYPERLINK, is_external=True)

    hyperlink = OxmlElement('w:hyperlink')
    hyperlink.set(
        qn('r:id'),
        r_id,
    )

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

    if color is not None:
        c = OxmlElement('w:color')
        c.set(qn('w:val'), color)
        rPr.append(c)

    if not underline:
        u = OxmlElement('w:u')
        u.set(qn('w:val'), 'none')
        rPr.append(u)

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

    paragraph._p.append(hyperlink)

    return hyperlink
예제 #7
0
    def set_table_margins(self, tbl, width: dict = None):
        """
        <w:tbl>
            <w:tblPr>
                    <w:tblStyle w:val="LightShading"/>
                    <w:tblW w:type="auto" w:w="0"/>
                    <w:tblCellMar>
                            <w:left w:type="dxa" w:w="63"/>
                            <w:right w:type="dxa" w:w="63"/>
                    </w:tblCellMar>
                    <w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0" w:lastRow="0" w:noHBand="0" w:noVBand="1" w:val="04A0"/>
            </w:tblPr>
        """  # noqa

        # 67 = Cm(0.11) ...?
        if width is None:
            width = dict(left=67, right=67)

        margins = OxmlElement('w:tblCellMar')

        for side, w in width.items():
            margin = OxmlElement(f'w:{side}')
            margin.set(qn('w:w'), str(w))
            margin.set(qn('w:type'), 'dxa')

            margins.append(margin)

        tbl._tbl.tblPr.append(margins)
    def insert_toc(self):
        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 = OxmlElement('w:updateFields')
        fldChar3.set(qn('w:val'), 'true')
        # 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
    def add_page_number(run: Run):
        """
        Add the page number to a paragraph.

        Args:
            run: Run in which the page number will be added.
        """

        # access to XML run element <w:r>
        r = run._r

        # create new XML elements, set their attributes and add them to the run element
        # so that the page number correspond to a real page number
        fldChar1 = OxmlElement('w:fldChar')
        fldChar1.set(qn('w:fldCharType'), 'begin')
        r.append(fldChar1)

        instrText = OxmlElement('w:instrText')
        instrText.set(qn('xml:space'), 'preserve')
        instrText.text = 'PAGE'
        r.append(instrText)

        fldChar2 = OxmlElement('w:fldChar')
        fldChar2.set(qn('w:fldCharType'), 'end')
        r.append(fldChar2)
예제 #10
0
def add_hyperlink(paragraph,
                  url,
                  text,
                  font_name=None,
                  underline_hyperlink=True,
                  indent=0,
                  color=True):
    """
    A function that places a hyperlink within a paragraph object.

    :param paragraph: The paragraph we are adding the hyperlink to.
    :param url: A string containing the required url
    :param text: The text displayed for the url
    :return: 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, RT.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.font.name = font_name
    r._r.append(hyperlink)

    # A workaround for the lack of a hyperlink style (doesn't go purple after using the link)
    # Delete this if using a template that has the hyperlink style in it
    if color:
        r.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERLINK
    r.font.underline = underline_hyperlink

    return r
예제 #11
0
    def add_tbl_border(self, tbl):
        """Add table bottom border with OxmlElement"""
        borders = OxmlElement('w:tblBorders')
        bottom_border = OxmlElement('w:bottom')
        bottom_border.set(qn('w:val'), 'single')
        bottom_border.set(qn('w:sz'), '4')
        borders.append(bottom_border)

        tbl._tbl.tblPr.append(borders)
예제 #12
0
 def add_fig_caption(self, paragraph):
     """Add figure caption to image with auto updating numbers
     - User must select all (cmd/ctrl + A), then F9 to update fig captions"""
     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'  # noqa
     r.append(instrText)
     fldChar = OxmlElement('w:fldChar')
     fldChar.set(qn('w:fldCharType'), 'end')
     r.append(fldChar)
예제 #13
0
def add_hyperlink(paragraph, text, url):
	part = paragraph.part
	r_id = part.relate_to(url, RELATIONSHIP_TYPE.HYPERLINK, is_external=True)
	hyperlink = OxmlElement("w:hyperlink")
	hyperlink.set(qn("r:id"), r_id, )
	new_run = OxmlElement("w:r")
	rPr = OxmlElement("w:rPr")
	new_run.append(rPr)
	new_run.text = text
	hyperlink.append(new_run)
	r = paragraph.add_run()
	r._r.append(hyperlink)
	r.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERLINK
	r.font.underline = True
	return hyperlink
예제 #14
0
def add_hyperlink(paragraph, url, text):
    """
    A function that places a hyperlink within a paragraph object.

    Parameters
    ----------
    paragraph: The paragraph we are adding the hyperlink to.
    url: A string containing the required url
    text: The text displayed for the url

    Returns
    -------
    hyperlink: hyperlink object

    """

    # 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,
                          docx.opc.constants.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)

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

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

    # Style it
    c = OxmlElement("w:color")
    c.set(qn("w:val"), "4F81BD")
    rPr.append(c)
    u = OxmlElement("w:u")
    u.set(qn("w:val"), "none")
    rPr.append(u)

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

    paragraph._p.append(hyperlink)

    return hyperlink
예제 #15
0
 def add_smallCaps(self):
     """
     Return a newly added <w:smallCaps/> child element.
     """
     smallCaps = OxmlElement('w:smallCaps')
     self.insert(0, smallCaps)
     return smallCaps
예제 #16
0
 def add_shadow(self):
     """
     Return a newly added <w:shadow/> child element.
     """
     shadow = OxmlElement('w:shadow')
     self.insert(0, shadow)
     return shadow
예제 #17
0
 def add_rtl(self):
     """
     Return a newly added <w:rtl/> child element.
     """
     rtl = OxmlElement('w:rtl')
     self.insert(0, rtl)
     return rtl
예제 #18
0
 def add_outline(self):
     """
     Return a newly added <w:outline/> child element.
     """
     outline = OxmlElement('w:outline')
     self.insert(0, outline)
     return outline
예제 #19
0
 def add_oMath(self):
     """
     Return a newly added <w:oMath/> child element.
     """
     oMath = OxmlElement('w:oMath')
     self.insert(0, oMath)
     return oMath
예제 #20
0
 def add_noProof(self):
     """
     Return a newly added <w:noProof/> child element.
     """
     noProof = OxmlElement('w:noProof')
     self.insert(0, noProof)
     return noProof
예제 #21
0
 def add_imprint(self):
     """
     Return a newly added <w:imprint/> child element.
     """
     imprint = OxmlElement('w:imprint')
     self.insert(0, imprint)
     return imprint
예제 #22
0
 def add_snapToGrid(self):
     """
     Return a newly added <w:snapToGrid/> child element.
     """
     snapToGrid = OxmlElement('w:snapToGrid')
     self.insert(0, snapToGrid)
     return snapToGrid
예제 #23
0
 def add_webHidden(self):
     """
     Return a newly added <w:webHidden/> child element.
     """
     webHidden = OxmlElement('w:webHidden')
     self.insert(0, webHidden)
     return webHidden
예제 #24
0
 def add_strike(self):
     """
     Return a newly added <w:strike/> child element.
     """
     strike = OxmlElement('w:strike')
     self.insert(0, strike)
     return strike
예제 #25
0
 def add_specVanish(self):
     """
     Return a newly added <w:specVanish/> child element.
     """
     specVanish = OxmlElement('w:specVanish')
     self.insert(0, specVanish)
     return specVanish
예제 #26
0
 def add_iCs(self):
     """
     Return a newly added <w:iCs/> child element.
     """
     iCs = OxmlElement('w:iCs')
     self.insert(0, iCs)
     return iCs
예제 #27
0
 def add_vanish(self):
     """
     Return a newly added <w:vanish/> child element.
     """
     vanish = OxmlElement('w:vanish')
     self.insert(0, vanish)
     return vanish
예제 #28
0
 def add_emboss(self):
     """
     Return a newly added <w:emboss/> child element.
     """
     emboss = OxmlElement('w:emboss')
     self.insert(0, emboss)
     return emboss
예제 #29
0
 def new(cls, text):
     """
     Return a new ``<w:t>`` element.
     """
     t = OxmlElement('w:t')
     t.text = text
     return t
예제 #30
0
 def add_i(self):
     """
     Return a newly added <w:i/> child element.
     """
     i = OxmlElement('w:i')
     self.insert(0, i)
     return i