How to use the python programming language to display the specified two excel changes in a web page?Similar to this picture enter image description here
import streamlit as st
import pandas as pd
# 创建页面布局
st.set_page_config(page_title="fgtest", layout="wide", page_icon="test_icon")
# 读取 Excel 表格数据
df1 = pd.read_excel(r"E:panda_gfciEffectTable1.xlsx")
df2 = pd.read_excel(r"E:panda_gfciEffectTable2.xlsx")
# 获取两个 DataFrame 的所有列名并取并集
all_columns = set(df1.columns).union(df2.columns)
# 将两个 DataFrame 对齐并用 None 填充缺失值
df1 = df1.reindex(columns=all_columns, fill_value=None)
df2 = df2.reindex(columns=all_columns, fill_value=None)
# 获取两个 DataFrame 的所有行索引并取并集
all_index = set(df1.index).union(df2.index)
# 将两个 DataFrame 的行对齐并用 None 填充缺失值
df1 = df1.reindex(index=all_index, fill_value=None)
df2 = df2.reindex(index=all_index, fill_value=None)
# 创建一个函数来标识两个 DataFrame 不一致的地方
def highlight_diff(val1, val2):
if val1 != val2 and not (pd.isnull(val1) and pd.isnull(val2)):
return 'background-color: #ff6666' # 红色
else:
return ''
# 创建一个函数来应用样式
def apply_style_to_row(row, df):
return [highlight_diff(row[name], df.loc[row.name, name]) for name in row.index]
def apply_style_to_df(df, other_df):
return df.style.apply(apply_style_to_row, df=other_df, axis=1)
# 显示两个 DataFrame,并应用样式
df1_html = apply_style_to_df(df1, df2)
df2_html = apply_style_to_df(df2, df1)
col1, col2 = st.columns(2)
col1.subheader("提交前")
col1.write(df1_html, unsafe_allow_html=True)
col2.subheader("提交后")
col2.write(df2_html, unsafe_allow_html=True)
I tried to implement the requirement this way。There seems to be a lack of effective algorithm code。For example:Myers Diff?
New contributor
方刚.Ky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1