|
| 1 | +# Iteration Layer Python SDK |
| 2 | + |
| 3 | +Official Python SDK for the [Iteration Layer API](https://iterationlayer.com). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```sh |
| 8 | +pip install iterationlayer |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +```python |
| 14 | +from iterationlayer import IterationLayer |
| 15 | + |
| 16 | +client = IterationLayer("il_your_api_key") |
| 17 | +``` |
| 18 | + |
| 19 | +### Document Extraction |
| 20 | + |
| 21 | +Extract structured data from documents using AI. |
| 22 | + |
| 23 | +```python |
| 24 | +from iterationlayer import TextFieldConfig, CurrencyAmountFieldConfig |
| 25 | + |
| 26 | +result = client.extract( |
| 27 | + files=[{"type": "url", "name": "invoice.pdf", "url": "https://example.com/invoice.pdf"}], |
| 28 | + schema={ |
| 29 | + "fields": [ |
| 30 | + TextFieldConfig(type="TEXT", name="company_name", description="The company name"), |
| 31 | + CurrencyAmountFieldConfig(type="CURRENCY_AMOUNT", name="total", description="The invoice total"), |
| 32 | + ] |
| 33 | + }, |
| 34 | +) |
| 35 | + |
| 36 | +print(result["company_name"]["value"]) # "Acme Corp" |
| 37 | +print(result["company_name"]["confidence"]) # 0.95 |
| 38 | +``` |
| 39 | + |
| 40 | +### Image Transformation |
| 41 | + |
| 42 | +Resize, crop, convert, and apply effects to images. |
| 43 | + |
| 44 | +```python |
| 45 | +from iterationlayer import ResizeOperation, ConvertOperation |
| 46 | + |
| 47 | +result = client.transform( |
| 48 | + file={"type": "url", "name": "photo.jpg", "url": "https://example.com/photo.jpg"}, |
| 49 | + operations=[ |
| 50 | + ResizeOperation(type="resize", width_in_px=800, height_in_px=600, fit="cover"), |
| 51 | + ConvertOperation(type="convert", format="webp", quality=85), |
| 52 | + ], |
| 53 | +) |
| 54 | + |
| 55 | +import base64 |
| 56 | +image_bytes = base64.b64decode(result["buffer"]) |
| 57 | +``` |
| 58 | + |
| 59 | +### Image Generation |
| 60 | + |
| 61 | +Compose images from layer definitions. |
| 62 | + |
| 63 | +```python |
| 64 | +from iterationlayer import SolidColorBackgroundLayer, TextLayer |
| 65 | + |
| 66 | +result = client.generate_image( |
| 67 | + dimensions={"width_in_px": 1200, "height_in_px": 630}, |
| 68 | + layers=[ |
| 69 | + SolidColorBackgroundLayer(type="solid-color-background", index=0, hex_color="#1a1a2e"), |
| 70 | + TextLayer( |
| 71 | + type="text", |
| 72 | + index=1, |
| 73 | + text="Hello World", |
| 74 | + font_name="Inter", |
| 75 | + font_size_in_px=48, |
| 76 | + text_color="#ffffff", |
| 77 | + position={"x_in_px": 50, "y_in_px": 50}, |
| 78 | + dimensions={"width_in_px": 1100, "height_in_px": 530}, |
| 79 | + ), |
| 80 | + ], |
| 81 | + output_format="png", |
| 82 | +) |
| 83 | + |
| 84 | +import base64 |
| 85 | +image_bytes = base64.b64decode(result["buffer"]) |
| 86 | +``` |
| 87 | + |
| 88 | +### Document Generation |
| 89 | + |
| 90 | +Generate PDF, DOCX, EPUB, or PPTX from structured data. |
| 91 | + |
| 92 | +```python |
| 93 | +from iterationlayer import HeadlineBlock, ParagraphBlock |
| 94 | + |
| 95 | +result = client.generate_document( |
| 96 | + format="pdf", |
| 97 | + document={ |
| 98 | + "metadata": {"title": "Invoice #123"}, |
| 99 | + "page": { |
| 100 | + "size": {"preset": "A4"}, |
| 101 | + "margins": {"top_in_pt": 36, "bottom_in_pt": 36, "left_in_pt": 36, "right_in_pt": 36}, |
| 102 | + }, |
| 103 | + "styles": { |
| 104 | + "text": {"font_family": "Helvetica", "font_size_in_pt": 12, "line_height": 1.5, "color": "#000000"}, |
| 105 | + "headline": {"font_family": "Helvetica", "font_size_in_pt": 24, "color": "#000000", "spacing_before_in_pt": 12, "spacing_after_in_pt": 6}, |
| 106 | + "link": {"color": "#0066cc"}, |
| 107 | + "list": {"indent_in_pt": 18, "spacing_between_items_in_pt": 4}, |
| 108 | + "table": { |
| 109 | + "header": {"background_color": "#f0f0f0", "font_family": "Helvetica", "font_size_in_pt": 12, "color": "#000000", "padding_in_pt": 6}, |
| 110 | + "body": {"font_family": "Helvetica", "font_size_in_pt": 12, "color": "#000000", "padding_in_pt": 6}, |
| 111 | + }, |
| 112 | + "grid": {"gap_in_pt": 12}, |
| 113 | + "separator": {"color": "#cccccc", "thickness_in_pt": 1, "margin_top_in_pt": 12, "margin_bottom_in_pt": 12}, |
| 114 | + "image": {"alignment": "center", "margin_top_in_pt": 8, "margin_bottom_in_pt": 8}, |
| 115 | + }, |
| 116 | + "content": [ |
| 117 | + HeadlineBlock(type="headline", level="h1", text="Invoice #123"), |
| 118 | + ParagraphBlock(type="paragraph", markdown="Thank you for your business."), |
| 119 | + ], |
| 120 | + }, |
| 121 | +) |
| 122 | + |
| 123 | +import base64 |
| 124 | +pdf_bytes = base64.b64decode(result["buffer"]) |
| 125 | +``` |
| 126 | + |
| 127 | +### Webhooks (Async) |
| 128 | + |
| 129 | +Provide a `webhook_url` to receive results asynchronously. |
| 130 | + |
| 131 | +```python |
| 132 | +result = client.extract( |
| 133 | + files=[{"type": "url", "name": "invoice.pdf", "url": "https://example.com/invoice.pdf"}], |
| 134 | + schema={ |
| 135 | + "fields": [ |
| 136 | + CurrencyAmountFieldConfig(type="CURRENCY_AMOUNT", name="total", description="The invoice total"), |
| 137 | + ] |
| 138 | + }, |
| 139 | + webhook_url="https://your-app.com/webhooks/extraction", |
| 140 | +) |
| 141 | + |
| 142 | +# result = {"async": True, "message": "Request accepted..."} |
| 143 | +``` |
| 144 | + |
| 145 | +### Context Manager |
| 146 | + |
| 147 | +The client can be used as a context manager to ensure the underlying HTTP connection is closed. |
| 148 | + |
| 149 | +```python |
| 150 | +with IterationLayer("il_your_api_key") as client: |
| 151 | + result = client.extract(...) |
| 152 | +``` |
| 153 | + |
| 154 | +### Error Handling |
| 155 | + |
| 156 | +```python |
| 157 | +from iterationlayer import IterationLayerError |
| 158 | + |
| 159 | +try: |
| 160 | + result = client.extract(...) |
| 161 | +except IterationLayerError as e: |
| 162 | + print(e.status_code) # 422 |
| 163 | + print(e.error_message) # "Validation error: ..." |
| 164 | +``` |
| 165 | + |
| 166 | +## Documentation |
| 167 | + |
| 168 | +Full documentation is available at [iterationlayer.com/docs](https://iterationlayer.com/docs). |
| 169 | + |
| 170 | +## Issues & Feedback |
| 171 | + |
| 172 | +Please report bugs and request features in the [issues repository](https://github.com/iterationlayer/issues). |
0 commit comments