@ilwdchar_compat<EOL>def write_tables_to_document(xmldoc, tables, overwrite=False):
from ligo.lw.ligolw import LIGO_LW<EOL>from ligo.lw import lsctables<EOL>try:<EOL><INDENT>llw = get_ligolw_element(xmldoc)<EOL><DEDENT>except ValueError:<EOL><INDENT>llw = LIGO_LW()<EOL>xmldoc.appendChild(llw)<EOL><DEDENT>for table in tables:<EOL><INDENT>try:  <EOL><INDENT>old = lsctables.TableByName[<EOL>table.TableName(table.Name)].get_table(xmldoc)<EOL><DEDENT>except ValueError:  <EOL><INDENT>llw.appendChild(table)<EOL><DEDENT>else:<EOL><INDENT>if overwrite:<EOL><INDENT>llw.removeChild(old)<EOL>old.unlink()<EOL>llw.appendChild(table)<EOL><DEDENT>else:<EOL><INDENT>old.extend(table)<EOL><DEDENT><DEDENT><DEDENT>return xmldoc<EOL>
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