Python 用熊猫给excel中的细胞着色
我需要一些帮助。所以我有类似的东西Python 用熊猫给excel中的细胞着色,python,excel,pandas,duplicates,highlight,Python,Excel,Pandas,Duplicates,Highlight,我需要一些帮助。所以我有类似的东西 import pandas as pd path = '/Users/arronteb/Desktop/excel/ejemplo.xlsx' xlsx = pd.ExcelFile(path) df = pd.read_excel(xlsx,'Sheet1') df['is_duplicated'] = df.duplicated('#CSR') df_nodup = df.loc[df['is_duplicated'] == False] df_nodup
import pandas as pd
path = '/Users/arronteb/Desktop/excel/ejemplo.xlsx'
xlsx = pd.ExcelFile(path)
df = pd.read_excel(xlsx,'Sheet1')
df['is_duplicated'] = df.duplicated('#CSR')
df_nodup = df.loc[df['is_duplicated'] == False]
df_nodup.to_excel('ejemplo.xlsx', encoding='utf-8')
因此,基本上这个程序将
ejempo.xlsx
(ejempo是西班牙语的示例,只是文件名)加载到df
(aDataFrame
)中,然后检查特定列中的重复值. 它会删除重复项并再次保存文件。那部分工作正常。问题是,我需要用不同的颜色(如黄色)高亮显示包含重复项的单元格,而不是删除重复项。您可以创建一个函数来进行高亮显示
def highlight_cells():
# provide your criteria for highlighting the cells here
return ['background-color: yellow']
然后将突出显示功能应用于数据帧
df.style.apply(highlight_cells)
我也遇到了同样的问题,我这周刚解决了。我的问题是没有让include正常工作,以获得我发现正常工作的在线代码 我假设你的意思是改变背景颜色,而不是改变字体颜色。如果我错了,请澄清你的要求 我的解决方案与特定的库相关联。openpyxl
#### This import section is where my mistake was at
#### This works for me
import openpyxl ### Excel files
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
from openpyxl.styles import Fill, Color
from openpyxl.styles import Style
from openpyxl.styles.colors import RED
from openpyxl.styles.colors import GREEN
str_xls_PathFileCurrent = str_xls_FileName
### Opens Excel Document
var_xls_FileOpen = openpyxl.load_workbook(str_xls_PathFileCurrent)
### Opens up the Excel worksheet
var_xls_TabName = var_xls_FileOpen.worksheets[0]
### Put the spreadsheet tab names into an array
ary_xls_SheetNames = var_xls_FileOpen.get_sheet_names()
### Open the sheet in the file you working on
var_xls_TabSheet = var_xls_FileOpen.get_sheet_by_name(ary_xls_SheetNames[0])
xls_cell = var_xls_TabSheet['d10']
#### Changes the cell background color
xls_cell.style = Style(fill=PatternFill(patternType='solid'
, fgColor=Color('C4C4C4'))) ### Changes background color
#### Changes the fonts (does not use style)
xls_cell.font = xls_cell.font.copy(color = 'FFFF0000') ### Works (Changes to red font text)
xls_cell.font = xls_cell.font.copy(bold = True) ### Works (Changes to bold font)
xls_cell.font = xls_cell.font.copy(italic= True) ### Works (Changes to Italic Text)
xls_cell.font = xls_cell.font.copy(size = 34) ### Works (Changes Size)
右边是背景色。我将尝试使用您的部分代码。谢谢谢谢我试试这个way@CarlosArronteBello运气好吗?@CarlosArronteBello太好了!很高兴我能够帮助您。这仅适用于0.17.1版以后的版本,如中所述