File size: 10,669 Bytes
70694c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import pandas as pd
import streamlit as st
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
import io
import base64
import os
from datetime import datetime, timedelta
import math
from matplotlib.patches import FancyBboxPatch
from pypinyin import lazy_pinyin, Style
from matplotlib.backends.backend_pdf import PdfPages

def get_font(size=14):
    """Loads a specific font file, falling back to a common name if not found."""
    font_path = "simHei.ttc"
    if not os.path.exists(font_path):
        font_path = "SimHei.ttf"
    return font_manager.FontProperties(fname=font_path, 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']
    chars = chars[:2]
    # 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."""
    try:
        # Attempt to read the date from a specific 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 the current date if reading fails
        date_str = datetime.today().strftime('%Y-%m-%d')
        base_date = datetime.today().date()
    
    try:
        # Read the main schedule data
        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 time strings 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 hall, group in df.groupby('Hall'):
            group = group.sort_values('StartTime_dt')
            current = None
            for _, row in group.iterrows():
                if current is None:
                    current = row.copy()
                else:
                    if 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 requirement
        merged_df['StartTime_dt'] = merged_df['StartTime_dt'] - timedelta(minutes=10)
        merged_df['EndTime_dt'] = merged_df['EndTime_dt'] - timedelta(minutes=5)
        
        # Format times back to strings for display
        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', 'Movie', 'StartTime_str', 'EndTime_str']], date_str
    except Exception:
        # Return None if any processing error occurs
        return None, date_str

def create_print_layout(data, date_str):
    """Creates PNG and PDF layouts of the schedule, maximizing content size."""
    if data is None or data.empty:
        return None

    # --- Figure Setup ---
    # A4 paper size in inches, 300 dpi for good quality
    fig_width, fig_height = 8.27, 11.69
    dpi = 300

    # Create separate figures for PNG and PDF
    png_fig = plt.figure(figsize=(fig_width, fig_height), dpi=dpi)
    png_ax = png_fig.add_subplot(111)
    png_ax.set_axis_off()
    
    pdf_fig = plt.figure(figsize=(fig_width, fig_height), dpi=dpi)
    pdf_ax = pdf_fig.add_subplot(111)
    pdf_ax.set_axis_off()

    # Set tight margins to maximize the content area on the page.
    margin = 0.03
    png_fig.subplots_adjust(left=margin, right=1-margin, top=1-margin, bottom=margin)
    pdf_fig.subplots_adjust(left=margin, right=1-margin, top=1-margin, bottom=margin)

    # --- Drawing Logic ---
    def process_figure(fig, ax):
        # --- Dynamic Sizing ---
        halls = sorted(data['Hall'].unique(), key=lambda h: int(h.replace('号','')) if h else 0)
        
        # Calculate total lines needed: one for each movie + a gap between each hall's schedule
        num_movie_lines = len(data)
        num_hall_breaks = len(halls) - 1
        total_lines = num_movie_lines + num_hall_breaks
        if total_lines == 0: return

        # Calculate the height available for content based on margins
        available_height = 1 - (2 * margin)
        # Calculate the height for each line to perfectly fill the page
        line_height = available_height / total_lines
        
        # Dynamically set font size based on line height.
        # This makes the text as large as possible while still fitting.
        # (72 points per inch, 0.65 is a factor for vertical padding)
        font_size_pt = line_height * fig_height * 72 * 0.65
        hall_font = get_font(font_size_pt)
        movie_font = get_font(font_size_pt)
        date_font = get_font(font_size_pt * 0.7)

        # --- Content Drawing ---
        ax.text(0.01, 0.99, date_str, 
                fontsize=date_font.get_size(), color='#A9A9A9',
                ha='left', va='top', fontproperties=date_font,
                transform=ax.transAxes, zorder=2)
        
        y_position = 0.99 # Start drawing from the top
        
        for hall in halls:
            hall_data = data[data['Hall'] == hall].sort_values('StartTime_str')
            y_block_top = y_position
            hall_num_str = hall.replace("号", "")
            hall_display_text = f"${hall_num_str}^{{\\#}}$"
            
            # Center text vertically within its line height for better appearance
            text_v_align_offset = line_height * 0.5
            
            for i, (_, row) in enumerate(hall_data.iterrows()):
                if i == 0:
                    ax.text(0.05, y_position - text_v_align_offset, hall_display_text,
                            fontproperties=hall_font, fontweight='bold',
                            ha='center', va='center', transform=ax.transAxes, zorder=2)
                
                pinyin_abbr = get_pinyin_abbr(row['Movie'])
                movie_display_text = f"{i+1}. {pinyin_abbr} {row['Movie']}"
                ax.text(0.15, y_position - text_v_align_offset, movie_display_text,
                        fontproperties=movie_font, ha='left', va='center',
                        transform=ax.transAxes, zorder=2, clip_on=True)
                
                time_display_text = f"{row['StartTime_str']} - {row['EndTime_str']}"
                ax.text(0.99, y_position - text_v_align_offset, time_display_text,
                        fontproperties=movie_font, ha='right', va='center',
                        transform=ax.transAxes, zorder=2)
                
                y_position -= line_height

            # Add a decorative rounded box around each hall's schedule
            y_block_bottom = y_position + line_height
            rect_height = y_block_top - y_block_bottom
            
            rect = FancyBboxPatch((0, y_block_bottom), 1, rect_height,
                                  boxstyle="round,pad=0.01,rounding_size=0.02",
                                  edgecolor='gray', facecolor='white',
                                  linewidth=0.8, zorder=1, transform=ax.transAxes)
            ax.add_patch(rect)
            
            if hall != halls[-1]:
                y_position -= line_height # Add gap for the next block

    # Render the layout on both figures
    process_figure(png_fig, png_ax)
    process_figure(pdf_fig, pdf_ax)
    
    # --- File Saving ---
    # Save PNG to a memory buffer
    png_buffer = io.BytesIO()
    png_fig.savefig(png_buffer, format='png', bbox_inches='tight', pad_inches=0.05)
    png_buffer.seek(0)
    image_base64 = base64.b64encode(png_buffer.getvalue()).decode()
    plt.close(png_fig)
    
    # Save PDF to a memory buffer
    pdf_buffer = io.BytesIO()
    with PdfPages(pdf_buffer) as pdf:
        pdf.savefig(pdf_fig, bbox_inches='tight', pad_inches=0.05)
    pdf_buffer.seek(0)
    pdf_base64 = base64.b64encode(pdf_buffer.getvalue()).decode()
    plt.close(pdf_fig)
    
    return {
        'png': f"data:image/png;base64,{image_base64}",
        'pdf': f"data:application/pdf;base64,{pdf_base64}"
    }

def display_pdf(base64_pdf):
    """Generates the HTML to embed and display 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 屏幕时间表打印", layout="wide")
st.title("LED 屏幕时间表打印")

uploaded_file = st.file_uploader("选择打开【放映时间核对表.xls】文件", accept_multiple_files=False, type=["xls"])

if uploaded_file:
    with st.spinner("文件正在处理中,请稍候..."):
        schedule, date_str = process_schedule(uploaded_file)
        if schedule is not None:
            output = create_print_layout(schedule, date_str)
            
            # Use tabs for PDF and PNG previews
            tab1, tab2 = st.tabs(["PDF 预览", "PNG 预览"])
            
            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("无法处理文件,请检查文件格式或内容是否正确。")