-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.py
More file actions
99 lines (76 loc) · 2.66 KB
/
init.py
File metadata and controls
99 lines (76 loc) · 2.66 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
"""
unoconvert - Universal Office Converter.
Convert between office document formats using LibreOffice/OpenOffice.
"""
__version__ = '0.8.2'
from .config import Config
from .converter import Converter
from .connection import OfficeConnection
from .listener import Listener
from .formats import fmts, doctypes
def convert(input_file, output_file=None, format='pdf', **kwargs):
"""
Convert a document from one format to another.
Args:
input_file (str): Path to the input file or file-like object content
output_file (str, optional): Path to the output file. If None, it will be generated.
format (str, optional): Output format. Defaults to 'pdf'.
**kwargs: Additional configuration options
Returns:
str: Path to the output file
Raises:
RuntimeError: If conversion fails
ValueError: If format is not supported
ImportError: If LibreOffice/OpenOffice is not found
"""
# Create configuration
config = Config(format=format, **kwargs)
# Set up input and output
if output_file:
config.output = output_file
if hasattr(input_file, 'read'):
# Input is a file-like object
config.stdin = True
input_content = input_file.read()
else:
# Input is a filename
config.filenames = [input_file]
input_content = input_file
# Connect to LibreOffice/OpenOffice
connection = OfficeConnection(config)
if not connection.connect():
raise RuntimeError("Failed to connect to LibreOffice/OpenOffice")
# Convert document
converter = Converter(connection, config)
return converter.convert(input_content)
def listen(**kwargs):
"""
Start a LibreOffice/OpenOffice listener.
Args:
**kwargs: Configuration options
Returns:
bool: True if listener was started successfully, False otherwise
Raises:
ImportError: If LibreOffice/OpenOffice is not found
"""
# Create configuration
config = Config(listener=True, **kwargs)
# Connect to LibreOffice/OpenOffice
connection = OfficeConnection(config)
if not connection.connect():
raise RuntimeError("Failed to connect to LibreOffice/OpenOffice")
# Start listener
listener = Listener(connection, config)
return listener.start()
def get_formats(doctype=None):
"""
Get available formats.
Args:
doctype (str, optional): Document type to filter formats. Defaults to None.
Returns:
list: Available formats
"""
if doctype:
return [fmt for fmt in fmts.list if fmt.doctype == doctype]
else:
return fmts.list