diff --git a/predict_with_excel.py b/predict_with_excel.py index de3c3e7..13ec5b6 100644 --- a/predict_with_excel.py +++ b/predict_with_excel.py @@ -316,6 +316,37 @@ def get_welding_time(row): return welding_time +def minutes_to_dhm_format(minutes): + """ + 将分钟数转换为 "xx天xx小时xx分钟" 的字符串格式 + + Args: + minutes: 分钟数(浮点数) + + Returns: + 格式化的字符串,例如:"2天5小时30分钟" + """ + if pd.isna(minutes) or not isinstance(minutes, (int, float)): + return "0分钟" + + total_minutes = int(round(minutes)) + + days = total_minutes // (24 * 60) + remaining = total_minutes % (24 * 60) + hours = remaining // 60 + mins = remaining % 60 + + parts = [] + if days > 0: + parts.append(f"{days}天") + if hours > 0: + parts.append(f"{hours}小时") + if mins > 0 or len(parts) == 0: # 如果没有天和小时,至少显示分钟 + parts.append(f"{mins}分钟") + + return "".join(parts) + + def save_df_to_excel(df, output_file_path): df.to_excel(output_file_path, index=False) @@ -345,7 +376,8 @@ def build_prediction_dataframe(df: pd.DataFrame) -> pd.DataFrame: total_row = {col: np.nan for col in df_without_last.columns} total_row["序号"] = "总计" - total_row["焊接工时"] = df_without_last["焊接工时"].sum() + total_minutes = df_without_last["焊接工时"].sum() + total_row["焊接工时"] = minutes_to_dhm_format(total_minutes) result_df = pd.concat([df_without_last, pd.DataFrame([total_row])], ignore_index=True) return result_df