binary-husky/gpt_academic · error · Exception
转换PDF失败: {str(e)}
Error message
转换PDF失败: {str(e)} What it means
The outer catch-all of WordToPdfConverter.convert_to_pdf: any exception escaping the platform branches (missing LibreOffice at [22], conversion failure at [23], empty PDF at [24]/[25], or anything else) is printed and re-raised as a generic Exception('转换PDF失败: <original>'). The original type is lost (bare `raise Exception`), so callers cannot distinguish root causes without string matching; the original message is preserved only as text.
Source
Thrown at crazy_functions/doc_fns/conversation_doc/word2pdf.py:86
raise RuntimeError("PDF生成失败或文件为空")
print(f"PDF转换成功,文件大小: {pdf_path.stat().st_size} 字节")
else:
# Windows和MacOS使用docx2pdf
print(f"使用docx2pdf转换 {word_path} 到 {pdf_path}")
convert(word_path, pdf_path)
# 验证PDF是否成功生成
if not pdf_path.exists() or pdf_path.stat().st_size == 0:
raise RuntimeError("PDF生成失败或文件为空")
print(f"PDF转换成功,文件大小: {pdf_path.stat().st_size} 字节")
return str(pdf_path)
except Exception as e:
print(f"PDF转换异常: {str(e)}")
raise Exception(f"转换PDF失败: {str(e)}")
@staticmethod
def batch_convert(word_dir: Union[str, Path], pdf_dir: Union[str, Path] = None) -> list:
"""
批量转换目录下的所有Word文档
参数:
word_dir: 包含Word文档的目录路径
pdf_dir: 可选,PDF文件的输出目录。如果未指定,将使用与Word文档相同的目录
返回:
生成的PDF文件路径列表
"""
word_dir = Path(word_dir)
if pdf_dir:
pdf_dir = Path(pdf_dir)
pdf_dir.mkdir(parents=True, exist_ok=True)
View on GitHub (pinned to d6bde0fa54)
Solutions
- Read the message suffix — it is the original error text (e.g. '请先安装LibreOffice...' means error 22); the printed 'PDF转换异常:' line above the raise carries the same info
- Fix the underlying cause per errors 22-25
- In your own wrapper, dispatch on message content or stop calling this API and call platform-specific tools directly
- If patching the library: use `raise` (bare) or `raise RuntimeError(...) from e` to preserve the cause chain
Example fix
// before
except Exception as e:
print(f"PDF转换异常: {str(e)}")
raise Exception(f"转换PDF失败: {str(e)}")
// after
except Exception as e:
print(f"PDF转换异常: {str(e)}")
raise RuntimeError(f"转换PDF失败: {str(e)}") from e Defensive patterns
Strategy: try-catch
Validate before calling
from pathlib import Path
import platform, shutil
def precheck_word2pdf(word_path) -> list[str]:
problems = []
p = Path(word_path)
if not p.exists(): problems.append('missing file')
if platform.system() == 'Linux' and shutil.which('libreoffice') is None:
problems.append('libreoffice missing')
return problems Try / catch
try:
pdf = WordToPdfConverter.convert_to_pdf(word_path)
except Exception as e: # everything is re-raised as bare Exception
msg = str(e)
if '请先安装LibreOffice' in msg: ...
elif 'LibreOffice转换失败' in msg: ...
elif 'PDF生成失败' in msg: ...
else: raise Prevention
- This wrapper flattens all underlying errors to Exception — dispatch on message substrings, or patch to re-raise with `from e`
- Run the preflight checks (file exists, converter present) yourself before calling
- Log the printed 'PDF转换异常:' line; it mirrors the original error
When it happens
Trigger: Any caller of convert_to_pdf() hitting the underlying errors [22]-[25], or unexpected ones like an invalid word_path (not a Path, suffix fails), pdf_path.parent creation permission errors, or os.rename cross-device failure.
Common situations: Developers see only the wrapped message in logs and must trace the preceding 'PDF转换异常:' print to identify the real cause; try/except ValueError style guards around convert_to_pdf never fire because everything is re-raised as bare Exception.
Related errors
- Failed to generate image, please try again later: {str(e)}
- 在线搜索失败!\n{Exceptions}
- 无法下载资源{txt},请检查。
- Nougat解析论文失败。
- 请先安装LibreOffice: sudo apt-get install libreoffice
AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14).
Data as JSON: /api/errors/2cb56b8503c5cefd.
Report an issue: GitHub.