|
| 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