py如何让搜索结果获取表格中的文字颜色
import tkinter as tkfrom tkinter import ttk
import openpyxl
def search_excel(file_path, sheet_name, keyword):
wb = openpyxl.load_workbook(file_path)
sheet = wb
result = []
for row in sheet.iter_rows(values_only=True):
if keyword.lower() in ' '.join(str(cell) for cell in row).lower():
result.append(row)
return result
def perform_search(*args):
file_path = 'data.xlsx'# Excel文件路径
sheet_name = 'Sheet1' # 表格名称
keyword = search_var.get()
results = search_excel(file_path, sheet_name, keyword)
# 清空Treeview中的数据
for i in treeview.get_children():
treeview.delete(i)
# 插入查询结果到Treeview中
for index, row in enumerate(results, start=1):
treeview.insert('', 'end', values=(str(index),) + row)
# 创建GUI窗口
window = tk.Tk()
window.title("Excel查询软件")
# 设置窗口大小为600x300
window.geometry("1000x300")
window_width = 1000
window_height = 300
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
window.geometry(f"{window_width}x{window_height}+{x}+{y}")
# 查询输入框
search_label = tk.Label(window, text="请输入关键字:")
search_label.pack()
search_var = tk.StringVar()
def on_search_change(*args):
perform_search()
# 跟踪输入框的值变化
search_var.trace('w', on_search_change)
search_entry = tk.Entry(window, textvariable=search_var)
search_entry.pack()
# 查询结果显示区域
treeview_frame = ttk.Frame(window)
treeview_frame.pack(side=tk.TOP, fill=tk.BOTH)
treeview_scrollbar = ttk.Scrollbar(treeview_frame)
treeview_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# 设置查询结果显示窗口大小为600x300
treeview = ttk.Treeview(treeview_frame, yscrollcommand=treeview_scrollbar.set, height=10)
treeview.pack(fill=tk.BOTH, expand=True)
treeview_scrollbar.config(command=treeview.yview)
# 定义表格列名和列宽
columns = [('索引列', -200), ('列1', -50), ('列2', 120), ('列3', 140), ('列4', 140)]# 根据实际情况修改列名和列宽
# 设置Treeview列名
treeview['columns'] = for col in columns]
# 设置每列的属性和宽度
treeview.column('#0', width=columns)
for index, (column_name, column_width) in enumerate(columns, start=1):
treeview.column('#' + str(index), width=column_width)
treeview.heading('#' + str(index), text=column_name)
# 运行窗口主循环
window.mainloop()
请问下大佬,如何在以上内容,添加一个 让搜索出来显示的文字颜色与表格中文字颜色一致呢? 非常好。学到了谢谢楼主 from openpyxl import load_workbook
# 打开 Excel 文件
workbook = load_workbook('example.xlsx')
# 选择要操作的工作表
sheet = workbook['Sheet1']
# 遍历所有单元格
for row in sheet.iter_rows():
for cell in row:
# 获取单元格文字颜色
font_color = cell.font.color.rgb
# 输出单元格文字颜色
print(f"Cell ({cell.coordinate}) font color: {font_color}")
# 关闭 Excel 文件
workbook.close() 本帖最后由 991547436 于 2023-7-14 17:15 编辑
# 清空Treeview中的数据
for i in treeview.get_children():
treeview.delete(i)
i = 0
# 插入查询结果到Treeview中
for index, row in enumerate(results, start=1):
i = i + 1
treeview.insert('', 'end', values=(str(index),) + row, tags = (i,))
treeview.tag_configure(i, background='green')
只能按照行进行渲染颜色,你可以吧results每一行的结果集末尾加上当前excel读取出的颜色值,暂存,值插入treeview的时候根据颜色值去修改i行的background颜色
这个是你原来row的值,('1', 2, 3, 4, 5, 6),获取的时候可以加颜色比如('1', 2, 3, 4, 5, 6,#red),在treeview.insert的时候把值还原成('1', 2, 3, 4, 5, 6)插入,然后tag_configure的时候把颜色设置为red
我这个是设置背景色,字体颜色不知道有没有,自己百度看看 马铃薯君 发表于 2023-7-14 16:48
from openpyxl import load_workbook
# 打开 Excel 文件
import tkinter as tk
from tkinter import ttk
import openpyxl
from openpyxl import load_workbook
def search_excel(file_path, sheet_name, keyword):
wb = openpyxl.load_workbook(file_path)
sheet = wb
result = []
for row in sheet.iter_rows(values_only=True):
if keyword.lower() in ' '.join(str(cell) for cell in row).lower():
result.append(row)
return result
def perform_search(*args):
file_path = 'data.xlsx'# Excel文件路径
sheet_name = 'Sheet1' # 表格名称
keyword = search_var.get()
results = search_excel(file_path, sheet_name, keyword)
# 清空Treeview中的数据
for i in treeview.get_children():
treeview.delete(i)
# 插入查询结果到Treeview中
for index, row in enumerate(results, start=1):
treeview.insert('', 'end', values=(str(index),) + row)
# 打开 Excel 文件
workbook = load_workbook('data.xlsx')
# 选择要操作的工作表
sheet = workbook['Sheet1']
# 创建GUI窗口
window = tk.Tk()
window.title("Excel查询软件")
# 设置窗口大小为600x300
window.geometry("1000x300")
window_width = 1000
window_height = 300
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
window.geometry(f"{window_width}x{window_height}+{x}+{y}")
# 查询输入框
search_label = tk.Label(window, text="请输入关键字:")
search_label.pack()
search_var = tk.StringVar()
def on_search_change(*args):
perform_search()
# 跟踪输入框的值变化
search_var.trace('w', on_search_change)
search_entry = tk.Entry(window, textvariable=search_var)
search_entry.pack()
# 查询结果显示区域
treeview_frame = ttk.Frame(window)
treeview_frame.pack(side=tk.TOP, fill=tk.BOTH)
treeview_scrollbar = ttk.Scrollbar(treeview_frame)
treeview_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# 设置查询结果显示窗口大小为600x300
treeview = ttk.Treeview(treeview_frame, yscrollcommand=treeview_scrollbar.set, height=10)
treeview.pack(fill=tk.BOTH, expand=True)
treeview_scrollbar.config(command=treeview.yview)
# 定义表格列名和列宽
columns = [('索引列', -200), ('列1', -50), ('列2', 120), ('列3', 140), ('列4', 140)]# 根据实际情况修改列名和列宽
# 设置Treeview列名
treeview['columns'] = for col in columns]
# 设置每列的属性和宽度
treeview.column('#0', width=columns)
for index, (column_name, column_width) in enumerate(columns, start=1):
treeview.column('#' + str(index), width=column_width)
treeview.heading('#' + str(index), text=column_name)
# 运行窗口主循环
window.mainloop()
# 遍历所有单元格
for row in sheet.iter_rows():
for cell in row:
# 获取单元格文字颜色
font_color = cell.font.color.rgb
# 输出单元格文字
没用呢,大佬 感谢分享,只要py的帖子,我都点赞留言! 有没有PY语言大佬帮忙改改啊
页:
[1]