code: def write_tables_to_document(xmldoc, tables, overwrite=False):
      
      from ligo.lw.ligolw import LIGO_LW
      from ligo.lw import lsctables
  
      # find or create LIGO_LW tag
      try:
          llw = get_ligolw_element(xmldoc)
      except ValueError:
          llw = LIGO_LW()
          xmldoc.appendChild(llw)
  
      for table in tables:
          try:  # append new data to existing table
              old = lsctables.TableByName[
                  table.TableName(table.Name)].get_table(xmldoc)
          except ValueError:  # or create a new table
              llw.appendChild(table)
          else:
              if overwrite:
                  llw.removeChild(old)
                  old.unlink()
                  llw.appendChild(table)
              else:
                  old.extend(table)
  
      return xmldoc
docs: Write the given LIGO_LW table into a :class:`Document`
  
      Parameters
      ----------
      xmldoc : :class:`~ligo.lw.ligolw.Document`
          the document to write into
  
      tables : `list` of :class:`~ligo.lw.table.Table`
          the set of tables to write
  
      overwrite : `bool`, optional, default: `False`
          if `True`, delete an existing instance of the table type, otherwise
          append new rows