Ethscriptions commited on
Commit
c1f32de
·
verified ·
1 Parent(s): 38e5d00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -28
app.py CHANGED
@@ -6,8 +6,8 @@ import io
6
  import base64
7
  import matplotlib.gridspec as gridspec
8
  import math
9
- import matplotlib.font_manager as fm
10
 
 
11
  SPLIT_TIME = "17:30"
12
  BUSINESS_START = "09:30"
13
  BUSINESS_END = "01:30"
@@ -27,7 +27,7 @@ def process_schedule(file):
27
  df = df.dropna(subset=['Hall', 'StartTime', 'EndTime'])
28
 
29
  # 转换影厅格式为 "#号" 格式
30
- df['Hall'] = df['Hall'].str.extract(r'(\d+)号').astype(str) + '#'
31
 
32
  # 保存原始时间字符串用于诊断
33
  df['original_end'] = df['EndTime']
@@ -89,25 +89,28 @@ def process_schedule(file):
89
  for part in [part1, part2]:
90
  part['EndTime'] = part['EndTime'].dt.strftime('%-I:%M')
91
 
92
- return part1[['Hall', 'EndTime']], part2[['Hall', 'EndTime']]
 
 
 
 
93
 
94
  except Exception as e:
95
  st.error(f"处理文件时出错: {str(e)}")
96
- return None, None
97
 
98
- def create_print_layout(data, title, date):
99
  """创建打印布局"""
100
  if data.empty:
101
  return None
102
-
103
- # 设置中文字体
104
- plt.rcParams['font.family'] = 'Microsoft YaHei'
105
- plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
106
- plt.rcParams['axes.unicode_minus'] = False
107
-
108
  # 设置 A5 纸张竖向尺寸
109
  fig = plt.figure(figsize=(5.83, 8.27), dpi=300)
110
- plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05)
 
 
 
 
111
 
112
  # 计算行数和总数
113
  total_items = len(data)
@@ -115,7 +118,7 @@ def create_print_layout(data, title, date):
115
  num_rows = math.ceil(total_items / num_cols)
116
 
117
  # 创建网格
118
- gs = gridspec.GridSpec(num_rows, num_cols, hspace=0.1, wspace=0.1)
119
 
120
  # 计算字体大小
121
  base_fontsize = min(45, 265 / num_rows)
@@ -173,16 +176,22 @@ def create_print_layout(data, title, date):
173
  ax.set_yticks([])
174
 
175
  # 在底部添加日期和班次信息
176
- plt.figtext(0.5, 0.02,
177
- f"{date} {'白班' if title == '第一部分' else '夜班'}",
178
- ha='center',
179
- fontsize=10,
180
- fontweight='bold',
181
- fontfamily='Microsoft YaHei')
 
 
 
 
 
 
182
 
183
  # 转换为图片
184
  buffer = io.BytesIO()
185
- plt.savefig(buffer, format='png', bbox_inches='tight', pad_inches=0.1, dpi=300)
186
  buffer.seek(0)
187
  image_base64 = base64.b64encode(buffer.getvalue()).decode()
188
  plt.close()
@@ -196,16 +205,12 @@ st.title("散厅时间快捷打印")
196
  uploaded_file = st.file_uploader("上传【放映场次核对表.xls】文件", type=["xls", "xlsx"])
197
 
198
  if uploaded_file:
199
- # 读取日期
200
- date_df = pd.read_excel(uploaded_file, nrows=6)
201
- date_str = date_df.iloc[5, 2] # C6 单元格的日期
202
-
203
- part1, part2 = process_schedule(uploaded_file)
204
 
205
  if part1 is not None and part2 is not None:
206
- # 创建打印布局,增加日期参数
207
- part1_image = create_print_layout(part1, "第一部分", date_str)
208
- part2_image = create_print_layout(part2, "第二部分", date_str)
209
 
210
  # 显示预览
211
  col1, col2 = st.columns(2)
 
6
  import base64
7
  import matplotlib.gridspec as gridspec
8
  import math
 
9
 
10
+ # [前面的常量定义和 process_schedule 函数保持不变]
11
  SPLIT_TIME = "17:30"
12
  BUSINESS_START = "09:30"
13
  BUSINESS_END = "01:30"
 
27
  df = df.dropna(subset=['Hall', 'StartTime', 'EndTime'])
28
 
29
  # 转换影厅格式为 "#号" 格式
30
+ df['Hall'] = df['Hall'].str.extract(r'(\d+)号').astype(str) + ' '
31
 
32
  # 保存原始时间字符串用于诊断
33
  df['original_end'] = df['EndTime']
 
89
  for part in [part1, part2]:
90
  part['EndTime'] = part['EndTime'].dt.strftime('%-I:%M')
91
 
92
+ # 提取日期
93
+ date_df = pd.read_excel(file, skiprows=5, nrows=1, usecols="C")
94
+ date_str = date_df.iloc[0, 0].strftime('%Y-%m-%d') if not pd.isna(date_df.iloc[0, 0]) else datetime.today().strftime('%Y-%m-%d')
95
+
96
+ return part1[['Hall', 'EndTime']], part2[['Hall', 'EndTime']], date_str
97
 
98
  except Exception as e:
99
  st.error(f"处理文件时出错: {str(e)}")
100
+ return None, None, None
101
 
102
+ def create_print_layout(data, title, date_str):
103
  """创建打印布局"""
104
  if data.empty:
105
  return None
106
+
 
 
 
 
 
107
  # 设置 A5 纸张竖向尺寸
108
  fig = plt.figure(figsize=(5.83, 8.27), dpi=300)
109
+ plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.1)
110
+
111
+ # 设置字体
112
+ plt.rcParams['font.family'] = 'sans-serif'
113
+ plt.rcParams['font.sans-serif'] = ['SimHei']
114
 
115
  # 计算行数和总数
116
  total_items = len(data)
 
118
  num_rows = math.ceil(total_items / num_cols)
119
 
120
  # 创建网格
121
+ gs = gridspec.GridSpec(num_rows + 1, num_cols, hspace=0.1, wspace=0.1, height_ratios=[1] * num_rows + [0.2])
122
 
123
  # 计算字体大小
124
  base_fontsize = min(45, 265 / num_rows)
 
176
  ax.set_yticks([])
177
 
178
  # 在底部添加日期和班次信息
179
+ ax_bottom = plt.subplot(gs[num_rows, :])
180
+ ax_bottom.text(0.5, 0.5, f"{date_str} {title}",
181
+ fontsize=base_fontsize * 0.8,
182
+ fontweight='bold',
183
+ ha='center',
184
+ va='center')
185
+
186
+ # 移除底部坐标轴
187
+ ax_bottom.set_xticks([])
188
+ ax_bottom.set_yticks([])
189
+ for spine in ax_bottom.spines.values():
190
+ spine.set_visible(False)
191
 
192
  # 转换为图片
193
  buffer = io.BytesIO()
194
+ plt.savefig(buffer, format='png', bbox_inches='tight', pad_inches=0.05)
195
  buffer.seek(0)
196
  image_base64 = base64.b64encode(buffer.getvalue()).decode()
197
  plt.close()
 
205
  uploaded_file = st.file_uploader("上传【放映场次核对表.xls】文件", type=["xls", "xlsx"])
206
 
207
  if uploaded_file:
208
+ part1, part2, date_str = process_schedule(uploaded_file)
 
 
 
 
209
 
210
  if part1 is not None and part2 is not None:
211
+ # 创建打印布局
212
+ part1_image = create_print_layout(part1, "白班", date_str)
213
+ part2_image = create_print_layout(part2, "夜班", date_str)
214
 
215
  # 显示预览
216
  col1, col2 = st.columns(2)