Skip to content

Commit 111b05b

Browse files
committed
add example
1 parent 08de0a1 commit 111b05b

File tree

9 files changed

+492
-2
lines changed

9 files changed

+492
-2
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,5 @@ openapi_spec.yml
154154
.pixi
155155
.claude/settings.local.json
156156

157-
# Environment file
158-
.env
157+
# Example output
158+
examples/output

examples/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NUTRIENT_API_KEY=your_api_key_here

examples/README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Nutrient DWS Python Client Examples
2+
3+
This example project demonstrates how to use the Nutrient DWS Python Client for document processing operations.
4+
5+
## Project Structure
6+
7+
- `assets/` - Contains sample files for processing (PDF, DOCX, PNG)
8+
- `src/` - Contains Python source files
9+
- `direct_method.py` - Examples using direct method calls
10+
- `workflow.py` - Examples using the workflow builder pattern
11+
- `output/` - Directory where processed files will be saved
12+
- `.env.example` - Example environment variables file
13+
14+
## Prerequisites
15+
16+
- Python 3.8 or higher
17+
- pip
18+
19+
## Setup
20+
21+
1. Clone the repository:
22+
```bash
23+
git clone https://github.com/pspdfkit-labs/nutrient-dws-client-python.git
24+
cd nutrient-dws-client-python
25+
```
26+
27+
2. Install the main package in development mode:
28+
```bash
29+
pip install -e .
30+
```
31+
32+
3. Navigate to the examples directory:
33+
```bash
34+
cd examples
35+
```
36+
37+
4. Install dependencies for the example project:
38+
```bash
39+
pip install -r requirements.txt
40+
```
41+
42+
5. Create a `.env` file from the example:
43+
```bash
44+
cp .env.example .env
45+
```
46+
47+
6. Edit the `.env` file and add your Nutrient DWS Processor API key. You can sign up for a free API key by visiting [Nutrient](https://www.nutrient.io/api/):
48+
```
49+
NUTRIENT_API_KEY=your_api_key_here
50+
```
51+
52+
## Running the Examples
53+
54+
### Direct Method Examples
55+
56+
To run the direct method examples:
57+
58+
```bash
59+
python src/direct_method.py
60+
```
61+
62+
This will:
63+
1. Convert a DOCX file to PDF
64+
2. Extract text from the PDF
65+
3. Add a watermark to the PDF
66+
4. Merge multiple documents
67+
5. Process sample.pdf directly with text extraction and image watermarking
68+
69+
### Workflow Examples
70+
71+
To run the workflow examples:
72+
73+
```bash
74+
python src/workflow.py
75+
```
76+
77+
This will:
78+
1. Perform a basic document conversion workflow
79+
2. Create a document merging with watermark workflow
80+
3. Extract text with JSON output
81+
4. Execute a complex multi-step workflow
82+
5. Process sample.pdf using workflow pattern
83+
84+
## Output
85+
86+
All processed files will be saved to the `output/` directory. You can examine these files to see the results of the document processing operations.
87+
88+
## Documentation
89+
90+
For more information about the Nutrient DWS Python Client, refer to:
91+
92+
- [README.md](../README.md) - Main documentation
93+
- [METHODS.md](../METHODS.md) - Direct methods documentation
94+
- [LLM_DOC.md](../LLM_DOC.md) - Complete API documentation

examples/assets/sample.docx

16.4 KB
Binary file not shown.

examples/assets/sample.pdf

277 KB
Binary file not shown.

examples/assets/sample.png

11.3 KB
Loading

examples/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python-dotenv>=1.0.0
2+
pathlib

examples/src/direct_method.py

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
"""
2+
Direct Method Example
3+
4+
This example demonstrates how to use the Nutrient DWS Python Client
5+
with direct method calls for document processing operations.
6+
"""
7+
8+
import asyncio
9+
import os
10+
import json
11+
from pathlib import Path
12+
from dotenv import load_dotenv
13+
14+
from nutrient_dws import NutrientClient
15+
16+
# Load environment variables from .env file
17+
load_dotenv()
18+
19+
# Check if API key is provided
20+
if not os.getenv('NUTRIENT_API_KEY'):
21+
print('Error: NUTRIENT_API_KEY is not set in .env file')
22+
exit(1)
23+
24+
# Initialize the client with API key
25+
client = NutrientClient({
26+
'apiKey': os.getenv('NUTRIENT_API_KEY')
27+
})
28+
29+
# Define paths
30+
assets_dir = Path(__file__).parent.parent / 'assets'
31+
output_dir = Path(__file__).parent.parent / 'output'
32+
33+
# Ensure output directory exists
34+
output_dir.mkdir(parents=True, exist_ok=True)
35+
36+
37+
# Example 1: Convert a document
38+
async def convert_document():
39+
print('Example 1: Converting DOCX to PDF')
40+
41+
try:
42+
docx_path = assets_dir / 'sample.docx'
43+
result = await client.convert(docx_path, 'pdf')
44+
45+
# Save the result to the output directory
46+
output_path = output_dir / 'converted-document.pdf'
47+
with open(output_path, 'wb') as f:
48+
f.write(result['buffer'])
49+
50+
print(f'Conversion successful. Output saved to: {output_path}')
51+
print(f'MIME type: {result["mimeType"]}')
52+
return output_path
53+
except Exception as error:
54+
print(f'Conversion failed: {error}')
55+
raise error
56+
57+
58+
# Example 2: Extract text from a document
59+
async def extract_text(file_path: Path):
60+
print('\nExample 2: Extracting text from PDF')
61+
62+
try:
63+
result = await client.extract_text(file_path)
64+
65+
# Save the extracted text to the output directory
66+
output_path = output_dir / 'extracted-text.json'
67+
with open(output_path, 'w') as f:
68+
json.dump(result['data'], f, indent=2, default=str)
69+
70+
# Display a sample of the extracted text
71+
text_sample = result['data']['pages'][0]['plainText'][:100] + '...'
72+
print(f'Text extraction successful. Output saved to: {output_path}')
73+
print(f'Text sample: {text_sample}')
74+
return output_path
75+
except Exception as error:
76+
print(f'Text extraction failed: {error}')
77+
raise error
78+
79+
80+
# Example 3: Add a watermark to a document
81+
async def add_watermark(file_path: Path):
82+
print('\nExample 3: Adding watermark to PDF')
83+
84+
try:
85+
result = await client.watermark_text(file_path, 'CONFIDENTIAL', {
86+
'opacity': 0.5,
87+
'font_color': '#FF0000',
88+
'rotation': 45,
89+
'width': {'value': 50, 'unit': '%'}
90+
})
91+
92+
# Save the watermarked document to the output directory
93+
output_path = output_dir / 'watermarked-document.pdf'
94+
with open(output_path, 'wb') as f:
95+
f.write(result['buffer'])
96+
97+
print(f'Watermarking successful. Output saved to: {output_path}')
98+
return output_path
99+
except Exception as error:
100+
print(f'Watermarking failed: {error}')
101+
raise error
102+
103+
104+
# Example 4: Merge multiple documents
105+
async def merge_documents():
106+
print('\nExample 4: Merging documents')
107+
108+
try:
109+
# Create a second PDF
110+
pdf_path = assets_dir / 'sample.pdf'
111+
112+
# Get the converted PDF from Example 1
113+
converted_pdf_path = output_dir / 'converted-document.pdf'
114+
115+
# Merge the documents
116+
result = await client.merge([converted_pdf_path, pdf_path])
117+
118+
# Save the merged document to the output directory
119+
output_path = output_dir / 'merged-document.pdf'
120+
with open(output_path, 'wb') as f:
121+
f.write(result['buffer'])
122+
123+
print(f'Merging successful. Output saved to: {output_path}')
124+
return output_path
125+
except Exception as error:
126+
print(f'Merging failed: {error}')
127+
raise error
128+
129+
130+
# Example 5: Process sample.pdf directly
131+
async def process_sample_pdf():
132+
print('\nExample 5: Processing sample.pdf directly')
133+
134+
try:
135+
pdf_path = assets_dir / 'sample.pdf'
136+
137+
# Extract text from sample.pdf
138+
extract_result = await client.extract_text(pdf_path)
139+
extract_output_path = output_dir / 'sample-pdf-extracted-text.json'
140+
with open(extract_output_path, 'w') as f:
141+
json.dump(extract_result['data'], f, indent=2, default=str)
142+
143+
watermark_image_path = assets_dir / 'sample.png'
144+
145+
# Add watermark to sample.pdf
146+
watermark_result = await client.watermark_image(pdf_path, watermark_image_path, {
147+
'opacity': 0.4,
148+
})
149+
150+
watermark_output_path = output_dir / 'sample-pdf-watermarked.pdf'
151+
with open(watermark_output_path, 'wb') as f:
152+
f.write(watermark_result['buffer'])
153+
154+
print('Sample PDF processing successful.')
155+
print(f'Extracted text saved to: {extract_output_path}')
156+
print(f'Watermarked PDF saved to: {watermark_output_path}')
157+
158+
return watermark_output_path
159+
except Exception as error:
160+
print(f'Sample PDF processing failed: {error}')
161+
raise error
162+
163+
164+
# Run all examples
165+
async def run_examples():
166+
try:
167+
print('Starting direct method examples...\n')
168+
169+
# Run the examples in sequence
170+
converted_pdf_path = await convert_document()
171+
await extract_text(converted_pdf_path)
172+
await add_watermark(converted_pdf_path)
173+
await merge_documents()
174+
await process_sample_pdf()
175+
176+
print('\nAll examples completed successfully!')
177+
except Exception as error:
178+
print(f'\nExamples failed: {error}')
179+
180+
181+
# Execute the examples
182+
if __name__ == '__main__':
183+
asyncio.run(run_examples())

0 commit comments

Comments
 (0)