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)
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)
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
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)
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
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
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]))
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 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)
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)
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)
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)
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))
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]))
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')
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
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
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]))
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)
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
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 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)
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)
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)
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_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 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 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)
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]))
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)