PaddlePaddle/PaddleOCR · error · ValueError
Unsupported language
Error message
Unsupported language
What it means
Thrown by the pdf2word GUI app's initPredictor() when the language passed is neither 'EN' nor 'CN'. The method hard-codes model directories and dictionary mappings for exactly two languages; anything else (including lowercase 'en'/'cn') reaches the else branch. It is a whitelist guard, not a detection failure.
Source
Thrown at ppstructure/pdf2word/pdf2word.py:422
)
lang_dict = DICT_EN
elif lang == "CN":
args.det_model_dir = os.path.join(
root, "inference", "cn_PP-OCRv3_det_infer" # 此处从这里找到模型存放位置
)
args.rec_model_dir = os.path.join(
root, "inference", "cn_PP-OCRv3_rec_infer"
)
args.table_model_dir = os.path.join(
root, "inference", "cn_ppstructure_mobile_v2.0_SLANet_infer"
)
args.output = os.path.join(root, "output") # 结果保存路径
args.layout_model_dir = os.path.join(
root, "inference", "picodet_lcnet_x1_0_fgd_layout_cdla_infer"
)
lang_dict = DICT_CN
else:
raise ValueError("Unsupported language")
args.rec_char_dict_path = os.path.join(
root, "ppocr", "utils", lang_dict["rec_char_dict_path"]
)
args.layout_dict_path = os.path.join(
root, "ppocr", "utils", "dict", "layout_dict", lang_dict["layout_dict_path"]
)
# init predictor
return StructureSystem(args)
def handleOpenFileSignal(self):
"""
可以多选图像文件
"""
selectedFiles = QFileDialog.getOpenFileNames(
self, "多文件选择", "/", "图片文件 (*.png *.jpeg *.jpg *.bmp *.pdf)"
)[0]
if len(selectedFiles) > 0:
self.imagePaths = selectedFilesView on GitHub (pinned to 2661c7c0ef)
Solutions
- Pass exactly 'EN' or 'CN' (uppercase) to initPredictor
- If calling from code, normalize first: lang = 'CN' if lang.lower().startswith('zh') or lang.lower()=='cn' else 'EN'
- Extend the if/elif chain in ppstructure/pdf2word/pdf2word.py:391-421 to support additional languages by adding matching model dirs and lang_dict entries
Example fix
// before
predictor = window.initPredictor(lang='ch') # ValueError
// after
lang = 'CN' if lang.lower() in ('ch', 'cn', 'zh') else 'EN'
predictor = window.initPredictor(lang=lang) Defensive patterns
Strategy: validation
Validate before calling
SUPPORTED = {'EN', 'CN'}
if lang not in SUPPORTED:
raise SystemExit(f"lang must be one of {SUPPORTED}, got {lang!r}")
window.initPredictor(lang=lang) Type guard
def is_pdf2word_lang(lang: str) -> bool:
return isinstance(lang, str) and lang in {"EN", "CN"} Try / catch
try:
window.initPredictor(lang=lang)
except ValueError as e:
if 'Unsupported language' in str(e):
lang = 'CN' if lang.lower().startswith('zh') else 'EN'
window.initPredictor(lang=lang)
else:
raise Prevention
- Keep the language whitelist ('EN','CN') next to the UI dropdown so only valid values can be selected
- Normalize language codes at the system boundary (map 'ch'/'zh'/'cn' -> 'CN', 'en' -> 'EN')
When it happens
Trigger: Calling initPredictor(lang=...) with any string other than 'EN' or 'CN', e.g. 'ch' (the PaddleOCR CLI convention), 'en', 'zh', or a value read from a GUI dropdown/config file.
Common situations: Porting code from PaddleOCR CLI tools that use lang='ch' into the pdf2word GUI; a config file or UI combo box storing lowercase codes; passing locale identifiers like 'zh-CN'.
Related errors
- fileUrl and filePath are mutually exclusive.
- File not found: ${path}
- Bad request: ${text}
- OCR pipeline config text must decode to an object.
- OCR pipeline config must be an object or YAML text.
AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14).
Data as JSON: /api/errors/60d8c4a4a9146575.
Report an issue: GitHub.