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 _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 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 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 set_cell_border(cell:_Cell, **kwargs): '''Set cell`s border. Reference: * https://stackoverflow.com/questions/33069697/how-to-setup-cell-borders-with-python-docx * https://blog.csdn.net/weixin_44312186/article/details/104944110 Args: cell (_Cell): ``python-docx`` Cell instance you want to modify. kwargs (dict): Dict with keys: top, bottom, start, end. 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 existence, 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 existence, 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 set_answer_space(cell): tc = cell._tc tc_pr = tc.get_or_add_tcPr() tc_borders = tc_pr.first_child_found_in("w:tcBorders") if tc_borders is None: tc_borders = OxmlElement('w:tcBorders') tc_pr.append(tc_borders) tag = 'w:{}'.format('bottom') element = tc_borders.find(qn(tag)) if element is None: element = OxmlElement(tag) tc_borders.append(element) element.set(qn('w:color'), '#000000') element.set(qn('w:val'), 'single')
def set_paragraph_border(paragraph, **kwargs): """ Set paragraph's border Usage: set_paragraph_border( paragraph, 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"}, ) """ pPr = paragraph._p.get_or_add_pPr() # check for tag existnace, if none found, then create one pBorders = pPr.first_child_found_in("w:pBorders") if pBorders is None: pBorders = OxmlElement('w:pBorders') pPr.append(pBorders) # list over all available tags for edge in ('top', 'start', 'bottom', 'end'): edge_data = kwargs.get(edge) if edge_data: edge_str = edge if edge_str == 'start': edge_str = 'left' if edge_str == 'end': edge_str = 'right' tag = 'w:{}'.format(edge_str) # check for tag existnace, if none found, then create one element = pBorders.find(qn(tag)) if element is None: element = OxmlElement(tag) pBorders.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]))