我有一个要打印到表中的单词列表(print_data),另一个单词列表(列表项)应该为其他列表中的某些单词着色。我需要自动包装第一张清单。
我将彼此分开做这两种事情,但是将代码组合在一起将不会返回彩色单词。只有普通表。
这是我的代码:
print_data_set = [['hi'],['word'],['some sentance']]
list_item=['hi','some']
""" Seitenlayout"""
doc = BaseDocTemplate(
"example.pdf",
pagesize=A4,
rightMargin=45,
leftMargin=61,
topMargin=50,
bottomMargin=70,
showBoundary=False)
elements = []
""" Tabellen Überschrift """
styles = getSampleStyleSheet()
styleN = styles['Normal']
styleN.wordWrap = 'CJK'
frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height - 2 * cm, id='normal')
""" Tabellenlayout"""
for list in print_data_set:
max_num = (len(list))
break
data2 = [[Paragraph(cell, styleN) for cell in row] for row in print_data_set]
col_width = 1 * cm
available_width = doc.width - 3.7 * cm
max_number_of_cols = floor(available_width / col_width)
table_style = TableStyle([('ALIGN', (0, 0), (-1, -1), 'RIGHT')])
for row, values, in enumerate(data2):
for column, value in enumerate(values):
for item in list_item:
if value == item:
table_style.add('TEXTCOLOR', (column, row), (column, row), red)
frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height - 2 * cm, id='normal')
colwidths = [frame._width / 6. for i in range(6)]
t = LongTable(data2, colWidths=colwidths)
t.setStyle(table_style)
elements.append(t)
template = PageTemplate(id='longtable', frames=frame)
doc.addPageTemplates([template])
doc.build(elements)