-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
54 lines (44 loc) · 1.5 KB
/
example.py
File metadata and controls
54 lines (44 loc) · 1.5 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
#!/usr/bin/env python3
"""
Example script for using the unoconvert library programmatically.
This script demonstrates how to convert Excel to PDF using the library.
"""
import os
import sys
import argparse
from unoconvert import convert
def main():
"""Main entry point for the example script."""
parser = argparse.ArgumentParser(description='Convert Excel files to PDF')
parser.add_argument('input', help='Input Excel file')
parser.add_argument('-o', '--output', help='Output PDF file')
args = parser.parse_args()
# Check if input file exists
if not os.path.exists(args.input):
print(f"Error: Input file '{args.input}' does not exist", file=sys.stderr)
return 1
# Determine output filename if not specified
output_file = args.output
if not output_file:
base, _ = os.path.splitext(args.input)
output_file = f"{base}.pdf"
try:
# Convert Excel to PDF
print(f"Converting '{args.input}' to '{output_file}'...")
result = convert(
args.input,
output_file,
format='pdf',
# Additional options can be specified here
doctype='spreadsheet',
verbose=1,
timeout=30,
preserve=True
)
print(f"Conversion successful: '{result}'")
return 0
except Exception as e:
print(f"Error during conversion: {str(e)}", file=sys.stderr)
return 1
if __name__ == '__main__':
sys.exit(main())