title: Adding Borders To Cells With Content Utilsing Python-PPTX
body: <p>I have tables with no fill and no borders and I'm trying to add borders to cells that aren't empty, however the following misses certain borders, does anyone have any clue as to where I am going wrong please?</p>
  <p>I note that the output is expected when opening with LibreOffice but in PowerPoint it is incorrect</p>
  <pre><code>from pptx import Presentation
  from pptx.oxml.xmlchemy import OxmlElement
  
  def SubElement(parent, tagname, **kwargs):
      element = OxmlElement(tagname)
      element.attrib.update(kwargs)
      parent.append(element)
      return element
  
  def _set_cell_border(cell, border_color=&quot;000000&quot;, border_width='20000'):
      tc = cell._tc
      tcPr = tc.get_or_add_tcPr()
      for lines in ['a:lnL','a:lnR','a:lnT','a:lnB']:
          
          # Every time before a node is inserted, the nodes with the same tag should be removed.
          tag = lines.split(&quot;:&quot;)[-1]
          for e in tcPr.getchildren():
              if tag in str(e.tag):
                  tcPr.remove(e)
          # end
  
          ln = SubElement(tcPr, lines, w=border_width, cap='flat', cmpd='sng', algn='ctr')
          solidFill = SubElement(ln, 'a:solidFill')
          srgbClr = SubElement(solidFill, 'a:srgbClr', val=border_color)
          prstDash = SubElement(ln, 'a:prstDash', val='solid')
          round_ = SubElement(ln, 'a:round')
          headEnd = SubElement(ln, 'a:headEnd', type='none', w='med', len='med')
          tailEnd = SubElement(ln, 'a:tailEnd', type='none', w='med', len='med')
  
  prs = Presentation(&quot;test3.pptx&quot;)
  for slide in prs.slides:
      for shape in slide.shapes:
          if shape.has_table == True:
              for cell in shape.table.iter_cells():
                  if cell.text != &quot;&quot;:
                      cell = _set_cell_border(cell)
              
  
  
   
  
  prs.save(&quot;test3.pptx&quot;)
              
  
  
  
  </code></pre>