def set_vertical_cell_direction(cell: _Cell, direction: str):
    # tbRl -- top to bottom, btLr -- bottom to top
    assert direction in ("tbRl", "btLr")
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()
    textDirection = OxmlElement('w:textDirection')
    textDirection.set(qn('w:val'), direction)  # btLr tbRl
    tcPr.append(textDirection)
Exemple #2
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
Exemple #3
0
def set_raws_height(rows):
    for i in range(len(rows)):
        tr = rows[i]._tr
        trPr = tr.get_or_add_trPr()
        trHeight = OxmlElement('w:trHeight')
        trHeight.set(qn('w:val'), "512")
        trHeight.set(qn('w:hRule'), "atLeast")
        trPr.append(trHeight)
Exemple #4
0
def main() -> None:
    # Open the docx document
    document = Document(sys.argv[1])

    # Retrieve all paragraphs present in the document
    paragraphs = document.paragraphs

    # Iterate over each paragraph
    for paragraph in paragraphs:
        # Check whether a paragraph is a title, author name/s, date, figure, and
        # image or table caption
        if paragraph.style.name in [
                "Title",
                "Author",
                "Date",
                "Captioned Figure",
                "Table Caption",
                "Image Caption",
        ]:
            # Center the caption
            paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
        else:
            # Fully justify the paragraph
            paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY

    # Iterate over each table
    for table in document.tables:
        # Center-align a table
        table.alignment = WD_TABLE_ALIGNMENT.CENTER
        # Allow autofit
        table.allow_autofit = True
        # Autofit the table
        table.autofit = True
        # Set the width of the table to auto, in this way its dimensions
        # will be computed automatically
        table._tblPr.xpath("./w:tblW")[0].attrib[
            "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}type"] = "auto"
        # Iterate over each row of a table
        for row in table.rows:
            # Iterate over each cell contained in a row
            for cell in row.cells:
                # Get a pointer to the properties of a cell
                tcPr = cell._tc.get_or_add_tcPr()

                # Set the type of a cell according to the content
                tcType = OxmlElement("w:type")
                tcType.set(qn("w:val"), "auto")
                tcPr.append(tcType)

                # Set the width of a cell to 0, since the width will be
                # computed automatically depending on the content
                tcWidth = OxmlElement("w:w")
                tcWidth.set(qn("w:val"), "0")
                tcPr.append(tcWidth)

    # Save the new formatted document
    document.save(sys.argv[1])
def set_repeat_table_header(row):
    ''' set repeat table row on every new page
    '''
    tr = row._tr
    trPr = tr.get_or_add_trPr()
    tblHeader = OxmlElement('w:tblHeader')
    tblHeader.set(qn('w:val'), "true")
    trPr.append(tblHeader)
    return row
    def format(self) -> None:
        # center table within the page
        self.alignment = WD_TABLE_ALIGNMENT.CENTER

        # set column width for the patient details
        # 3cm is a good default width to fit dob and age on one line
        for cell in self.columns[1].cells:
            cell.width = Cm(3)

        # format the rows and cells
        for row in self.rows:
            # format the column headers and set header row to repeat
            if row.cells[0].text.lower() == "bed":
                for cell in row.cells:
                    for paragraph in cell.paragraphs:
                        for run in paragraph.runs:
                            run.underline = True
                tr = row._tr
                trPr = tr.get_or_add_trPr()
                tblHeader = OxmlElement("w:tblHeader")
                tblHeader.set(qn("w:val"), "true")
                trPr.append(tblHeader)

            # format the ward headers
            if row.cells[0].text == row.cells[1].text:
                shading_elm = parse_xml(f'<w:shd {nsdecls("w")} w:fill="EEEEEE"/>')
                for cell in row.cells:
                    cell._tc.get_or_add_tcPr().append(shading_elm)
                    # keep this header row on the same page as the next row
                    for paragraph in cell.paragraphs:
                        paragraph.paragraph_format.keep_with_next = True
                if "birthday" in row.cells[0].text:
                    for cell in row.cells:
                        for para in cell.paragraphs:
                            for run in para.runs:
                                run.italic = True

            # format new patients as bold
            if row._index in self._new_patient_indices:
                for cell in row.cells:
                    for para in cell.paragraphs:
                        for run in para.runs:
                            run.bold = True

            # format all cells in the table
            for cell in row.cells:
                cell.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
                for paragraph in cell.paragraphs:
                    formatter = paragraph.paragraph_format
                    formatter.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
                    formatter.space_before = Pt(2)
                    formatter.space_after = Pt(2)

            # prevent table rows from splitting across page breaks
            no_split = parse_xml(f"<w:cantSplit {nsdecls('w')}/>")
            row._tr.get_or_add_trPr().append(no_split)
Exemple #7
0
def add_bgcolor(cell, element):
    '''
    Adds a background (fill) colour to the given cell based on given element.
    '''
    if element.background_color:
        tc = cell._tc
        tcPr = tc.get_or_add_tcPr()
        tcShd = OxmlElement('w:shd')
        tcShd.set(qn('w:fill'), element.background_color)
        tcPr.append(tcShd)
Exemple #8
0
def set_cell_border(cell):
    kwargs = {
        "top": {
            "sz": 12,
            "val": "single"
        },
        "bottom": {
            "sz": 12,
            "val": "single"
        },
        "left": {
            "sz": 12,
            "val": "single"
        },
        "right": {
            "sz": 12,
            "val": "single"
        },
    }
    """
    Set cell`s border
    Usage:
    set_cell_border(
        cell,
        top={"sz": 12, "val": "single", "color": "#FF0000", "space": "0"},
        bottom={"sz": 12, "color": "#00FF00", "val": "single"},
        left={"sz": 24, "val": "dashed", "shadow": "true"},
        right={"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 ('left', 'top', 'right', '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]))
Exemple #9
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
Exemple #10
0
def set_cell_vertical_alignment(cell, align="center"):
    try:
        tc = cell._tc
        tcPr = tc.get_or_add_tcPr()
        tcValign = OxmlElement('w:vAlign')
        tcValign.set(qn('w:val'), align)
        tcPr.append(tcValign)
        return True
    except:
        traceback.print_exc()
        return False
Exemple #11
0
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)
Exemple #12
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)
Exemple #13
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)
Exemple #14
0
    def add_shading(self, hex_color_string=None):
        """
        XML representation
            <w:shd w:fill="1F497D"/>
        """
        hex_color_string = hex_color_string or '#888888'
        if '#' in hex_color_string:
            hex_color_string = hex_color_string[1:]

        rPr = self.ref._element.get_or_add_rPr()
        ele = OxmlElement('w:shd')
        ele.set(qn('w:fill'), hex_color_string)
        rPr.append(ele)
Exemple #15
0
 def _coloring_cells(self, cell, condition):
     if condition == 'Critical':
         color = '#960000'
     elif condition == 'High':
         color = '#fa3434'
     elif condition == 'Medium':
         color = '#ffa929'
     else:
         color = 'ffff40'
     tcPr = cell._tc.get_or_add_tcPr()
     tcVAlign = OxmlElement("w:shd")
     tcVAlign.set(qn("w:fill"), color)
     tcPr.append(tcVAlign)
Exemple #16
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)
Exemple #17
0
def indent_table(table, indent:float):
    '''Indent a table.
    
    Args:
        table (Table): ``python-docx`` Table object.
        indent (float): Indent value, the basic unit is 1/20 pt.
    '''
    tbl_pr = table._element.xpath('w:tblPr')
    if tbl_pr:
        e = OxmlElement('w:tblInd')
        e.set(qn('w:w'), str(20*indent)) # basic unit 1/20 pt for openxml 
        e.set(qn('w:type'), 'dxa')
        tbl_pr[0].append(e)
Exemple #18
0
def indent_table(table, indent: float):
    ''' indent table.
        ---
        Args:
          - table: docx table object
          - indent: indent value, the basic unit is 1/20 pt
    '''
    tbl_pr = table._element.xpath('w:tblPr')
    if tbl_pr:
        e = OxmlElement('w:tblInd')
        e.set(qn('w:w'), str(20 * indent))  # basic unit 1/20 pt for openxml
        e.set(qn('w:type'), 'dxa')
        tbl_pr[0].append(e)
Exemple #19
0
def set_vertical_cell_direction(cell: _Cell, direction: str = 'btLr'):
    '''Set vertical text direction for cell.
        ---
        Args:
          - direction: tbRl -- top to bottom, btLr -- bottom to top
        
        https://stackoverflow.com/questions/47738013/how-to-rotate-text-in-table-cells
    '''
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()
    textDirection = OxmlElement('w:textDirection')
    textDirection.set(qn('w:val'), direction)  # btLr tbRl
    tcPr.append(textDirection)
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)
Exemple #21
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
Exemple #22
0
def set_vertical_cell_direction(cell:_Cell, direction:str='btLr'):
    '''Set vertical text direction for cell.

    Reference:
        https://stackoverflow.com/questions/47738013/how-to-rotate-text-in-table-cells
    
    Args:
        direction (str): Either "tbRl" (top to bottom) or "btLr" (bottom to top).
    '''
    tc = cell._tc
    tcPr = tc.get_or_add_tcPr()
    textDirection = OxmlElement('w:textDirection')
    textDirection.set(qn('w:val'), direction)  # btLr tbRl
    tcPr.append(textDirection)
Exemple #23
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)
Exemple #24
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
Exemple #25
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
Exemple #26
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
Exemple #27
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
Exemple #28
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
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
Exemple #30
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
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
  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
Exemple #33
0
def saveDoc(self, py):
    """" формирование документа"""
    self.py = py
    document = docx.Document()
    sections = document.sections
    section = sections[0]
    section.left_margin = Inches(1)
    section.right_margin = Inches(0.65)
    section.page_width = Inches(8.3)
    section.page_height = Inches(11.7)

    styles = document.styles
    styles["List Number"].font.name = "Calibri"
    styles["Normal"].font.name = "Calibri"

    style = styles.add_style('table_z', WD_STYLE_TYPE.PARAGRAPH)
    style.base_style = styles['Normal']

    style = styles["table_z"]
    style.font.bold = True

    style = styles.add_style('Code', WD_STYLE_TYPE.PARAGRAPH)
    style.base_style = styles['Normal']
    style.font.name = "Courier New"
    style.font.size = Pt(10)
    styles = document.styles
    style = styles["Code"]

    p = style._element.get_or_add_pPr()
    numPr = OxmlElement('w:shd')  # create number properties element
    numPr.set(qn('w:fill'), "FEF2E8")  # set list type/indentation
    p.append(numPr)

    style = styles.add_style('hyperlink', WD_STYLE_TYPE.CHARACTER)
    style.base_style = styles['Default Paragraph Font']
    style = styles['hyperlink']
    style.font._element.get_or_add_rPr()
    style.font._element.get_or_add_unhideWhenUsed()
    style.font._element.get_or_add_semiHidden()
    style.font._element.rPr.get_or_add_u()
    style.font._element.rPr.u.val = WD_UNDERLINE.SINGLE
    style.font._element.rPr.get_or_add_color()
    style.font._element.rPr.color.val = RGBColor(2, 3, 226)
    style.font._element.rPr.color.themeColor = MSO_THEME_COLOR.HYPERLINK

    h = document.add_header()
    hp = h.add_paragraph(text1 % (self.py.nameProject.itemText(self.py.nameProject.currentIndex()),
                                  self.py.numBid.text()))
    hp.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    document.add_paragraph(u"Цель", style='table_z')
    document.add_paragraph(text2_0 % (self.py.nameProject.itemText(self.py.nameProject.currentIndex())))
    h = document.add_paragraph('', style='List Bullet 3')
    h.add_run(text2_1 % (self.py.numberBid.text())).style = styles['hyperlink']
    h.add_run(' - %s' % (self.py.specBid.text()))

    document.add_heading("Этап 1. Подготовка к публикации", level=2)
    document.add_paragraph(text3 % (self.py.timeIn.text(), self.py.timeOut.text(), self.py.dateIn.text()))
    make_table(document, self.py.tableView.model().cached, 0)
    document.add_paragraph()
    document.add_paragraph(text19_0)
    document.add_paragraph(text19_1)
    make_table(document, self.py.tableView.model().cached, 1)
    document.add_paragraph()
    document.add_paragraph(text20)
    document.add_paragraph()
    document.add_heading("Этап 2. Публикация изменений", level=2)
    document.add_paragraph()
    listSer = []
    listApp = []
    listVamish = []
    test = []
    if self.py.app1.text() != "":
        listSer = {self.py.app1.text(): self.py.ipAddress1.text()}
        listApp = [self.py.app1.text()]
        listVamish = [self.py.vamish1.text()]
        test = [self.py.test1.text()]
    if self.py.app2.text() != "":
        listSer[self.py.app2.text()] = self.py.ipAddress2.text()
        listApp.append(self.py.app2.text())
        listVamish.append(self.py.vamish2.text())
        test.append(self.py.test2.text())
    if self.py.app3.text() != "":
        listSer[self.py.app3.text()] = self.py.ipAddress3.text()
        listApp.append(self.py.app3.text())
        listVamish.append(self.py.vamish3.text())
        test.append(self.py.test3.text())
    if self.py.app4.text() != "":
        listSer[self.py.app4.text()] = self.py.ipAddress4.text()
        listApp.append(self.py.app4.text())
        listVamish.append(self.py.vamish4.text())
        test.append(self.py.test4.text())
    document.add_paragraph(text5 % ", ".join(i for i in listSer), style='List Number')
    for i in range(0, len(listApp)):
        if (len(listApp) - 1) == i:
            serverPer = 0
        else:
            serverPer = i + 1
        com1 = bashIPPost % (listSer.get(listApp[serverPer]), self.py.portApp.text(), listSer.get(listApp[i]))
        com2 = bashIPPre % (listSer.get(listApp[i]), self.py.portApp.text(), listSer.get(listApp[serverPer]))
        document.add_paragraph(text6 % (self.py.nameProject.itemText(self.py.nameProject.currentIndex()),
                                        listApp[i], listApp[serverPer]), style='List Number')
        document.add_paragraph("%s \n%s" % (com1, com2), style="Code")
        document.add_paragraph(text7 % (listApp[i], self.py.numberRev.text()), style='List Number')
        h = document.add_paragraph('svn switch ', style="Code")
        h.add_run(self.py.svn.text()).style = styles['hyperlink']
        h.add_run('\nsvn update')
        document.add_paragraph(text8 % (listApp[i]), style='List Number')
        document.add_paragraph("%s \n%s \n%s" % (text9, com2, com1), style="Code")
        if self.py.cache.isChecked():
            document.add_paragraph(text10 % (listVamish[i]), style='List Number')
            document.add_paragraph(self.py.comandCache.text(), style="Code")
        document.add_paragraph(text12, style='List Number')
        document.add_paragraph(text13_0, style='List Bullet')
        document.add_paragraph(text13_1, style='List Bullet')
        document.add_paragraph(text13_2, style='List Bullet')
        document.add_paragraph(text13_3, style='List Bullet')
        for j in (self.py.tableView.model().cached):
            if j[2]:
                h = document.add_paragraph(style='List Paragraph')
                create_list(h, i)
                h.add_run(j[6]).style = styles['hyperlink']
        document.add_paragraph(text14, style='List Number')
        document.add_paragraph(text17 % (test[i]), style="Code")
        s = ", ".join(c for c in listApp[0:i+1])
        document.add_paragraph(text15 % s, style='List Number')
    document.add_paragraph(text20, style="List Number")
    document.add_heading("Этап 3. Мониторинг работы", level=2)
    document.add_paragraph(text4)

    return document