我想使用xlsxwriter更改字符串中的某些文本颜色。 我的想法是用彩色文本替换非彩色文本。 但是失败了...
结果显示“ TypeError:'Format'对象无法解释为整数”
似乎f“ {wrong}”,cell_format)是一个整数。
这很奇怪,因为如果我们不能使用replace()
来更改字符串中的单个字体颜色,还能做什么呢?
我的输出是:
应该是:
我的代码:
import xlsxwriter
from functools import partial
def x_in_y(word, inner):
return inner in word
workbook = xlsxwriter.Workbook('C:\\Users\\Claude\\Desktop\\hello.xlsx')
worksheet = workbook.add_worksheet()
cell_format = workbook.add_format()
cell_format.set_font_color('red')
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.replace(wrong, f"{wrong}",cell_format))
for name in filtered_names:
worksheet.write(row+2,0,name)
row += 2
workbook.close()
答案 0 :(得分:1)
因此,我在工作中遇到了类似的情况,我认为无法部分格式化字符串,更不用说根据某些特定条件(例如您的情况)了。我看到了您的帖子以及令人惊异的约翰·麦克纳马拉(John Mcnamara)的回复,因此我决定尝试使用富字符串方法(我真的怀疑是否还有其他方法)。
首先让我提及一下,我能够使用pandas和xlsxwriter实现它。其次,应避免使用pandas和xlsxwriter进行for循环(因为文件越多的行具有完成程序所需的时间越长),但是我无法以不同的方式实现它。您需要在此处进行一些错误处理,因为如果索引值不存在,则会引发值错误。最后,我没有包括一个单元格包含多个错误单词的情况,我们需要对所有单词进行格式化。
这就是我要做的:
import pandas as pd
# Create your dataframe
df = pd.DataFrame(data={'A': ["Typo: pasport 'should be passport'", 'pasport',
"Typo: limmit 'should be limit'", 'limmit',
"Typo: putt 'should be put'", 'putt dweqrerwr',
'dfsdf putt']})
# Create a list with the words that are wrong
wrong_words = ['pasport', 'limmit', 'putt']
# Kickstart the xlsxwriter
writer = pd.ExcelWriter('Testing rich strings.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', header=False, index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Define the red format and a default format
cell_format_red = workbook.add_format({'font_color': 'red'})
cell_format_default = workbook.add_format({'bold': False})
# Start iterating through the rows and through all of the words in the list
for row in range(0,df.shape[0]):
for word in wrong_words:
try:
# 1st case, wrong word is at the start and there is additional text
if (df.iloc[row,0].index(word) == 0) \
and (len(df.iloc[row,0]) != len(word)):
worksheet.write_rich_string(row, 0, cell_format_red, word,
cell_format_default,
df.iloc[row,0][len(word):])
# 2nd case, wrong word is at the middle of the string
elif (df.iloc[row,0].index(word) > 0) \
and (df.iloc[row,0].index(word) != len(df.iloc[row,0])-len(word)) \
and ('Typo:' not in df.iloc[row,0]):
starting_point = df.iloc[row,0].index(word)
worksheet.write_rich_string(row, 0, cell_format_default,
df.iloc[row,0][0:starting_point],
cell_format_red, word, cell_format_default,
df.iloc[row,0][starting_point+len(word):])
# 3rd case, wrong word is at the end of the string
elif (df.iloc[row,0].index(word) > 0) \
and (df.iloc[row,0].index(word) == len(df.iloc[row,0])-len(word)):
starting_point = df.iloc[row,0].index(word)
worksheet.write_rich_string(row, 0, cell_format_default,
df.iloc[row,0][0:starting_point],
cell_format_red, word)
# 4th case, wrong word is the only one in the string
elif (df.iloc[row,0].index(word) == 0) \
and (len(df.iloc[row,0]) == len(word)):
worksheet.write(row, 0, word, cell_format_red)
except ValueError:
continue
writer.save()
最终输出与所需输出相同:
我希望这会有所帮助。