LED-print / app.py
Ethscriptions's picture
Update app.py
9a4c156 verified
raw
history blame
13.4 kB
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
def get_font(size=14):
"""Loads a specific TrueType font, defaulting to a common Chinese font."""
# Prioritize the .ttc file if it exists
font_path = "simHei.ttc"
if not os.path.exists(font_path):
# Fallback to the .ttf file
font_path = "SimHei.ttf"
# If neither exists, matplotlib will use its default font.
# For best results, ensure one of these fonts is in the same directory as the script.
if os.path.exists(font_path):
return font_manager.FontProperties(fname=font_path, size=size)
else:
# Fallback to a generic font if SimHei is not found
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 ""
# Extract the first two Chinese characters
chars = [c for c in text if '\u4e00' <= c <= '\u9fff'][:2]
if not chars:
return ""
# Get the first letter of the pinyin for each character
pinyin_list = lazy_pinyin(chars, style=Style.FIRST_LETTER)
return ''.join(pinyin_list).upper()
def process_schedule(file):
"""
Processes the uploaded Excel file to extract and clean the movie schedule data.
Adds a sequence number for movies within each hall.
"""
try:
# Try to read the date from the specified cell
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:
# Fallback to today's date if reading fails
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']
# Clean and format the data
df['Hall'] = df['Hall'].ffill()
df.dropna(subset=['StartTime', 'EndTime', 'Movie'], inplace=True)
df['Hall'] = df['Hall'].astype(str).str.extract(r'(\d+号)')
# Convert times to datetime objects
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
)
# Handle overnight screenings
df.loc[df['EndTime_dt'] < df['StartTime_dt'], 'EndTime_dt'] += timedelta(days=1)
df = df.sort_values(['Hall', 'StartTime_dt'])
# Merge consecutive screenings of the same movie
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'] # Extend the end time
else:
merged_rows.append(current)
current = row.copy()
if current is not None:
merged_rows.append(current)
merged_df = pd.DataFrame(merged_rows)
# Adjust times as per original logic
merged_df['StartTime_dt'] -= timedelta(minutes=10)
merged_df['EndTime_dt'] -= timedelta(minutes=5)
# Add a sequence number for each movie within its hall
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(data, date_str):
"""
Generates PNG and PDF print layouts for the movie schedule based on a dynamic grid.
"""
if data is None or data.empty:
return None
# --- 1. Layout and Column Calculation ---
A4_width_in, A4_height_in = 8.27, 11.69 # A4 size in inches
dpi = 300
# Calculate total rows and row height based on A4 size
total_content_rows = len(data)
totalA = total_content_rows + 2 # Add 2 for top/bottom margin rows
row_height = A4_height_in / totalA
# Prepare data strings for width calculation
data = data.reset_index(drop=True)
data['hall_str'] = data['Hall'].str.replace('号', '') + '#'
data['seq_str'] = data['Seq'].astype(str) + '.'
data['pinyin_abbr'] = data['Movie'].apply(get_pinyin_abbr)
data['time_str'] = data['StartTime_str'] + ' - ' + data['EndTime_str']
# Create a temporary figure to access the renderer for text width calculation
temp_fig = plt.figure(figsize=(A4_width_in, A4_height_in), dpi=dpi)
renderer = temp_fig.canvas.get_renderer()
# Base font size is 90% of the row height (converted from inches to points)
base_font_size_pt = (row_height * 0.9) * 72
def get_col_width_in(series, font_size_pt):
"""Calculates the required width for a column in inches."""
if series.empty:
return 0
font_prop = get_font(font_size_pt)
# Find the string with the maximum visual length in the series
longest_str_idx = series.astype(str).str.len().idxmax()
max_content = str(series.loc[longest_str_idx])
# Get width in pixels from the renderer
text_width_px, _, _ = renderer.get_text_width_height_descent(max_content, font_prop, ismath=False)
# Convert to inches and add a 10% buffer
return (text_width_px / dpi) * 1.1
# Calculate widths for fixed-content columns
margin_col_width = row_height # Left/right margins are as wide as the rows are high
hall_col_width = get_col_width_in(data['hall_str'], base_font_size_pt)
seq_col_width = get_col_width_in(data['seq_str'], base_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)
# The movie title column takes the remaining width
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) # Close the temporary figure
# Store column widths and their horizontal starting positions
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]
# --- 2. Drawing ---
def draw_figure(fig, ax):
"""The common drawing function to be applied to both PNG and PDF figures."""
# Get a renderer from the actual figure we are drawing on
renderer = fig.canvas.get_renderer()
# Draw vertical dotted grid lines between columns
for col_name in ['hall', 'seq', 'movie', 'pinyin']:
x_line = col_x_starts[col_name] + col_widths[col_name]
line_top_y = A4_height_in - row_height
line_bottom_y = row_height
ax.add_line(Line2D([x_line, x_line], [line_bottom_y, line_top_y], color='gray', linestyle=':', linewidth=0.5))
# --- Draw Content and Horizontal Lines for each row ---
last_hall_drawn = None
for i, row in data.iterrows():
y_bottom = A4_height_in - (i + 2) * row_height # Y-coordinate of the row's bottom line
y_center = y_bottom + row_height / 2 # Y-coordinate of the row's vertical center
# --- Draw Cell Content ---
# Hall Number (only for the first row of each hall)
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']
# Sequence Number
ax.text(col_x_starts['seq'] + col_widths['seq'] / 2, y_center, row['seq_str'],
fontproperties=get_font(base_font_size_pt), ha='center', va='center')
# Pinyin Abbreviation
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')
# Time String
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 Title (with special font scaling to fit)
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 # Target 90% of cell width
if text_w_in > max_width_in:
# If text is too wide, reduce font size proportionally
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'], # Left-aligned with padding
fontproperties=movie_font_prop, ha='left', va='center')
# --- Draw Horizontal Lines ---
is_last_in_hall = (i == len(data) - 1) or (row['Hall'] != data.loc[i + 1, 'Hall'])
if is_last_in_hall:
# Draw a solid black line to separate halls
line_start_x = margin_col_width
line_end_x = A4_width_in - margin_col_width
ax.add_line(Line2D([line_start_x, line_end_x], [y_bottom, y_bottom], color='black', linestyle='-', linewidth=0.8))
else:
# Draw a dotted gray line for rows within a hall
ax.add_line(Line2D([margin_col_width, A4_width_in - margin_col_width], [y_bottom, y_bottom], color='gray', linestyle=':', linewidth=0.5))
# --- 3. Setup Figures and Generate Output ---
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]) # Use the whole figure area for drawing
ax.set_axis_off()
ax.set_xlim(0, A4_width_in)
ax.set_ylim(0, A4_height_in)
# Add date string to the top margin area
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
def display_pdf(base64_pdf):
"""Generates the HTML to embed a PDF in Streamlit."""
pdf_display = f'<iframe src="{base64_pdf}" width="100%" height="800" type="application/pdf"></iframe>'
return pdf_display
# --- Streamlit App ---
st.set_page_config(page_title="LED Screen Schedule Printer", layout="wide")
st.title("LED Screen Schedule Printer")
uploaded_file = st.file_uploader("Select the '放映时间核对表.xls' file", accept_multiple_files=False, type=["xls", "xlsx"])
if uploaded_file:
with st.spinner("Processing file, please wait..."):
schedule, date_str = process_schedule(uploaded_file)
if schedule is not None and not schedule.empty:
output = create_print_layout(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.")