File size: 1,693 Bytes
a555f47 |
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 |
import subprocess
import os
import concurrent.futures
LIBREOFFICE_PATH = "/Applications/LibreOffice.app/Contents/MacOS/soffice"
def convert_xls_to_xlsx(input_file, outdir='clean_data'):
# Ensure output directory exists
os.makedirs(outdir, exist_ok=True)
# Construct the LibreOffice command
cmd = [
LIBREOFFICE_PATH,
'--convert-to', 'xlsx',
input_file,
'--outdir', outdir,
'--headless'
]
# Execute the command
try:
result = subprocess.run(cmd)
print(result)
print(f"Successfully converted {input_file} to xlsx format")
except subprocess.CalledProcessError as e:
print(f"Error converting file: {e}")
raise
def parallelize_convert_xls_to_xlsx(input_files, outdir='clean_data', max_workers=None):
# Create output directory if it doesn't exist
os.makedirs(outdir, exist_ok=True)
# Use ThreadPoolExecutor for parallel processing
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
# Submit all conversion tasks
future_to_file = {
executor.submit(convert_xls_to_xlsx, input_file, outdir): input_file
for input_file in input_files
}
# Process completed conversions
for future in concurrent.futures.as_completed(future_to_file):
input_file = future_to_file[future]
try:
future.result()
except Exception as e:
print(f"Conversion failed for {input_file}: {str(e)}")
if __name__ == "__main__":
convert_xls_to_xlsx('original_data/VEnron2/VEnron2/1/1_11_07act.xls', "clean_data") |