Spaces:
Running
Running
import pandas as pd | |
import streamlit as st | |
import matplotlib.pyplot as plt | |
import matplotlib.font_manager as font_manager | |
from matplotlib.lines import Line2D | |
import io | |
import base64 | |
import os | |
from datetime import datetime, timedelta | |
from pypinyin import lazy_pinyin, Style | |
from matplotlib.backends.backend_pdf import PdfPages | |
import matplotlib.gridspec as gridspec | |
import math | |
# --- Constants for the second script --- | |
SPLIT_TIME = "17:30" | |
BUSINESS_START = "09:30" | |
BUSINESS_END = "01:30" | |
BORDER_COLOR = 'grey' | |
DATE_COLOR = '#A9A9A9' | |
A5_WIDTH_IN = 5.83 | |
A5_HEIGHT_IN = 8.27 | |
NUM_COLS = 3 | |
# --- Functions from the first script --- | |
def get_font(size=14): | |
"""Loads a specific TrueType font, defaulting to a common Chinese font.""" | |
font_path = "simHei.ttc" | |
if not os.path.exists(font_path): | |
font_path = "SimHei.ttf" | |
if os.path.exists(font_path): | |
return font_manager.FontProperties(fname=font_path, size=size) | |
else: | |
st.warning("SimHei font not found. Display may not be correct. Please add simHei.ttc or SimHei.ttf.") | |
return font_manager.FontProperties(family='sans-serif', size=size) | |
def get_pinyin_abbr(text): | |
"""Gets the first letter of the Pinyin for the first two Chinese characters of a text.""" | |
if not text: | |
return "" | |
chars = [c for c in text if '\u4e00' <= c <= '\u9fff'][:2] | |
if not chars: | |
return "" | |
pinyin_list = lazy_pinyin(chars, style=Style.FIRST_LETTER) | |
return ''.join(pinyin_list).upper() | |
def format_seq(n): | |
"""Converts an integer to a circled number string (e.g., 1 -> ①).""" | |
if not isinstance(n, int) or n <= 0: | |
return str(n) | |
circled_chars = "①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳" \ | |
"㉑㉒㉓㉔㉕㉖㉗㉘㉙㉚㉛㉜㉝㉞㉟" \ | |
"㊱㊲㊳㊴㊵㊶㊷㊸㊹㊺㊻㊼㊽㊾㊿" | |
if 1 <= n <= 50: | |
return circled_chars[n - 1] | |
return f'({n})' | |
def process_schedule_led(file): | |
"""Processes '放映时间核对表.xls'.""" | |
try: | |
date_df = pd.read_excel(file, header=None, skiprows=7, nrows=1, usecols=[3]) | |
date_str = pd.to_datetime(date_df.iloc[0, 0]).strftime('%Y-%m-%d') | |
base_date = pd.to_datetime(date_str).date() | |
except Exception: | |
date_str = datetime.today().strftime('%Y-%m-%d') | |
base_date = datetime.today().date() | |
try: | |
df = pd.read_excel(file, header=9, usecols=[1, 2, 4, 5]) | |
df.columns = ['Hall', 'StartTime', 'EndTime', 'Movie'] | |
df['Hall'] = df['Hall'].ffill() | |
df.dropna(subset=['StartTime', 'EndTime', 'Movie'], inplace=True) | |
df['Hall'] = df['Hall'].astype(str).str.extract(r'(\d+号)') | |
df['StartTime_dt'] = pd.to_datetime(df['StartTime'], format='%H:%M', errors='coerce').apply( | |
lambda t: t.replace(year=base_date.year, month=base_date.month, day=base_date.day) if pd.notnull(t) else t | |
) | |
df['EndTime_dt'] = pd.to_datetime(df['EndTime'], format='%H:%M', errors='coerce').apply( | |
lambda t: t.replace(year=base_date.year, month=base_date.month, day=base_date.day) if pd.notnull(t) else t | |
) | |
df.loc[df['EndTime_dt'] < df['StartTime_dt'], 'EndTime_dt'] += timedelta(days=1) | |
df = df.sort_values(['Hall', 'StartTime_dt']) | |
merged_rows = [] | |
for _, group in df.groupby('Hall'): | |
current = None | |
for _, row in group.sort_values('StartTime_dt').iterrows(): | |
if current is None: | |
current = row.copy() | |
elif row['Movie'] == current['Movie']: | |
current['EndTime_dt'] = row['EndTime_dt'] | |
else: | |
merged_rows.append(current) | |
current = row.copy() | |
if current is not None: | |
merged_rows.append(current) | |
merged_df = pd.DataFrame(merged_rows) | |
merged_df['StartTime_dt'] -= timedelta(minutes=10) | |
merged_df['EndTime_dt'] -= timedelta(minutes=5) | |
merged_df['Seq'] = merged_df.groupby('Hall').cumcount() + 1 | |
merged_df['StartTime_str'] = merged_df['StartTime_dt'].dt.strftime('%H:%M') | |
merged_df['EndTime_str'] = merged_df['EndTime_dt'].dt.strftime('%H:%M') | |
return merged_df[['Hall', 'Seq', 'Movie', 'StartTime_str', 'EndTime_str']], date_str | |
except Exception as e: | |
st.error(f"Error processing schedule data: {e}. Please check the file format.") | |
return None, date_str | |
def create_print_layout_led(data, date_str): | |
"""Generates print layouts for '放映时间核对表.xls'.""" | |
if data is None or data.empty: | |
return None | |
A4_width_in, A4_height_in = 8.27, 11.69 | |
dpi = 300 | |
total_content_rows = len(data) | |
totalA = total_content_rows + 2 | |
row_height = A4_height_in / totalA | |
data = data.reset_index(drop=True) | |
data['hall_str'] = '$' + data['Hall'].str.replace('号', '') + '^{\#}$' | |
data['seq_str'] = data['Seq'].apply(format_seq) | |
data['pinyin_abbr'] = data['Movie'].apply(get_pinyin_abbr) | |
data['time_str'] = data['StartTime_str'] + ' - ' + data['EndTime_str'] | |
temp_fig = plt.figure(figsize=(A4_width_in, A4_height_in), dpi=dpi) | |
renderer = temp_fig.canvas.get_renderer() | |
base_font_size_pt = (row_height * 0.9) * 72 | |
seq_font_size_pt = (row_height * 0.5) * 72 | |
def get_col_width_in(series, font_size_pt, is_math=False): | |
if series.empty: | |
return 0 | |
font_prop = get_font(font_size_pt) | |
longest_str_idx = series.astype(str).str.len().idxmax() | |
max_content = str(series.loc[longest_str_idx]) | |
text_width_px, _, _ = renderer.get_text_width_height_descent(max_content, font_prop, ismath=is_math) | |
return (text_width_px / dpi) * 1.1 | |
margin_col_width = row_height | |
hall_col_width = get_col_width_in(data['hall_str'], base_font_size_pt, is_math=True) | |
seq_col_width = get_col_width_in(data['seq_str'], seq_font_size_pt) | |
pinyin_col_width = get_col_width_in(data['pinyin_abbr'], base_font_size_pt) | |
time_col_width = get_col_width_in(data['time_str'], base_font_size_pt) | |
movie_col_width = A4_width_in - ( | |
margin_col_width * 2 + hall_col_width + seq_col_width + pinyin_col_width + time_col_width) | |
plt.close(temp_fig) | |
col_widths = {'hall': hall_col_width, 'seq': seq_col_width, 'movie': movie_col_width, 'pinyin': pinyin_col_width, | |
'time': time_col_width} | |
col_x_starts = {} | |
current_x = margin_col_width | |
for col_name in ['hall', 'seq', 'movie', 'pinyin', 'time']: | |
col_x_starts[col_name] = current_x | |
current_x += col_widths[col_name] | |
def draw_figure(fig, ax): | |
renderer = fig.canvas.get_renderer() | |
for col_name in ['hall', 'seq', 'movie', 'pinyin']: | |
x_line = col_x_starts[col_name] + col_widths[col_name] | |
line_top_y, line_bottom_y = A4_height_in - row_height, row_height | |
ax.add_line( | |
Line2D([x_line, x_line], [line_bottom_y, line_top_y], color='gray', linestyle=':', linewidth=0.5)) | |
last_hall_drawn = None | |
for i, row in data.iterrows(): | |
y_bottom = A4_height_in - (i + 2) * row_height | |
y_center = y_bottom + row_height / 2 | |
if row['Hall'] != last_hall_drawn: | |
ax.text(col_x_starts['hall'] + col_widths['hall'] / 2, y_center, row['hall_str'], | |
fontproperties=get_font(base_font_size_pt), ha='center', va='center') | |
last_hall_drawn = row['Hall'] | |
ax.text(col_x_starts['seq'] + col_widths['seq'] / 2, y_center, row['seq_str'], | |
fontproperties=get_font(seq_font_size_pt), ha='center', va='center') | |
ax.text(col_x_starts['pinyin'] + col_widths['pinyin'] / 2, y_center, row['pinyin_abbr'], | |
fontproperties=get_font(base_font_size_pt), ha='center', va='center') | |
ax.text(col_x_starts['time'] + col_widths['time'] / 2, y_center, row['time_str'], | |
fontproperties=get_font(base_font_size_pt), ha='center', va='center') | |
movie_font_size = base_font_size_pt | |
movie_font_prop = get_font(movie_font_size) | |
text_w_px, _, _ = renderer.get_text_width_height_descent(row['Movie'], movie_font_prop, ismath=False) | |
text_w_in = text_w_px / dpi | |
max_width_in = col_widths['movie'] * 0.9 | |
if text_w_in > max_width_in: | |
movie_font_size *= (max_width_in / text_w_in) | |
movie_font_prop = get_font(movie_font_size) | |
ax.text(col_x_starts['movie'] + 0.05, y_center, row['Movie'], | |
fontproperties=movie_font_prop, ha='left', va='center') | |
is_last_in_hall = (i == len(data) - 1) or (row['Hall'] != data.loc[i + 1, 'Hall']) | |
line_start_x = margin_col_width | |
line_end_x = A4_width_in - margin_col_width | |
if is_last_in_hall: | |
ax.add_line(Line2D([line_start_x, line_end_x], [y_bottom, y_bottom], color='black', linestyle='-', | |
linewidth=0.8)) | |
else: | |
ax.add_line(Line2D([line_start_x, line_end_x], [y_bottom, y_bottom], color='gray', linestyle=':', | |
linewidth=0.5)) | |
outputs = {} | |
for format_type in ['png', 'pdf']: | |
fig = plt.figure(figsize=(A4_width_in, A4_height_in), dpi=dpi) | |
ax = fig.add_axes([0, 0, 1, 1]) | |
ax.set_axis_off() | |
ax.set_xlim(0, A4_width_in) | |
ax.set_ylim(0, A4_height_in) | |
ax.text(margin_col_width, A4_height_in - (row_height / 2), date_str, | |
fontproperties=get_font(10), color='#A9A9A9', ha='left', va='center') | |
draw_figure(fig, ax) | |
buf = io.BytesIO() | |
fig.savefig(buf, format=format_type, dpi=dpi, bbox_inches='tight', pad_inches=0) | |
buf.seek(0) | |
data_uri = base64.b64encode(buf.getvalue()).decode() | |
mime_type = 'image/png' if format_type == 'png' else 'application/pdf' | |
outputs[format_type] = f"data:{mime_type};base64,{data_uri}" | |
plt.close(fig) | |
return outputs | |
# --- Functions from the second script --- | |
def process_schedule_times(file): | |
"""Processes '放映场次核对表.xls'.""" | |
try: | |
df = pd.read_excel(file, skiprows=8) | |
df = df.iloc[:, [6, 7, 9]] | |
df.columns = ['Hall', 'StartTime', 'EndTime'] | |
df = df.dropna(subset=['Hall', 'StartTime', 'EndTime']) | |
df['Hall'] = df['Hall'].str.extract(r'(\d+)号').astype(str) + ' ' | |
df['original_end'] = df['EndTime'] | |
base_date = datetime.today().date() | |
df['StartTime'] = pd.to_datetime(df['StartTime']) | |
df['EndTime'] = pd.to_datetime(df['EndTime']) | |
business_start = datetime.strptime(f"{base_date} {BUSINESS_START}", "%Y-%m-%d %H:%M") | |
business_end = datetime.strptime(f"{base_date} {BUSINESS_END}", "%Y-%m-%d %H:%M") | |
if business_end < business_start: | |
business_end += timedelta(days=1) | |
for idx, row in df.iterrows(): | |
end_time = row['EndTime'] | |
if end_time.hour < 9: | |
df.at[idx, 'EndTime'] = end_time + timedelta(days=1) | |
if row['StartTime'].hour >= 21 and end_time.hour < 9: | |
df.at[idx, 'EndTime'] = end_time + timedelta(days=1) | |
df['time_for_comparison'] = df['EndTime'].apply( | |
lambda x: datetime.combine(base_date, x.time()) | |
) | |
df.loc[df['time_for_comparison'].dt.hour < 9, 'time_for_comparison'] += timedelta(days=1) | |
valid_times = ( | |
((df['time_for_comparison'] >= datetime.combine(base_date, business_start.time())) & | |
(df['time_for_comparison'] <= datetime.combine(base_date + timedelta(days=1), business_end.time()))) | |
) | |
df = df[valid_times] | |
df = df.sort_values('EndTime') | |
split_time = datetime.strptime(f"{base_date} {SPLIT_TIME}", "%Y-%m-%d %H:%M") | |
split_time_for_comparison = df['time_for_comparison'].apply( | |
lambda x: datetime.combine(base_date, split_time.time()) | |
) | |
part1 = df[df['time_for_comparison'] <= split_time_for_comparison].copy() | |
part2 = df[df['time_for_comparison'] > split_time_for_comparison].copy() | |
for part in [part1, part2]: | |
part['EndTime'] = part['EndTime'].dt.strftime('%-H:%M') | |
date_df = pd.read_excel( | |
file, | |
skiprows=5, | |
nrows=1, | |
usecols=[2], | |
header=None | |
) | |
date_cell = date_df.iloc[0, 0] | |
try: | |
if isinstance(date_cell, str): | |
date_str = datetime.strptime(date_cell, '%Y-%m-%d').strftime('%Y-%m-%d') | |
else: | |
date_str = pd.to_datetime(date_cell).strftime('%Y-%m-%d') | |
except: | |
date_str = datetime.today().strftime('%Y-%m-%d') | |
return part1[['Hall', 'EndTime']], part2[['Hall', 'EndTime']], date_str | |
except Exception as e: | |
st.error(f"处理文件时出错: {str(e)}") | |
return None, None, None | |
def create_print_layout_times(data, title, date_str): | |
"""Creates print layouts for '放映场次核对表.xls'.""" | |
if data.empty: | |
return None | |
def generate_figure(): | |
total_items = len(data) | |
num_rows = math.ceil(total_items / NUM_COLS) if total_items > 0 else 1 | |
date_header_height_in = 0.3 | |
data_area_height_in = A5_HEIGHT_IN - date_header_height_in | |
cell_width_in = A5_WIDTH_IN / NUM_COLS | |
cell_height_in = data_area_height_in / num_rows | |
cell_width_pt = cell_width_in * 72 | |
cell_height_pt = cell_height_in * 72 | |
target_text_width_pt = cell_width_pt * 0.9 | |
fontsize_from_width = target_text_width_pt / (8 * 0.6) | |
fontsize_from_height = cell_height_pt * 0.8 | |
base_fontsize = min(fontsize_from_width, fontsize_from_height) | |
fig = plt.figure(figsize=(A5_WIDTH_IN, A5_HEIGHT_IN), dpi=300) | |
fig.subplots_adjust(left=0, right=1, top=1, bottom=0) | |
plt.rcParams['font.family'] = 'sans-serif' | |
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] | |
gs = gridspec.GridSpec( | |
num_rows + 1, NUM_COLS, | |
hspace=0, wspace=0, | |
height_ratios=[date_header_height_in] + [cell_height_in] * num_rows, | |
figure=fig | |
) | |
data_values = data.values.tolist() | |
while len(data_values) % NUM_COLS != 0: | |
data_values.append(['', '']) | |
rows_per_col_layout = math.ceil(len(data_values) / NUM_COLS) | |
sorted_data = [['', '']] * len(data_values) | |
for i, item in enumerate(data_values): | |
if item[0] and item[1]: | |
row_in_col = i % rows_per_col_layout | |
col_idx = i // rows_per_col_layout | |
new_index = row_in_col * NUM_COLS + col_idx | |
if new_index < len(sorted_data): | |
sorted_data[new_index] = item | |
for idx, (hall, end_time) in enumerate(sorted_data): | |
if hall and end_time: | |
row_grid = idx // NUM_COLS + 1 | |
col_grid = idx % NUM_COLS | |
ax = fig.add_subplot(gs[row_grid, col_grid]) | |
for spine in ax.spines.values(): | |
spine.set_visible(True) | |
spine.set_linestyle((0, (1, 2))) | |
spine.set_color(BORDER_COLOR) | |
spine.set_linewidth(0.75) | |
display_text = f"{hall}{end_time}" | |
ax.text(0.5, 0.5, display_text, | |
fontsize=base_fontsize, | |
fontweight='bold', | |
ha='center', va='center', | |
transform=ax.transAxes) | |
ax.set_xticks([]) | |
ax.set_yticks([]) | |
ax.set_facecolor('none') | |
ax_date = fig.add_subplot(gs[0, :]) | |
ax_date.text(0.01, 0.5, f"{date_str} {title}", | |
fontsize=base_fontsize * 0.5, | |
color=DATE_COLOR, fontweight='bold', | |
ha='left', va='center', | |
transform=ax_date.transAxes) | |
ax_date.set_axis_off() | |
ax_date.set_facecolor('none') | |
return fig | |
fig_for_output = generate_figure() | |
png_buffer = io.BytesIO() | |
fig_for_output.savefig(png_buffer, format='png') | |
png_buffer.seek(0) | |
png_base64 = base64.b64encode(png_buffer.getvalue()).decode() | |
pdf_buffer = io.BytesIO() | |
with PdfPages(pdf_buffer) as pdf: | |
pdf.savefig(fig_for_output) | |
pdf_buffer.seek(0) | |
pdf_base64 = base64.b64encode(pdf_buffer.getvalue()).decode() | |
plt.close(fig_for_output) | |
return { | |
'png': f'data:image/png;base64,{png_base64}', | |
'pdf': f'data:application/pdf;base64,{pdf_base64}' | |
} | |
def display_pdf(base64_pdf): | |
"""Generates the HTML to embed a PDF in Streamlit.""" | |
return f'<iframe src="{base64_pdf}" width="100%" height="800" type="application/pdf"></iframe>' | |
# --- Streamlit App --- | |
st.set_page_config(page_title="Schedule Printer", layout="wide") | |
st.title("Schedule Printer") | |
uploaded_file = st.file_uploader("Select '放映时间核对表.xls' or '放映场次核对表.xls'", | |
type=["xls", "xlsx"]) | |
if uploaded_file: | |
if "时间" in uploaded_file.name: | |
st.header("LED Screen Schedule") | |
with st.spinner("Processing file, please wait..."): | |
schedule, date_str = process_schedule_led(uploaded_file) | |
if schedule is not None and not schedule.empty: | |
output = create_print_layout_led(schedule, date_str) | |
tab1, tab2 = st.tabs(["PDF Preview", "PNG Preview"]) | |
with tab1: | |
st.markdown(display_pdf(output['pdf']), unsafe_allow_html=True) | |
with tab2: | |
st.image(output['png'], use_container_width=True) | |
else: | |
st.error("Could not process the file. Please check if the file format and content are correct.") | |
elif "场次" in uploaded_file.name: | |
st.header("Screening Times Quick Print") | |
part1, part2, date_str = process_schedule_times(uploaded_file) | |
if part1 is not None and part2 is not None: | |
part1_output = create_print_layout_times(part1, "A", date_str) | |
part2_output = create_print_layout_times(part2, "C", date_str) | |
col1, col2 = st.columns(2) | |
with col1: | |
st.subheader("Day Shift (End time <= 17:30)") | |
if part1_output: | |
tab1_1, tab1_2 = st.tabs(["PDF Preview", "PNG Preview"]) | |
with tab1_1: | |
st.markdown(display_pdf(part1_output['pdf']), unsafe_allow_html=True) | |
with tab1_2: | |
st.image(part1_output['png']) | |
else: | |
st.info("No data for the day shift.") | |
with col2: | |
st.subheader("Night Shift (End time > 17:30)") | |
if part2_output: | |
tab2_1, tab2_2 = st.tabs(["PDF Preview", "PNG Preview"]) | |
with tab2_1: | |
st.markdown(display_pdf(part2_output['pdf']), unsafe_allow_html=True) | |
with tab2_2: | |
st.image(part2_output['png']) | |
else: | |
st.info("No data for the night shift.") | |