|
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'): |
|
|
|
|
|
os.makedirs(outdir, exist_ok=True) |
|
|
|
|
|
cmd = [ |
|
LIBREOFFICE_PATH, |
|
'--convert-to', 'xlsx', |
|
input_file, |
|
'--outdir', outdir, |
|
'--headless' |
|
] |
|
|
|
|
|
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): |
|
|
|
os.makedirs(outdir, exist_ok=True) |
|
|
|
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor: |
|
|
|
future_to_file = { |
|
executor.submit(convert_xls_to_xlsx, input_file, outdir): input_file |
|
for input_file in input_files |
|
} |
|
|
|
|
|
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") |