-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_document.py
373 lines (316 loc) · 16 KB
/
process_document.py
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
372
373
#!/usr/bin/env python3
import sys
import os
import subprocess
import time
import venv
from pathlib import Path
import platform
import site
import importlib
import logging
import traceback
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s: %(message)s',
handlers=[
logging.StreamHandler(sys.stdout)
]
)
class DocumentProcessor:
def __init__(self):
self.last_output = []
def activate_venv(self):
"""Activate the virtual environment and return the activated environment path"""
try:
venv_path = Path('.venv')
if sys.platform == 'win32':
python_path = venv_path / 'Scripts' / 'python'
pip_path = venv_path / 'Scripts' / 'pip'
site_packages = venv_path / 'Lib' / 'site-packages'
else:
python_path = venv_path / 'bin' / 'python'
pip_path = venv_path / 'bin' / 'pip'
site_packages = venv_path / 'lib' / f'python{sys.version_info.major}.{sys.version_info.minor}' / 'site-packages'
# Validate virtual environment exists
if not venv_path.exists():
logging.error(f"Virtual environment not found at {venv_path}")
raise FileNotFoundError(f"Virtual environment not found at {venv_path}")
# Add virtual environment to Python path
if str(site_packages) not in sys.path:
sys.path.insert(0, str(site_packages))
# Create new environment with venv path
env = os.environ.copy()
env['VIRTUAL_ENV'] = str(venv_path)
env['PATH'] = str(venv_path / ('Scripts' if sys.platform == 'win32' else 'bin')) + os.pathsep + env['PATH']
# Unset PYTHONHOME if set
env.pop('PYTHONHOME', None)
# Reload site module to recognize the new environment
importlib.reload(site)
logging.info(f"Virtual environment activated at {venv_path}")
return env, str(python_path), str(pip_path)
except Exception as e:
logging.error(f"Error activating virtual environment: {e}")
logging.error(traceback.format_exc())
raise
def install_requirements(self, pip_path, env):
"""Install packages from requirements.txt"""
try:
logging.info("Installing required packages...")
result = subprocess.run(
[pip_path, 'install', '-r', 'requirements.txt'],
env=env,
check=True,
capture_output=True,
text=True
)
logging.info("Successfully installed requirements")
return result
except subprocess.CalledProcessError as e:
logging.error(f"Error installing requirements: {e.stderr}")
logging.error(traceback.format_exc())
raise
def create_venv(self, venv_path=Path('.venv')):
"""Create a new virtual environment"""
try:
if venv_path.exists():
logging.warning(f"Virtual environment already exists at {venv_path}")
return
logging.info(f"Creating virtual environment at {venv_path}")
venv.create(venv_path, with_pip=True)
logging.info("Virtual environment created successfully")
except Exception as e:
logging.error(f"Error creating virtual environment: {e}")
logging.error(traceback.format_exc())
raise
def parse_requirements(self, filename='requirements.txt'):
"""Parse requirements file and filter based on platform compatibility"""
if not os.path.exists(filename):
logging.warning(f"{filename} not found. Installing only python-docx.")
return ['python-docx']
requirements = []
current_platform = platform.system().lower()
with open(filename, 'r') as f:
for line in f:
# Skip comments and empty lines
line = line.strip()
if not line or line.startswith('#'):
continue
# Check for platform-specific markers
if ';' in line:
pkg, marker = line.split(';', 1)
pkg = pkg.strip()
marker = marker.strip()
# Basic platform checking
if 'platform_system' in marker:
if current_platform == 'darwin' and 'darwin' not in marker.lower():
continue
if current_platform == 'windows' and 'windows' not in marker.lower():
continue
if current_platform == 'linux' and 'linux' not in marker.lower():
continue
requirements.append(pkg)
else:
requirements.append(pkg)
else:
requirements.append(line)
return requirements
def check_linux_dependencies(self):
"""Check if required Linux dependencies are installed"""
system = platform.system()
if system != 'Linux':
return True
missing_deps = []
# Check for LibreOffice
if not any(os.path.exists(path) for path in [
'/usr/bin/soffice',
'/usr/lib/libreoffice/program/soffice'
]):
missing_deps.append('libreoffice')
# Check for unoconv
try:
subprocess.run(['unoconv', '--version'], capture_output=True, check=True)
except (subprocess.CalledProcessError, FileNotFoundError):
missing_deps.append('unoconv')
if missing_deps:
logging.error("Missing required Linux dependencies: " + ', '.join(missing_deps))
logging.error("\nPlease install them using your package manager:")
logging.error("\nFor Ubuntu/Debian:")
logging.error("sudo apt-get update")
logging.error(f"sudo apt-get install {' '.join(missing_deps)}")
logging.error("\nFor Oracle Linux/RHEL:")
logging.error("sudo yum update")
logging.error(f"sudo yum install {' '.join(missing_deps)}")
return False
return True
def process_document(self, doc_path):
"""Process a .doc file through all conversion and modification steps"""
try:
logging.info(f"Starting document processing for {doc_path}")
self.last_output.append(f"Starting document processing for {doc_path}")
logging.info("Conversion has started...")
self.last_output.append("Conversion has started...")
# Validate input file
if not os.path.exists(doc_path):
logging.error(f"Input file not found: {doc_path}")
self.last_output.append(f"Input file not found: {doc_path}")
raise FileNotFoundError(f"Input file not found: {doc_path}")
# Get absolute paths
doc_path = os.path.abspath(doc_path)
docx_path = os.path.splitext(doc_path)[0] + '.docx'
# Get the current script directory
script_dir = os.path.dirname(os.path.abspath(__file__))
# Get the Python executable path
if hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix):
python_cmd = sys.executable
else:
python_cmd = 'python3'
logging.info(f"Using Python executable: {python_cmd}")
self.last_output.append(f"Using Python executable: {python_cmd}")
# Change to script directory
original_dir = os.getcwd()
os.chdir(script_dir)
try:
# Step 1: Convert .doc to .docx
logging.info("Starting .doc to .docx conversion...")
self.last_output.append("Starting .doc to .docx conversion...")
logging.info(f"Running command: {[python_cmd, 'doc_to_docx_converter.py', doc_path, docx_path]}")
self.last_output.append(f"Running command: {[python_cmd, 'doc_to_docx_converter.py', doc_path, docx_path]}")
result = subprocess.run(
[python_cmd, 'doc_to_docx_converter.py', doc_path, docx_path],
capture_output=True,
text=True,
check=True,
env=os.environ
)
logging.info(f"Command stdout: {result.stdout}")
self.last_output.append(f"Command stdout: {result.stdout}")
# Confirm successful conversion
logging.info("Conversion completed successfully.")
self.last_output.append("Conversion completed successfully.")
logging.info(f"New files created:")
self.last_output.append(f"New files created:")
logging.info(f"- {docx_path}")
self.last_output.append(f"- {docx_path}")
logging.info(f"- {docx_path.replace('.docx', '_with_headers.docx')}")
self.last_output.append(f"- {docx_path.replace('.docx', '_with_headers.docx')}")
logging.info(f"Location of new files: {os.path.dirname(docx_path)}")
self.last_output.append(f"Location of new files: {os.path.dirname(docx_path)}")
# Small delay to ensure file is ready
time.sleep(1)
# Step 2: Modify table properties
logging.info(f"Running command: {[python_cmd, 'modify_docx_tables.py', docx_path]}")
self.last_output.append(f"Running command: {[python_cmd, 'modify_docx_tables.py', docx_path]}")
result = subprocess.run(
[python_cmd, 'modify_docx_tables.py', docx_path],
capture_output=True,
text=True,
check=True,
env=os.environ
)
logging.info(f"Command stdout: {result.stdout}")
self.last_output.append(f"Command stdout: {result.stdout}")
if result.stderr:
logging.warning(f"Command stderr: {result.stderr}")
self.last_output.append(f"Command stderr: {result.stderr}")
# Step 3: Add table rows
logging.info(f"Running command: {[python_cmd, 'add_table_rows.py', docx_path]}")
self.last_output.append(f"Running command: {[python_cmd, 'add_table_rows.py', docx_path]}")
result = subprocess.run(
[python_cmd, 'add_table_rows.py', docx_path],
capture_output=True,
text=True,
check=True,
env=os.environ
)
logging.info(f"Command stdout: {result.stdout}")
self.last_output.append(f"Command stdout: {result.stdout}")
if result.stderr:
logging.warning(f"Command stderr: {result.stderr}")
self.last_output.append(f"Command stderr: {result.stderr}")
# Step 4: Create renamed copies with headers
logging.info(f"Running command: {[python_cmd, 'rename_docx.py', docx_path]}")
self.last_output.append(f"Running command: {[python_cmd, 'rename_docx.py', docx_path]}")
result = subprocess.run(
[python_cmd, 'rename_docx.py', docx_path],
capture_output=True,
text=True,
check=True,
env=os.environ
)
logging.info(f"Command stdout: {result.stdout}")
self.last_output.append(f"Command stdout: {result.stdout}")
if result.stderr:
logging.warning(f"Command stderr: {result.stderr}")
self.last_output.append(f"Command stderr: {result.stderr}")
logging.info("\n=== Processing Complete ===")
self.last_output.append("\n=== Processing Complete ===")
logging.info(f"Original .doc file: {doc_path}")
self.last_output.append(f"Original .doc file: {doc_path}")
logging.info(f"Intermediate .docx file: {docx_path}")
self.last_output.append(f"Intermediate .docx file: {docx_path}")
logging.info("Final files created with appropriate headers and content modifications.")
self.last_output.append("Final files created with appropriate headers and content modifications.")
return True
finally:
# Restore original directory
os.chdir(original_dir)
return True
except subprocess.CalledProcessError as e:
logging.critical(f"Command failed with exit status {e.returncode}")
self.last_output.append(f"Command failed with exit status {e.returncode}")
logging.critical(f"Command output: {e.stdout}")
self.last_output.append(f"Command output: {e.stdout}")
logging.critical(f"Command error: {e.stderr}")
self.last_output.append(f"Command error: {e.stderr}")
raise
except Exception as e:
logging.critical(f"Document processing failed: {e}")
self.last_output.append(f"Document processing failed: {e}")
logging.critical(traceback.format_exc())
self.last_output.append(traceback.format_exc())
raise
def deactivate_venv(self):
"""Deactivate the virtual environment"""
if 'VIRTUAL_ENV' in os.environ:
# Remove virtual environment from PATH
path = os.environ['PATH'].split(os.pathsep)
venv_path = os.environ['VIRTUAL_ENV']
path = [p for p in path if not p.startswith(venv_path)]
os.environ['PATH'] = os.pathsep.join(path)
# Remove virtual environment from sys.path
if str(Path(venv_path) / 'lib' / f'python{sys.version_info.major}.{sys.version_info.minor}' / 'site-packages') in sys.path:
sys.path.remove(str(Path(venv_path) / 'lib' / f'python{sys.version_info.major}.{sys.version_info.minor}' / 'site-packages'))
# Unset virtual environment variables
del os.environ['VIRTUAL_ENV']
if 'PYTHONHOME' in os.environ:
del os.environ['PYTHONHOME']
# Reload site module to recognize the changes
importlib.reload(site)
logging.info("\nVirtual environment deactivated.")
def main(self):
"""Main entry point for document processing"""
try:
# Check for input file
if len(sys.argv) < 2:
logging.error("Usage: python process_document.py <input_file.doc>")
sys.exit(1)
input_file = sys.argv[1]
# Create and activate virtual environment
self.create_venv()
env, python_path, pip_path = self.activate_venv()
# Install requirements
self.install_requirements(pip_path, env)
# Process document
output_file = self.process_document(input_file)
print(f"Successfully processed document: {output_file}")
# Deactivate virtual environment
self.deactivate_venv()
except Exception as e:
logging.error(f"Processing failed: {e}")
print(f"Error: {e}")
sys.exit(1)
if __name__ == "__main__":
processor = DocumentProcessor()
processor.main()