如何使用Python根据其他文本突出显示单元格中的特定文本?

问题描述 投票:0回答:1

在我的excel文件中有两列.A列中有段落,B列中有单词。我的要求是B列中的单词应该在A列中突出显示,就像B列中的害虫一样。有多少害虫第一列中应突出显示。第二行中应突出显示。我已经编写了代码,但是突出了整行enter image description here

我的代码:

from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.styles import PatternFill
from openpyxl.styles import colors
from openpyxl.styles import Font, Color
import pandas as pd
ft = Font(color=colors.GREEN, bold=True)
wb = load_workbook('sample_sentence.xlsx')
ws = wb.active
a1 = ws['A']
for a in a1:
    a2 = a
for cell1 in ws['A']:
   for cell in ws['B']:
      if cell.value.lower() in cell1.value.lower():
         a2.font = ft
         wb.save('sample_sentence.xlsx')

我的要求是在第一A1栏害虫应突出显示,在第二A2栏肯定要突出显示

python excel conditional-statements
1个回答
0
投票

在VBA中,这可以解决为:

© www.soinside.com 2019 - 2024. All rights reserved.