从表格中提取文本并比较单元格-python docx

从表格中提取文本并比较单元格-python docx,python,python-docx,Python,Python Docx,我有一个程序,它使用PythonDocx从表格单元格中的列表中打印随机值。 表、单元格和行的数量取决于用户输入。 我需要先比较表中的单元格,然后再在另一个表的同一个数字单元格中输入值 比如说 number_of_tables = 5 #input by user number_of_rows = 4 #input by user number_of_cols = 7 #input by user list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

我有一个程序,它使用PythonDocx从表格单元格中的列表中打印随机值。 表、单元格和行的数量取决于用户输入。 我需要先比较表中的单元格,然后再在另一个表的同一个数字单元格中输入值

比如说

number_of_tables = 5 #input by user
number_of_rows = 4 #input by user
number_of_cols = 7 #input by user

list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

docu = Document()

for tablenum in range(number_of_tables):
    tablename = docu.add_table(rows = number_of_rows, cols = number_of_cols)
    for rowiteration in tablename.rows[0:]:
        for cells in rowiteration.cells:
            cells.text = random.choices(list)

如果表1中的单元格(0,0)中有“a”,我不想在表2的单元格(0,0)中的“a”中打印

基本上,您希望从
列表中选择一个随机值,但排除一个(或多个)值-另请参见

因此,您应该构建另一个不包含要排除的值的列表-例如从选项中排除值
'a'

random.choice([s for s in list if s != 'a'])
对于您的场景,您必须排除其他表中相同单元格
(r,c)
中的所有值,如下所示:

for tablenum in range(number_of_tables):
  tablename = docu.add_table(rows=number_of_rows, cols=number_of_cols)
  for r, rowiteration in enumerate(tablename.rows):
    for c, cells in enumerate(rowiteration.cells):
      exclude = [docu.tables[num].cell(r,c).text for num in range(tablenum)]
      cells.text = random.choice([s for s in list if s not in exclude])

谢谢我可以从docu中提取上一个表,但我很难指出/提取该表中的特定单元格,以便与当前表单元格进行比较。