-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path04_file_conversion.py
More file actions
371 lines (293 loc) · 12 KB
/
04_file_conversion.py
File metadata and controls
371 lines (293 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""
File Conversion - Convert Various Formats to Text
This example demonstrates how to convert various file formats (PDF, DOCX, PPTX,
TXT, HTML, etc.) to text using the FileConverter class. This is useful for
processing documents before embedding or analysis.
Requirements:
- Core dependencies (already in ipfs_datasets_py)
- Optional: pandoc, pypandoc for advanced conversions
Usage:
python examples/04_file_conversion.py
"""
import asyncio
import tempfile
from pathlib import Path
def create_sample_files():
"""Create sample files for demonstration."""
samples = {}
# Create a text file
txt_file = tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False)
txt_file.write("This is a plain text document.\n")
txt_file.write("It contains multiple lines of text.\n")
txt_file.write("Plain text is the simplest format.")
txt_file.close()
samples['txt'] = txt_file.name
# Create a markdown file
md_file = tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False)
md_file.write("# Markdown Document\n\n")
md_file.write("This is a **markdown** file with:\n\n")
md_file.write("- Bullet points\n")
md_file.write("- *Italic text*\n")
md_file.write("- **Bold text**\n\n")
md_file.write("## Code Example\n\n")
md_file.write("```python\nprint('Hello, World!')\n```")
md_file.close()
samples['md'] = md_file.name
# Create a JSON file
json_file = tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False)
json_file.write('{\n')
json_file.write(' "title": "Sample JSON",\n')
json_file.write(' "description": "This is a JSON document",\n')
json_file.write(' "items": ["item1", "item2", "item3"],\n')
json_file.write(' "nested": {"key": "value"}\n')
json_file.write('}')
json_file.close()
samples['json'] = json_file.name
# Create a CSV file
csv_file = tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False)
csv_file.write("name,age,city\n")
csv_file.write("Alice,30,New York\n")
csv_file.write("Bob,25,Los Angeles\n")
csv_file.write("Charlie,35,Chicago\n")
csv_file.close()
samples['csv'] = csv_file.name
# Create an HTML file
html_file = tempfile.NamedTemporaryFile(mode='w', suffix='.html', delete=False)
html_file.write("<html><head><title>Sample HTML</title></head>\n")
html_file.write("<body>\n")
html_file.write(" <h1>HTML Document</h1>\n")
html_file.write(" <p>This is a paragraph with <strong>bold</strong> text.</p>\n")
html_file.write(" <ul><li>Item 1</li><li>Item 2</li></ul>\n")
html_file.write("</body></html>")
html_file.close()
samples['html'] = html_file.name
return samples
async def demo_basic_conversion():
"""Demonstrate basic file conversion."""
print("\n" + "="*70)
print("DEMO 1: Basic File Conversion")
print("="*70)
try:
from ipfs_datasets_py.processors.file_converter import FileConverter
# Create sample files
print("\n📝 Creating sample files...")
samples = create_sample_files()
# Initialize converter
converter = FileConverter()
# Convert each file
for file_type, file_path in samples.items():
print(f"\n🔄 Converting {file_type.upper()} file...")
try:
result = await converter.convert(file_path, target_format='txt')
if result.success:
print(f" ✅ Conversion successful")
print(f" Source format: {result.source_format}")
print(f" Output length: {len(result.content)} chars")
print(f" Preview: {result.content[:100]}...")
else:
print(f" ❌ Conversion failed: {result.error}")
except Exception as e:
print(f" ❌ Error: {e}")
# Cleanup
import os
for path in samples.values():
try:
os.unlink(path)
except:
pass
except ImportError as e:
print(f"\n❌ Missing dependencies: {e}")
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()
async def demo_metadata_extraction():
"""Demonstrate metadata extraction during conversion."""
print("\n" + "="*70)
print("DEMO 2: Metadata Extraction")
print("="*70)
try:
from ipfs_datasets_py.processors.file_converter import (
FileConverter,
MetadataExtractor
)
# Create a sample file
txt_file = tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False)
txt_file.write("Sample document with metadata.\n" * 10)
txt_file.close()
print(f"\n📄 Extracting metadata from: {txt_file.name}")
# Extract metadata
extractor = MetadataExtractor()
metadata = await extractor.extract(txt_file.name)
print("\n📊 Extracted Metadata:")
print(f" File size: {metadata.get('size_bytes', 0)} bytes")
print(f" MIME type: {metadata.get('mime_type', 'unknown')}")
print(f" Created: {metadata.get('created', 'unknown')}")
print(f" Modified: {metadata.get('modified', 'unknown')}")
if 'content_metadata' in metadata:
content_meta = metadata['content_metadata']
print(f"\n Content Metadata:")
print(f" - Character count: {content_meta.get('char_count', 0)}")
print(f" - Word count: {content_meta.get('word_count', 0)}")
print(f" - Line count: {content_meta.get('line_count', 0)}")
# Cleanup
import os
os.unlink(txt_file.name)
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()
async def demo_batch_conversion():
"""Demonstrate batch file conversion."""
print("\n" + "="*70)
print("DEMO 3: Batch File Conversion")
print("="*70)
try:
from ipfs_datasets_py.processors.file_converter import (
BatchProcessor,
ResourceLimits
)
# Create multiple sample files
print("\n📝 Creating sample files for batch processing...")
samples = create_sample_files()
file_paths = list(samples.values())
print(f" Created {len(file_paths)} files")
# Setup batch processor with resource limits
limits = ResourceLimits(
max_memory_mb=512,
max_concurrent=3,
timeout_seconds=30
)
print("\n🔄 Starting batch conversion...")
processor = BatchProcessor(resource_limits=limits)
# Process all files
results = await processor.process_batch(
file_paths,
target_format='txt'
)
# Display results
print("\n📊 Batch Processing Results:")
successful = sum(1 for r in results if r.success)
print(f" Total files: {len(results)}")
print(f" Successful: {successful}")
print(f" Failed: {len(results) - successful}")
# Show details
print("\n Details:")
for i, result in enumerate(results, 1):
status = "✅" if result.success else "❌"
print(f" {status} File {i}: {result.source_format} -> {len(result.content) if result.success else 0} chars")
# Cleanup
import os
for path in file_paths:
try:
os.unlink(path)
except:
pass
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()
async def demo_format_detection():
"""Demonstrate automatic format detection."""
print("\n" + "="*70)
print("DEMO 4: Automatic Format Detection")
print("="*70)
try:
from ipfs_datasets_py.processors.file_converter import FileConverter
samples = create_sample_files()
converter = FileConverter()
print("\n🔍 Detecting file formats...")
for file_type, file_path in samples.items():
# Detect without extension hint
detected = await converter.detect_format(file_path)
print(f" {file_type.upper():8s} -> Detected as: {detected}")
# Cleanup
import os
for path in samples.values():
try:
os.unlink(path)
except:
pass
except Exception as e:
print(f"\n❌ Error: {e}")
async def demo_url_download_convert():
"""Demonstrate downloading and converting from URL."""
print("\n" + "="*70)
print("DEMO 5: Download and Convert from URL")
print("="*70)
print("\n📥 URL download and conversion example")
print(" (Requires network access)")
# Example code (commented out as it requires network access)
"""
try:
from ipfs_datasets_py.processors.file_converter import FileConverter
converter = FileConverter()
# Download and convert a web page
url = "https://example.com"
result = await converter.convert_url(url, target_format='txt')
if result.success:
print(f" ✅ Downloaded and converted {url}")
print(f" Content length: {len(result.content)} chars")
except Exception as e:
print(f" ❌ Error: {e}")
"""
print("\n 💡 Tip: Use converter.convert_url(url, target_format='txt')")
print(" to download and convert web pages")
def show_supported_formats():
"""Show supported file formats."""
print("\n" + "="*70)
print("SUPPORTED FILE FORMATS")
print("="*70)
formats = {
"Text-based": ["txt", "md", "rst", "tex"],
"Documents": ["pdf", "docx", "odt", "rtf", "epub"],
"Presentations": ["pptx", "odp"],
"Spreadsheets": ["xlsx", "ods", "csv"],
"Web": ["html", "xml", "json", "yaml"],
"Archives": ["zip", "tar", "gz", "7z"],
"Code": ["py", "js", "java", "cpp", "go"],
}
for category, exts in formats.items():
print(f"\n{category}:")
print(f" {', '.join(exts)}")
def show_tips():
"""Show tips for file conversion."""
print("\n" + "="*70)
print("TIPS FOR FILE CONVERSION")
print("="*70)
print("\n1. Large Files:")
print(" - Use BatchProcessor with ResourceLimits")
print(" - Set appropriate timeout values")
print(" - Monitor memory usage")
print("\n2. PDFs:")
print(" - Text PDFs convert quickly")
print(" - Image PDFs require OCR (see 07_pdf_processing.py)")
print(" - Consider using PyMuPDF for better quality")
print("\n3. Office Documents:")
print(" - Install pandoc for best results")
print(" - DOCX/PPTX conversion preserves structure")
print(" - Use metadata extraction for document properties")
print("\n4. Error Handling:")
print(" - Always check result.success before using content")
print(" - Log conversion errors for debugging")
print(" - Implement fallback strategies for critical files")
print("\n5. Next Steps:")
print(" - See 07_pdf_processing.py for advanced PDF handling")
print(" - See 09_batch_processing.py for large-scale processing")
async def main():
"""Run all file conversion demonstrations."""
print("\n" + "="*70)
print("IPFS DATASETS PYTHON - FILE CONVERSION")
print("="*70)
await demo_basic_conversion()
await demo_metadata_extraction()
await demo_batch_conversion()
await demo_format_detection()
await demo_url_download_convert()
show_supported_formats()
show_tips()
print("\n" + "="*70)
print("✅ FILE CONVERSION EXAMPLES COMPLETE")
print("="*70)
if __name__ == "__main__":
asyncio.run(main())