-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcli.py
More file actions
153 lines (126 loc) · 4.18 KB
/
cli.py
File metadata and controls
153 lines (126 loc) · 4.18 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
#!/usr/bin/env python3
# Command line interface for doc2tex
# Used for batch processing or just quick converts from terminal
import argparse
import sys
import os
from pathlib import Path
from doc2tex import (
DocTeXConverter,
ConversionOptions,
DocumentType,
FontSize,
LineSpacing,
ConversionError
)
from doc2tex.utils import logger, setup_logger
def create_parser() -> argparse.ArgumentParser:
# Setting up all the flags for terminal usage
parser = argparse.ArgumentParser(
description='doc2tex - Simple DOCX ↔ LaTeX Converter',
epilog="""
Example usage:
python cli.py file.docx -o result.tex
python cli.py file.tex
"""
)
# Input files. You can pass multiple files here.
parser.add_argument(
'input',
nargs='+',
help='Files to convert'
)
parser.add_argument(
'-o', '--output',
help='Where to save the result. If not set, it guesses name.'
)
parser.add_argument(
'-d', '--output-dir',
help='Folder to save results if batching'
)
parser.add_argument(
'--direction',
choices=['to_latex', 'to_docx'],
help='Force it to go one way'
)
# Document settings
doc_group = parser.add_argument_group('Doc Settings')
doc_group.add_argument(
'--doc-type',
type=str,
choices=['article', 'report', 'thesis', 'letter', 'book'],
default='article'
)
doc_group.add_argument(
'--font-size',
type=str,
choices=['10pt', '11pt', '12pt'],
default='12pt'
)
doc_group.add_argument(
'--spacing',
type=str,
choices=['single', 'onehalf', 'double'],
default='single'
)
doc_group.add_argument(
'--margins',
type=str,
default='top=2.5cm,bottom=2.5cm,left=2.5cm,right=2.5cm'
)
# Bib and Image settings
parser.add_argument('--extract-bib', action='store_true', help='Try to extract bibliography')
parser.add_argument('--no-images', action='store_true', help='Dont handle images')
parser.add_argument('--optimize-images', action='store_true', help='Shrink images')
# General stuff
parser.add_argument('-v', '--verbose', action='store_true', help='Show more logs')
parser.add_argument('--version', action='version', version='doc2tex 1.0.0')
return parser
def build_options(args: argparse.Namespace) -> ConversionOptions:
# Map CLI args to our ConversionOptions class
return ConversionOptions(
document_type=DocumentType(args.doc_type),
font_size=FontSize(args.font_size),
line_spacing=LineSpacing(args.spacing),
page_margins=args.margins,
extract_bibliography=args.extract_bib,
preserve_images=not args.no_images,
optimize_images=args.optimize_images,
verbose=args.verbose,
)
def main():
parser = create_parser()
args = parser.parse_args()
# Setup the logger format
setup_logger(verbose=args.verbose)
try:
options = build_options(args)
converter = DocTeXConverter(options)
# Batch conversion if more than one file
if len(args.input) > 1 or args.output_dir:
logger.info(f"Batch converting {len(args.input)} files...")
results = converter.batch_convert(
args.input,
output_dir=args.output_dir,
direction=args.direction
)
success = sum(1 for r in results if r is not None)
logger.info(f"Done! {success}/{len(results)} worked.")
# Single file conversion
else:
input_file = args.input[0]
output_file = args.output
result = converter.convert(
input_file,
output_file,
direction=args.direction
)
print(f"Success! Saved to: {result}")
except ConversionError as e:
logger.error(f"Error: {e}")
sys.exit(1)
except Exception as e:
logger.error(f"Unexpected error: {e}")
sys.exit(1)
if __name__ == '__main__':
main()