Esempio n. 1
0
def _set_cell_border(cell, **kwargs):
    """
    This function comes from
    https://www.jianshu.com/p/9ad7db7825ba
    """
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()

    tcBorders = tcPr.first_child_found_in("w:tcBorders")
    if tcBorders is None:
        tcBorders = OxmlElement('w:tcBorders')
        tcPr.append(tcBorders)

    for edge in ('left', 'top', 'right', 'bottom', 'insideH', 'insideV'):
        edge_data = kwargs.get(edge)
        if edge_data:
            tag = 'w:{}'.format(edge)

            element = tcBorders.find(qn(tag))
            if element is None:
                element = OxmlElement(tag)
                tcBorders.append(element)

            for key in ["sz", "val", "color", "space", "shadow"]:
                if key in edge_data:
                    element.set(qn('w:{}'.format(key)), str(edge_data[key]))
Esempio n. 2
0
def _borders(cell, tcPr, xls_format):
    tcBorders = OxmlElement('w:tcBorders')
    for position in ['top', 'bottom', 'left', 'right']:

        # Map xls border format to xml format
        if xls_format['border'][position] == 'thin':
            val = 'single'
            sz = '2'
        elif xls_format['border'][position] == 'medium':
            val = 'single'
            sz = '8'
        elif xls_format['border'][position] == 'thick':
            val = 'single'
            sz = '16'
        else:
            val = 'nil'
            sz = '2'

        # Set border formats on obj
        # More options at http://officeopenxml.com/WPtableBorders.php
        side = OxmlElement(f'w:{position}')
        side.set(qn('w:val'), val)
        side.set(qn('w:sz'), sz)  # sz 2 = 1/4 pt is the minimum
        side.set(qn('w:space'), '0')
        side.set(qn('w:shadow'), 'false')
        if xls_format['border'][
                f'{position}Color'] is not None:  # Catch Nonetype colors
            side.set(qn('w:color'), xls_format['border'][f'{position}Color'])

        tcBorders.append(side)
    tcPr.append(tcBorders)
Esempio n. 3
0
def set_cell_border(cell: _Cell, **kwargs) -> None:
    """
    Set cell`s border of a Table cell instance.

    Usage:
    set_cell_border(
        cell,
        top={"sz": 12, "val": "single", "color": "#FF0000", "space": "0"},
        bottom={"sz": 12, "color": "#00FF00", "val": "single"},
        start={"sz": 24, "val": "dashed", "shadow": "true"},
        end={"sz": 12, "val": "dashed"},
    )
    """
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()
    # check for tag existnace, if none found, then create one
    tcBorders = tcPr.first_child_found_in("w:tcBorders")
    if tcBorders is None:
        tcBorders = OxmlElement('w:tcBorders')
        tcPr.append(tcBorders)
    # list over all available tags
    for edge in ('start', 'top', 'end', 'bottom', 'insideH', 'insideV'):
        edge_data = kwargs.get(edge)
        if edge_data:
            tag = 'w:{}'.format(edge)
            # check for tag existnace, if none found, then create one
            element = tcBorders.find(qn(tag))
            if element is None:
                element = OxmlElement(tag)
                tcBorders.append(element)
            # looks like order of attributes is important
            for key in ["sz", "val", "color", "space", "shadow"]:
                if key in edge_data:
                    element.set(qn('w:{}'.format(key)), str(edge_data[key]))
Esempio n. 4
0
def set_cell_margins(cell: _Cell, **kwargs):
    """
    cell:  actual cell instance you want to modify

    usage:

        set_cell_margins(cell, top=50, start=50, bottom=50, end=50)

    provided values are in twentieths of a point (1/1440 of an inch).
    read more here: http://officeopenxml.com/WPtableCellMargins.php
    """
    tc = cell._tc
    # accesses the actual element
    tcPr = tc.get_or_add_tcPr()
    # print(dir(tc))
    # i think access the table paragraph element
    tcMar = OxmlElement('w:tcMar')
    # access the table cell margin element

    for m in [
        "top",
        "start",
        "bottom",
        "end",
    ]:
        if m in kwargs:
            node = OxmlElement("w:{}".format(m))
            # w:top
            node.set(qn('w:w'), str(kwargs.get(m)))
            # w:w set kwargs[0]
            node.set(qn('w:type'), 'dxa')
            # w:type set 'dxa'
            tcMar.append(node)

    tcPr.append(tcMar)
Esempio n. 5
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
Esempio n. 6
0
 def set_table_styling(table, *args, special=False):
     tbl = table._tbl
     cell_number = 0
     coll_count = len(table.columns)
     borders = ['left', 'right', 'top', 'bottom']
     for cell in tbl.iter_tcs():
         tc_pr = cell.tcPr
         tc_borders = OxmlElement("w:tcBorders")
         for border in args:
             side = OxmlElement(f'w:{border}')
             side.set(qn("w:val"), "nil")
             tc_borders.append(side)
         for i in set(borders).difference(args):
             side = OxmlElement(f'w:{i}')
             side.set(qn("w:val"), "single")
             if cell_number < coll_count:
                 side.set(qn("w:sz"), "12")
                 cell_number += 1
                 side.set(qn("w:color"), "4f2d7f")
             else:
                 if special:
                     side.set(qn("w:color"), "ffffff")
                 else:
                     side.set(qn("w:sz"), "5")
                     side.set(qn("w:color"), "b5b5b5")
             tc_borders.append(side)
         tc_pr.append(tc_borders)
Esempio n. 7
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
Esempio n. 8
0
    def add_horizontal_line(self):
        p = self.ref._p
        p_pr = p.get_or_add_pPr()
        p_bdr = OxmlElement('w:pBdr')

        _insert_element_before(
            p_pr,
            p_bdr,
            successors=('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'))

        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')
        p_bdr.append(bottom)

        return self
Esempio n. 9
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)
Esempio n. 10
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
Esempio n. 11
0
def set_cell_border(cell, **kwargs):
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()

    # check for tag existnace, if none found, then create one
    tcBorders = tcPr.first_child_found_in("w:tcBorders")
    if tcBorders is None:
        tcBorders = OxmlElement('w:tcBorders')
        tcPr.append(tcBorders)

    # list over all available tags
    for edge in ('start', 'top', 'end', 'bottom', 'insideH', 'insideV'):
        edge_data = kwargs.get(edge)
        if edge_data:
            tag = 'w:{}'.format(edge)

            # check for tag existnace, if none found, then create one
            element = tcBorders.find(qn(tag))
            if element is None:
                element = OxmlElement(tag)
                tcBorders.append(element)

            # looks like order of attributes is important
            for key in ["sz", "val", "color", "space", "shadow"]:
                if key in edge_data:
                    element.set(qn('w:{}'.format(key)), str(edge_data[key]))
Esempio n. 12
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)
Esempio n. 13
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)
Esempio n. 14
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)
Esempio n. 15
0
def set_character_scaling(p_run, scale: float = 1.0):
    ''' Set character spacing: scaling. Font | Advanced | Character Spacing | Scaling.
        ---
        Args:
          - p_run: docx.text.run.Run, proxy object wrapping <w:r> element
          - scale: scaling factor
    '''
    # get or create run properties
    properties = p_run._element.xpath('w:rPr')
    if not properties:
        property = OxmlElement('w:rPr')
        p_run._element.append(property)
    else:
        property = properties[0]

    # get or create character scaling under properties
    ws = property.xpath('w:w')
    if not ws:
        w = OxmlElement('w:w')
        property.append(w)
    else:
        w = ws[0]

    # set scaling: percentage
    w.set(qn('w:val'), str(100 * scale))
Esempio n. 16
0
    def add_oval_shape(self, fill_hex_color=None):
        """
        https://python-docx.readthedocs.io/en/latest/user/shapes.html
        https://docs.microsoft.com/en-us/windows/win32/vml/web-workshop---specs---standards----how-to-use-vml-on-web-pages
        """
        fill_hex_color = fill_hex_color or '#ffffff'
        color = fill_hex_color
        if '#' != color[0]:
            color = '#' + color

        pict = OxmlElement('w:pict')
        nsmap = dict(v='urn:schemas-microsoft-com:vml')
        oval_attrs = dict(
            id=str(uuid4()),
            style=
            'width:12pt;height:12pt;z-index:-251658240;mso-position-vertical:top;mso-position-horizontal:left',
            fillcolor=color,
        )
        oval = oxml_parser.makeelement('{%s}%s' % (nsmap['v'], 'oval'),
                                       attrib=oval_attrs,
                                       nsmap=nsmap)

        border_attrs = dict(color='gray', joinstyle='round', endcap='flat')
        stroke = oxml_parser.makeelement('{%s}%s' % (nsmap['v'], 'stroke'),
                                         attrib=border_attrs,
                                         nsmap=nsmap)
        oval.append(stroke)
        pict.append(oval)
        self.ref._element.append(pict)
Esempio n. 17
0
def add_table_cell_margins(table, **kwargs):
    """
    table:  actual table instance you want to modify

    usage:

        set_table_cell_margins(table, top=50, bottom=50, start=50, end=50)

    provided values are in twentieths of a point (1/1440 of an inch).
    """
    tblPr = table._tblPr
    tblCellMar = OxmlElement('w:tblCellMar')

    for m in [
            "top",
            "bottom",
            "start",
            "end",
    ]:
        if m in kwargs:
            node = OxmlElement("w:{}".format(m))
            # w:top
            node.set(qn('w:w'), str(kwargs.get(m)))
            # w:w set kwargs[0]
            node.set(qn('w:type'), 'dxa')
            # w:type set 'dxa'
            tblCellMar.append(node)

    tblPr.append(tblCellMar)
Esempio n. 18
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
Esempio n. 19
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
Esempio n. 20
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
Esempio n. 21
0
def _set_table_cell_properties(table_cell, color_hex, **kwargs):
    if color_hex is None:
        return

    # pylint: disable=protected-access
    table_cell = table_cell._tc
    table_cell_properties = table_cell.get_or_add_tcPr()
    cell_shading = OxmlElement("w:shd")
    cell_shading.set(qn("w:fill"), color_hex)
    table_cell_properties.append(cell_shading)

    # check for cell borders tag existence, if none found, then create one
    table_cell_borders = table_cell_properties.first_child_found_in(
        "w:tcBorders")
    if table_cell_borders is None:
        table_cell_borders = OxmlElement('w:tcBorders')
        table_cell_properties.append(table_cell_borders)

    # list over all available border tags
    for edge in kwargs:
        edge_data = kwargs.get(edge)
        tag = 'w:{}'.format(edge)

        # check for given tag existence, if none found, then create one
        element = table_cell_borders.find(qn(tag))
        if element is None:
            element = OxmlElement(tag)
            table_cell_borders.append(element)

            for key in edge_data:
                element.set(qn('w:{}'.format(key)), str(edge_data[key]))
Esempio n. 22
0
def set_cell_margins(cell: _Cell, **kwargs):
    ''' Set cell margins. Provided values are in twentieths of a point (1/1440 of an inch).
        ---
        Args:
          - cell:  actual cell instance you want to modify
          - kwargs: a dict with keys: top, bottom, start, end
        
        Usage:
          - set_cell_margins(cell, top=50, start=50, bottom=50, end=50)
        
        Read more: 
          - https://blog.csdn.net/weixin_44312186/article/details/104944773
          - http://officeopenxml.com/WPtableCellMargins.php
    '''
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()
    tcMar = OxmlElement('w:tcMar')

    for m in ['top', 'start', 'bottom', 'end']:
        if m in kwargs:
            node = OxmlElement("w:{}".format(m))
            node.set(qn('w:w'), str(kwargs.get(m)))
            node.set(qn('w:type'), 'dxa')
            tcMar.append(node)

    tcPr.append(tcMar)
Esempio n. 23
0
def set_cell_margins(cell: _Cell, **kwargs):
    """
    cell:  actual cell instance you want to modify

    usage:

        set_cell_margins(cell, top=50, start=50, bottom=50, end=50)

    provided values are in twentieths of a point (1/1440 of an inch).
    read more here: http://officeopenxml.com/WPtableCellMargins.php
    """
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()
    tcMar = OxmlElement('w:tcMar')

    for m in [
            "top",
            "start",
            "bottom",
            "end",
    ]:
        if m in kwargs:
            node = OxmlElement("w:{}".format(m))
            node.set(qn('w:w'), str(kwargs.get(m)))
            node.set(qn('w:type'), 'dxa')
            tcMar.append(node)

    tcPr.append(tcMar)
Esempio n. 24
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
Esempio n. 25
0
def create_list(paragraph, list_type):
    p = paragraph._p  #access to xml paragraph element
    pPr = p.get_or_add_pPr()  #access paragraph properties
    numPr = OxmlElement('w:numPr')  #create number properties element
    numId = OxmlElement('w:numId')  #create numId element - sets bullet type
    numId.set(qn('w:val'), list_type)  #set list type/indentation
    numPr.append(numId)  #add bullet type to number properties list
    pPr.append(numPr)  #add number properties to paragraph
Esempio n. 26
0
def create_list(paragraph, list_type):
    p = paragraph._p #access to xml paragraph element
    pPr = p.get_or_add_pPr() #access paragraph properties
    numPr = OxmlElement('w:numPr') #create number properties element
    numId = OxmlElement('w:ilvl') #create numId element - sets bullet type
    numId.set(qn('w:val'), '1') #set list type/indentation
    numPr.append(numId) #add bullet type to number properties list
    numId = OxmlElement('w:numId') #create numId element - sets bullet type
    numId.set(qn('w:val'), str(list_type+1))
    numPr.append(numId) #add bullet type to number properties list
    pPr.append(numPr) #add number properties to paragraph
def add_section(parag, fmt=''):
    """ Create new section tag element"""
    pPr = OxmlElement('w:pPr')
    section = OxmlElement('w:sectPr')

    # set the page number type
    nt = set_page_number_type(fmt)

    # Append the section to the paragraph
    section.append(nt)
    pPr.append(section)
    parag._p.append(pPr)
Esempio n. 28
0
    def TableBordersAllCells(self, size):
        for r in range(len(self.table.rows)):
            for c in range(len(self.table.rows[0].cells)):
                tcPr = self.table.cell(r, c)._tc.tcPr
                tcBorders = OxmlElement('w:tcBorders')

                left = OxmlElement('w:left')
                left.set(qn('w:val'), 'single')
                left.set(qn('w:sz'), str(size))
                left.set(qn('w:space'), '0')
                left.set(qn('w:color'), 'auto')
                tcBorders.append(left)

                right = OxmlElement('w:right')
                right.set(qn('w:val'), 'single')
                right.set(qn('w:sz'), str(size))
                right.set(qn('w:space'), '0')
                right.set(qn('w:color'), 'auto')
                tcBorders.append(right)

                top = OxmlElement('w:top')
                top.set(qn('w:val'), 'single')
                top.set(qn('w:sz'), str(size))
                top.set(qn('w:space'), '0')
                top.set(qn('w:color'), 'auto')
                tcBorders.append(top)

                bottom = OxmlElement('w:bottom')
                bottom.set(qn('w:val'), 'single')
                bottom.set(qn('w:sz'), str(size))
                bottom.set(qn('w:space'), '0')
                bottom.set(qn('w:color'), 'auto')
                tcBorders.append(bottom)
                tcPr.append(tcBorders)
Esempio n. 29
0
    def modifyBorder(self, table):
        tbl = table._tbl  # get xml element in table
        for cell in tbl.iter_tcs():
            tcPr = cell.tcPr  # get tcPr element, in which we can define style of borders
            tcBorders = OxmlElement('w:tcBorders')
            top = OxmlElement('w:top')
            top.set(qn('w:val'), 'single')

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

            bottom = OxmlElement('w:bottom')
            bottom.set(qn('w:val'), 'single')
            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'), 'single')

            tcBorders.append(top)
            tcBorders.append(left)
            tcBorders.append(bottom)
            tcBorders.append(right)
            tcPr.append(tcBorders)
Esempio n. 30
0
def set_cell_margins(cell: _Cell, margins):
    """
    margins{top:, start:, bottom:, end:} sizes in Pt
    """
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()
    tcMar = OxmlElement('w:tcMar')

    for k, m in margins.items():
        node = OxmlElement(f'w:{k}')
        node.set(qn('w:w'), str(m))
        node.set(qn('w:type'), 'dxa')
        tcMar.append(node)

    tcPr.append(tcMar)
Esempio n. 31
0
def _add_caption(self, text, sequence_name=None):
    if not sequence_name:
        sequence_name = 'Figure'

    paragraph = self.insert_paragraph_after(text=None, style='Caption')
    paragraph.add_run('%s ' % sequence_name)
    new_fld = OxmlElement('w:fldSimple', attrs={
        qn('w:instr'): r' SEQ %s \* ARABIC ' % sequence_name
    })
    new_r = OxmlElement('w:r')
    new_r.add_t('0')
    new_fld.append(new_r)
    paragraph._p.append(new_fld)
    paragraph.add_run(": %s" % text)

    return paragraph
Esempio n. 32
0
def remove_border(paragraph):
    p = paragraph._p  # p is the <w:p> XML element
    pPr = p.get_or_add_pPr()
    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')
    bottom = OxmlElement('w:bottom')
    bottom.set(qn('w:sz'), '0')
    bottom.set(qn('w:space'), '0')
    bottom.set(qn('w:color'), 'white')
    pBdr.append(bottom)
Esempio n. 33
0
  def __init__(self, fname, lname, cname, date, course, instructor, site, ta, completion):
    
    # Validate required arguments (note cname is not required (can be None))
    x = 0
    for arg in [fname, lname, date, course, instructor, site, ta, completion]:
      x+=1
      if arg is None:
        print(x)
        raise ValueError("Required argument: {} was None.".format(arg))
    
    # Reference only the arguments that will be used outside of the constructor
    self.fname = fname
    self.lname = lname
    self.cname = fname if cname is None else cname
    
    # Create the document
    self.document = Document()
    
    # Set margins
    section = self.document.sections[0]
    section.top_margin = Inches(1.5)
    section.bottom_margin = Inches(.75)
    section.left_margin = Inches(1)
    section.right_margin = Inches(1)
    
    # Title line at the very top
    title = self.document.add_paragraph()
    title.style.font.name = "Times New Roman" # All subsequent text is same font
    title.paragraph_format.space_after = Pt(11)
    title.paragraph_format.alignment = 1 # 1 means center
    title.add_run("CTY SUMMER PROGRAM FINAL EVALUATION").bold = True
        
    # Populate heading information including names, date, site, and course
    heading = self.document.add_paragraph()
    heading.paragraph_format.space_after = Pt(11)
    heading.paragraph_format.line_spacing = 1
    heading.add_run("Student: " + fname + ' ' + lname + '\t')
    heading.add_run("Date: " + date + '\n')
    heading.add_run("Course: " + course + '\t')
    heading.add_run("Instructor: " + instructor + '\n')
    heading.add_run("Site: " + site + '\t')
    heading.add_run("Teaching Assistant: " + ta)

    #TODO I guess there is better API support for tab stops in python-docx now
    # https://python-docx.readthedocs.io/en/latest/dev/analysis/features/text/tab-stops.html
    # Insert a tab stop in the middle of the page for proper heading format.
    # Simplified code from: github.com/python-openxml/python-docx/issues/206
    pTabs = OxmlElement('w:tabs')
    heading.paragraph_format.element.get_or_add_pPr().append(pTabs)
    tab_n = OxmlElement('w:tab')
    tab_n.set(qn('w:val'), "left")
    tab_n.set(qn('w:pos'), '5000')
    pTabs.append(tab_n)
    
    # Setup the intro paragraph
    introText = "Congratulations, " + self.cname + ', '
    introText += "on " + completion + ' ' + course + '. '
    introText += "Please see the enclosed course description for more detailed "
    introText += "information on the course."
    intro = self.document.add_paragraph(introText)
    intro.paragraph_format.space_after = Pt(11)
    
    intro.paragraph_format.line_spacing = 1
    # Keep track of the number of body paragraphs in the document
    self.numParagraphs = 0