def config_borders(document: Document): section_properties = document.sections[0]._sectPr page_borders = OxmlElement('w:pgBorders') page_borders.set(qn('w:offsetFrom'), 'page') for border_name in ('top', 'bottom'): border = OxmlElement(f'w:{border_name}') border.set(qn('w:val'), 'single') border.set(qn('w:sz'), '8') border.set(qn('w:space'), '10') border.set(qn('w:color'), 'auto') page_borders.append(border) for border_name in ('right', 'left'): border = OxmlElement(f'w:{border_name}') border.set(qn('w:val'), 'single') border.set(qn('w:sz'), '8') border.set(qn('w:space'), '15') border.set(qn('w:color'), 'auto') page_borders.append(border) section_properties.append(page_borders) return
def set_cell_border(cell, **kwargs): """ 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() # 添加标签,如果不存在新增 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) # 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 StartBtn(): document = Document() section = document.sections[0] style = document.styles['Normal'] font = style.font header = section.header header.is_linked_to_previous = False paragraph = header.paragraphs[0] font.size = Pt(35) paragraph.text = TextVal.get() paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER sec_pr = document.sections[0]._sectPr # pg_borders = OxmlElement('w:pgBorders') pg_borders.set(qn('w:offsetFrom'), 'page') for border_name in ( 'top', 'left', 'bottom', 'right', ): # set all borders border_el = OxmlElement(f'w:{border_name}') border_el.set(qn('w:val'), 'single') #single line border_el.set(qn('w:sz'), '24') # for meaning of remaining attrs please look docs border_el.set(qn('w:space'), '26') border_el.set(qn('w:color'), 'blue') pg_borders.append(border_el) # register single border to border el sec_pr.append(pg_borders) # document.add_paragraph( 'Inspection File is ready',style="Caption") paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER global size1, size2 real = n.get() if (real == 2): sectPr = section._sectPr cols = sectPr.xpath('./w:cols')[0] cols.set(qn('w:num'), '1') size1 = 6.2362204724 size2 = 4.0787402 elif (real == 3): size1 = 5.354331 size2 = 2.771654 elif (real == 6): sectPr = section._sectPr cols = sectPr.xpath('./w:cols')[0] cols.set(qn('w:num'), '2') size1 = 2.75591 size2 = 2.75591 for file in glob.glob('*.%s' % ext): img = Image.open(file) img = img.resize((1024, 768)) img.save(file) paragraph_format = paragraph.paragraph_format p = document.add_paragraph() r = p.add_run() r.add_picture(file, width=Inches(size1), height=Inches(size2)) last_paragraph = document.paragraphs[-1] last_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER filenam = TextVal.get() document.save(filenam + ".docx") print(real) messagebox.showinfo("COMPRESS & SAVE ", "YOUR DOCUMENT IS READY") print('Done')