Portugal Cartão de Cidadão OCR Python SDK
Instantly extract data and MRZ from Portuguese IDs using our native Python library.
Parsing Cartão de Cidadão Challenges
Parsing the Cartão de Cidadão presents unique challenges due to variations in layout across different card versions and the presence of accented Portuguese characters, which require robust character recognition capabilities.
Why StructOCR for Portugal
Our model is specifically trained on a large dataset of Portuguese Cartão de Cidadão images, ensuring high accuracy. The Python SDK simplifies integration with a clean API, making data extraction quick and easy. This specialized id parsing api is designed to accurately interpret the 14 digits structure found on these identification documents, and it fully supports reading the Machine Readable Zone (MRZ) for rigorous global compliance and verification.
Common Use Cases in Portugal
- Digital Onboarding: Verify users for Fintech apps in Portugal.
- Telecom Registration: Automate SIM card registration with Cartão de Cidadão.
- Hotel Check-in: Speed up guest registration workflows.
Python SDK Integration
Install the SDK via pip: `pip install structocr`. Then use the following code.
Prerequisite: Python 3.6+ and `structocr` library installed.
from structocr import StructOCR
# Get 200 free credits:
# 👉 https://structocr.com/register
# Initialize with your API Key
client = StructOCR("YOUR_API_KEY_HERE")
def scan_portugal_id():
# Note: Supports JPG, PNG, WebP (Max 4.5MB)
# Target: Cartão de Cidadão
image_path = "portugal_national_id.jpg"
try:
print(f"Scanning {image_path}...")
# The SDK handles file upload and API communication
# It automatically detects that this is a Portuguese document
result = client.scan_national_id(image_path)
# Check success flag (SDK returns a dict matching the JSON response)
if result.get('success'):
data = result['data']
print("✅ Portugal Extraction Successful!")
# Basic Identity
print(f"Region: {data.get('country_code')} (Series: {data.get('card_series')})")
print(f"Name: {data.get('given_names')} {data.get('surname')}")
print(f"ID Number: {data.get('document_number')}")
# Critical Field: Personal Identity Number (CNP/CPF/NIN)
print(f"Personal #: {data.get('personal_number')}")
# Demographics
print(f"DOB: {data.get('date_of_birth')} ({data.get('sex')})")
print(f"Address: {data.get('address')}")
# Machine Readable Zone (MRZ)
additional = data.get('additional_fields', {})
if additional.get('mrz_line_1'):
print("\n--- MRZ Data ---")
print(additional.get('mrz_line_1'))
print(additional.get('mrz_line_2'))
if additional.get('mrz_line_3'):
print(additional.get('mrz_line_3'))
else:
print(f"❌ Extraction Failed: {result.get('error')}")
except Exception as e:
# Handle SDK or Network errors
print(f"An error occurred: {e}")
if __name__ == "__main__":
scan_portugal_id()Technical Specs
- •Latency: < 4s (Average)
- •Availability: Served from Cloudflare's global edge network
- •Security: AES-256 Encryption & SOC2 Compliant
- •Input: JPG, PNG, WebP (Max 4.5MB)
- •Output: JSON (Structured Data)
Key Features
- •Native Script Support: Reads English and local characters.
- •MRZ Parsing: Accurately extracts and validates the Machine Readable Zone lines for cross-checking.
- •Blur Detection: Automatically rejects blurry images.
- •Fraud Check: Validates Cartão de Cidadão number format.
- •Smart Crop: Removes background noise automatically.
JSON Response Example
The SDK returns a Python dictionary matching this JSON structure.
{
"success": true,
"data": {
"type": "national_id",
"country_code": "PRT",
"nationality": "PORTUGUESA",
"document_number": "12345678",
"card_series": "v2",
"personal_number": "123456789 ZX8",
"surname": "FERREIRA",
"given_names": "MARIA DE FÁTIMA",
"sex": "M",
"date_of_birth": "1990-05-15",
"place_of_birth": "LISBOA",
"address": "Av. da Liberdade 110, 2º Esq., 1250-146 Lisboa",
"date_of_issue": "2020-01-01",
"date_of_expiry": "2030-01-01",
"issuing_authority": "República Portuguesa",
"additional_fields": {
"phone_number": null,
"tramite_number": null,
"ejemplar": null,
"mrz_line_1": "IDPRT12345678<0<<<<<<<<<<<<<<<",
"mrz_line_2": "9005151M3001018PRT123456789ZX84",
"mrz_line_3": "FERREIRA<<MARIA<DE<FATIMA<<<<<"
}
}
}Frequently Asked Questions
Does the Python SDK handle image uploads?
Yes, the SDK automatically handles base64 encoding and file uploads.
Is data stored?
Only briefly. Uploaded images and results are kept for at most 24 hours for quality spot-checks, then deleted.
How to handle errors?
The SDK result dictionary contains a 'success' boolean and an 'error' message if failed.
You May Also Like
Related tutorials, platform guides, and comparisons
Portugal Passport OCR with Python SDK
Use the official StructOCR Python SDK and FastAPI to extract structured MRZ and VIZ data from Portugal passports with a server-side integration.
UAE Passport OCR with Python SDK
Use the official StructOCR Python SDK and FastAPI to extract structured MRZ and VIZ data from UAE passports with a server-side integration.
Côte d'Ivoire CNI (Carte Nationale d'Identité) OCR Python SDK
Python Tutorial: Automate KYC in Côte d'Ivoire. Extract French text, NNI, and validate TD1 MRZ from the ONECI CNI using StructOCR Python SDK.
Singapore Passport OCR with Python SDK
Use the official StructOCR Python SDK and FastAPI to extract structured MRZ and VIZ data from Singapore passports with a server-side integration.
Turkey Passport OCR with Python SDK
Use the official StructOCR Python SDK and FastAPI to extract structured MRZ and VIZ data from Turkey passports with a server-side integration.
Irish Passport OCR with Python SDK
Use the StructOCR Python SDK and FastAPI to extract visible identity, validity and MRZ fields from Irish passports.
Bangladesh Smart NID OCR Python SDK
Python Tutorial: Automate KYC in Bangladesh. Extract complex Bengali text and decode 2D barcodes from Smart NIDs using StructOCR Python SDK.
Belgium eID OCR Python SDK
Python Tutorial: Automate KYC in Belgium. Extract data from Electronic Identity Card (eID) using StructOCR Python SDK. Supports native text, National Register Number, and MRZ extraction.
Algeria CNIBE (Biometric ID) OCR Python SDK
Python Tutorial: Automate KYC in Algeria. Extract bilingual text and validate TD1 MRZ from CNIBE (Biometric ID) using StructOCR Python SDK.
Argentina DNI OCR Python SDK
Python Tutorial: Automate KYC in Argentina. Extract Spanish text, CUIL/CUIT, and validate TD1 MRZ from DNI using StructOCR Python SDK.
Technical Comparisons & Integrations
Explore platform integrations and competitive analysis
Add National ID OCR to Your Replit App in 5 Minutes
Step-by-step guide to integrating the StructOCR National ID scanner API into a Replit application. Build KYC and identity verification flows using Replit Agent and Secrets.
Add Passport OCR to Your Replit App in 5 Minutes
Step-by-step guide to integrating the StructOCR Passport scanner API into a Replit application. Build travel, hospitality, and global KYC flows using Replit Agent and Secrets.
Add Receipt OCR to Your Replit App in 5 Minutes
Step-by-step guide to integrating the StructOCR receipt API into a Replit application. Build expense tracking and receipt scanning tools using Replit Agent and Secrets.
Add VIN OCR to Your Replit App in 5 Minutes
Step-by-step guide to integrating the StructOCR VIN scanner API into a Replit application. Build automotive and fleet tools using Replit Agent and Secrets.
Single-Page MRZ SDKs vs. Dual-Page Indian KYC API
Why traditional MRZ scanners fail at full Indian Passport verification, and how to process both the front identity page and the back address page in a single API call.
Template-Based OCR vs. StructOCR API for Invoice Processing
Why traditional zonal/template-based OCR fails at processing vendor invoices, and how layout-agnostic AI APIs instantly extract line items and totals.
From tutorial to production in 5 minutes.
You've seen the code. Now get your API key, grab your 200 free credits, and see it work with your own images. No credit card required.