Tencent Cloud OCR SDK for Python
raw JSON → 3.1.92 verified Sat May 09 auth: no python
Official Tencent Cloud OCR SDK for Python, providing APIs for optical character recognition services including ID card, bank card, business license, and general text recognition. Current version 3.1.92, released Jan 2025. Active development with monthly releases.
pip install tencentcloud-sdk-python-ocr Common errors
error ModuleNotFoundError: No module named 'tencentcloud.ocr' ↓
cause Importing from 'tencentcloud.ocr' without the version subpackage.
fix
Install the package and use: from tencentcloud.ocr.v20181119 import ocr_client
error AttributeError: module 'tencentcloud.ocr.v20181119' has no attribute 'OcrClient' ↓
cause Trying to access OcrClient directly from the module instead of the correct class name inside ocr_client.
fix
Use: from tencentcloud.ocr.v20181119 import ocr_client then ocr_client.OcrClient
error tencentcloud.common.exception.TencentCloudSDKException: code: UnsupportedOperation, message: this action not support this region ↓
cause Specified endpoint region does not support the requested OCR action (e.g., GeneralBasicOCR).
fix
Check Tencent Cloud available regions per API; use a supported region like 'ap-guangzhou' or 'ap-beijing'.
Warnings
gotcha The SDK uses a versioned namespace (v20181119). Do not omit it; all clients and models must be imported from tencentcloud.ocr.v20181119. ↓
fix Use correct import: from tencentcloud.ocr.v20181119 import ocr_client, models
deprecated The old 'GeneralBasicOCR' method remains but newer endpoints like 'SmartStructuralOCR' are recommended for structured documents. ↓
fix Review Tencent Cloud OCR API documentation for latest available methods.
gotcha ImageBase64 or ImageUrl must be set; both can be provided but ImageUrl takes precedence per Tencent Cloud docs. ↓
fix Set exactly one of ImageBase64 (base64 string without header) or ImageUrl (public URL).
breaking SDK v3 dropped Python 2 support and restructured all modules. Old v2 imports (e.g., tencentcloud.ocr.OcrClient) will not work. ↓
fix Update to v3 import paths: from tencentcloud.ocr.v20181119 import ocr_client
Imports
- OcrClient wrong
from tencentcloud.ocr import OcrClientcorrectfrom tencentcloud.ocr.v20181119 import ocr_client - models wrong
from tencentcloud.ocr import modelscorrectfrom tencentcloud.ocr.v20181119 import models
Quickstart
import os
from tencentcloud.common import credential
from tencentcloud.ocr.v20181119 import ocr_client, models
cred = credential.Credential(
os.environ.get('TENCENTCLOUD_SECRET_ID', ''),
os.environ.get('TENCENTCLOUD_SECRET_KEY', '')
)
client = ocr_client.OcrClient(cred, "ap-guangzhou")
req = models.GeneralBasicOCRRequest()
req.ImageBase64 = "iVBORw0KGgoAAAANSUhEUgAA..."
resp = client.GeneralBasicOCR(req)
print(resp)