def write_table(tab): """Write to output file table elements. This function is called every time, we meet 'Table' dictionary's title. Firstly, if we have some information in 'string_to_write' we record it, because we'll use this variable to collect information from table's cells. Table in pandoc's json has following structure: dict: { 't': 'Table' 'c': [ [0] [1] [2] [3] [4] ] } Where: [0] - caption. [1] - is list of aligns by columns, looks like: [ { t: 'AlignDefault' }, ... ]. [2] - widths of columns. [3] - is list of table's headers (top cell of every column), can be empty. [4] - list of rows, and row is list of cells. Since every cell's structure is the same as text's one, we just parse them as list and write one by one. Args: tab - dictionary with 't': 'Table". """ global table global string_to_write global fmt for k in fmt.keys( ): # setting to zero all outside-table formatting, we use formatting ONLY inside table-cell fmt[k] = 0 if string_to_write: # writing all text from buffer, table has it's own rules for rows, cols and their shift-rules write_text() row = TableRow() # adding empty line before table table.addElement(row) row = TableRow() headers = tab['c'][3] if headers: cell = TableCell( ) # adding empty first cell in row (first column in document - text column). row.addElement(cell) for col in headers: cell = TableCell() list_parse(col, without_write=True) add_style(cell, table_header) cell_content = P(text=string_to_write) for key in fmt.keys(): if fmt[key] == 1: new_style = add_fmt( style=st_dict[cell.getAttribute(attr='stylename')], key=key) ods.styles.addElement(new_style) fmt[key] = 0 cell = TableCell(stylename=new_style.getAttribute( attr='name')) cell.addElement(cell_content) string_to_write = '' row.addElement(cell) table.addElement(row) t_content = tab['c'][4] for line in t_content: row = TableRow() cell = TableCell() row.addElement(cell) for col in line: cell = TableCell() list_parse(col, without_write=True) add_style(cell, table_content) cell_content = P(text=string_to_write) for key in fmt.keys(): if fmt[key] == 1: new_style = add_fmt( style=st_dict[cell.getAttribute(attr='stylename')], key=key) ods.styles.addElement(new_style) fmt[key] = 0 cell = TableCell(stylename=new_style.getAttribute( attr='name')) cell.addElement(cell_content) string_to_write = '' row.addElement(cell) table.addElement(row) row = TableRow() # adding empty line after table table.addElement(row)