@@ -217,7 +217,9 @@ def add_bookmarks(pdf_path: str, chapters: list) -> None:
217217 # print(f"处理章节“{title}”,页码索引:{p_index}")
218218 # 2. 如果值为 None (JSON里的null) 或者不存在,跳过这个书签(因为未使用)
219219 if p_index is None :
220- sys .stderr .write (f"[!!]跳过章节“{ title } ”的书签,原因:未指定页码\n " )
220+ # 无控制台运行时(pyw/exe)sys.stderr 可能为 None,需保护。
221+ if sys .stderr is not None :
222+ sys .stderr .write (f"[!!]跳过章节“{ title } ”的书签,原因:未指定页码\n " )
221223 continue
222224 # 3. 尝试将其转为整数并减 1 (pypdf 页码从 0 开始)
223225 try :
@@ -246,8 +248,10 @@ def add_bookmarks(pdf_path: str, chapters: list) -> None:
246248 writer .write (f )
247249
248250 except Exception as e :
249- sys .stderr .write (f"添加书签失败: { e } \n " )
250- traceback .print_exc ()
251+ # 无控制台运行时(pyw/exe)sys.stderr 可能为 None,需保护。
252+ if sys .stderr is not None :
253+ sys .stderr .write (f"添加书签失败: { e } \n " )
254+ traceback .print_exc ()
251255
252256def ui_call (func , * args , ** kwargs ) -> None :
253257 """在主线程执行 Tk UI 更新"""
@@ -289,7 +293,7 @@ def download_file(url: str, save_path: str, chapters: list | None = None) -> Non
289293 except Exception as e :
290294 current_state ["downloaded_size" ], current_state ["total_size" ] = 0 , 0
291295 current_state ["finished" ] = True
292- current_state ["failed_reason" ] = str ( e )
296+ current_state ["failed_reason" ] = traceback . format_exc (). rstrip ( )
293297
294298 if all (state ["finished" ] for state in download_states ): # 所有文件下载完成
295299 ui_call (download_progress_bar .config , value = 0 ) # 重置进度条
@@ -298,7 +302,15 @@ def download_file(url: str, save_path: str, chapters: list | None = None) -> Non
298302
299303 failed_states = [state for state in download_states if state ["failed_reason" ]]
300304 if len (failed_states ) > 0 : # 存在下载失败的文件
301- ui_call (messagebox .showwarning , "下载完成" , f"文件已下载到:{ os .path .dirname (save_path )} \n 以下文件下载失败:\n { "\n " .join (f"{ state ["download_url" ]} ,原因:{ state ["failed_reason" ]} " for state in failed_states )} " )
305+ failed_message = "\n \n " .join (
306+ f"{ state .get ('download_url' )} \n { state .get ('failed_reason' )} "
307+ for state in failed_states
308+ )
309+ ui_call (
310+ messagebox .showwarning ,
311+ "下载完成" ,
312+ f"文件已下载到:{ os .path .dirname (save_path )} \n 以下文件下载失败:\n { failed_message } " ,
313+ )
302314 else :
303315 ui_call (messagebox .showinfo , "下载完成" , f"文件已下载到:{ os .path .dirname (save_path )} " )
304316
0 commit comments