Created
January 5, 2025 15:37
-
-
Save dbrant/dd4b845017cd8516b8a818ee84c293a1 to your computer and use it in GitHub Desktop.
Using LibreOffice, convert all WordPerfect files inside a directory (recursively) into Docx format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import subprocess | |
| def is_wordperfect_file(filepath): | |
| with open(filepath, 'rb') as file: | |
| header = file.read(4) | |
| return header == bytes([0xFF, 0x57, 0x50, 0x43]) | |
| def convert_files(source_directory, target_directory): | |
| # Traverse the directory | |
| for root, dirs, files in os.walk(source_directory): | |
| for file in files: | |
| file_path = os.path.join(root, file) | |
| if is_wordperfect_file(file_path): | |
| # Construct target file paths | |
| docx_file_path = file_path + ".docx" | |
| # Calculate relative path from source directory and create corresponding path in target directory | |
| relative_path = os.path.relpath(root, source_directory) | |
| target_subdir = os.path.join(target_directory, relative_path) | |
| os.makedirs(target_subdir, exist_ok=True) | |
| # Adjust the output path for the DOCX file to be in the target directory | |
| target_docx_path = os.path.join(target_subdir, os.path.basename(docx_file_path)) | |
| # Construct the LibreOffice convert command | |
| command = [ | |
| "C:\Program Files\LibreOffice\program\soffice.exe", "--headless", "--convert-to", "docx", | |
| "--outdir", target_subdir, file_path | |
| ] | |
| # Execute the command | |
| subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
| print(f"Converted: {file_path} to {target_docx_path}") | |
| # Specify the source and target directories | |
| source_directory = "./source" | |
| target_directory = "./dest" | |
| convert_files(source_directory, target_directory) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment