我的代码:
import xlsxwriter
from functools import partial
def x_in_y(word, inner):
return inner in word
workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()
words = [
('pasport','passport'),
('limmit','limit'),
('putt','put')
]
sentence =['putt dweqrerwr','dfsdf putt','limmit','pasport']
row = 0
for wrong,correct in words:
filtered_names = filter(partial(x_in_y, inner=wrong), sentence)
next_elem = next(filtered_names, None)
if next_elem:
worksheet.write(row,0, f"Typo: {wrong} 'should be {correct}'")
worksheet.write(row+1,0,next_elem)
for name in filtered_names:
worksheet.write(row+2,0,name)
row += 2
workbook.close()
我希望结果可以是:
错别字:pasport“应为护照”
体育
错别字:限制“应该是极限”
限制
错别字:推杆“应该放”
输入 dweqrerwrdfsdf
输入
粗体表示红色。
我希望黑体文字为红色。
但是该怎么做呢?我尝试过worksheet.write_rich_string()
,但应先将文本切成薄片。
如何切片f“ {wrong}”?