Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import re | |
| st.set_page_config(layout="wide") | |
| st.title('影片放映时间表统计') | |
| # 1. 文件上传组件 | |
| uploaded_file = st.file_uploader("上传“影片放映时间表.xlsx”文件", type=['xlsx']) | |
| ad_duration = st.number_input('输入每个广告的时长(分钟)', min_value=0, value=5) | |
| if uploaded_file is not None: | |
| try: | |
| # 读取Excel文件 | |
| df = pd.read_excel(uploaded_file, header=3) | |
| # 明确将“影片”列转换为字符串类型,以避免混合类型错误 | |
| df['影片'] = df['影片'].astype(str) | |
| st.subheader('上传的原始数据') | |
| st.dataframe(df) | |
| # 2. 数据处理和清洗 | |
| def clean_hall_name(name): | |
| if isinstance(name, str): | |
| match = re.search(r'【(\d+)号', name) | |
| if match: | |
| return f"{match.group(1)}号厅" | |
| return name | |
| df['影厅'] = df['影厅'].apply(clean_hall_name) | |
| df['放映日期'] = pd.to_datetime(df['放映日期']) | |
| df['日期'] = df['放映日期'].dt.strftime('%m月%d日') | |
| df.dropna(subset=['影厅', '片长'], inplace=True) | |
| # 3. 统计 | |
| summary = df.groupby(['日期', '影厅']).agg( | |
| 影片数量=('影片', 'count'), | |
| 影片播放时长=('片长', 'sum') | |
| ).reset_index() | |
| summary['广告时长'] = summary['影片数量'] * ad_duration | |
| # 4. 创建数据透视表 | |
| pivot_table = summary.pivot_table( | |
| index='日期', | |
| columns='影厅', | |
| values=['广告时长', '影片播放时长'] | |
| ).fillna(0).astype(int) | |
| if not pivot_table.empty: | |
| pivot_table = pivot_table.swaplevel(0, 1, axis=1).sort_index(axis=1) | |
| st.subheader('影厅播放统计') | |
| # --- 表格样式优化 --- | |
| # 1. 定义CSS样式 | |
| styles = [ | |
| { | |
| 'selector': 'th.col_heading', # 目标是列标题 | |
| 'props': [ | |
| ('background-color', '#4a4a4a'), # 深色背景 | |
| ('color', 'white'), # 白色字体 | |
| ('text-align', 'center') # 文本居中 | |
| ] | |
| }, | |
| { | |
| 'selector': 'th.row_heading', # 目标是行标题(日期) | |
| 'props': [ | |
| ('text-align', 'center') | |
| ] | |
| } | |
| ] | |
| # 2. 将样式应用到DataFrame | |
| styler = pivot_table.style.set_table_styles(styles) | |
| # 3. 计算表格的动态高度以实现完全展开 | |
| # (行数 + 表头层级数 + 额外空间) * 每行高度 | |
| table_height = (len(pivot_table) + 2 + 1) * 35 | |
| # 4. 使用st.dataframe显示带样式的、完全展开的表格 | |
| st.dataframe(styler, height=table_height) | |
| else: | |
| st.warning("没有可用于生成统计信息的数据。") | |
| except Exception as e: | |
| st.error(f"处理文件时出错: {e}") |