'Trouble with getting a XML compatible string for python-docx table

I want to add information to a python-docx table and I'm using the following piece of code:

def addtableinfo(table, col1, col2):
    row = table.add_row()
    row.cells[0].text= col1
    row.cells[1].text= col2

addtableinfo(doc.document.tables[0], "sim", "descrição")

It's giving the following error: ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters

I also used "descrição".encode('utf-8') but didnt work as well. And also tried to get a valid xml using the following code I found online, but none have worked so far.

def valid_xml_char_ordinal(c):
    codepoint = ord(c)
    # conditions ordered by presumed frequency
    return (
        0x20 <= codepoint <= 0xD7FF or
        codepoint in (0x9, 0xA, 0xD) or
        0xE000 <= codepoint <= 0xFFFD or
        0x10000 <= codepoint <= 0x10FFFF)
cleaned_string = ''.join(c for c in input_string if valid_xml_char_ordinal(c))

Does anyone has a hint in how to solve this issue?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source