Skip to content

Instantly share code, notes, and snippets.

@dbrant
Created January 5, 2025 15:41
Show Gist options
  • Select an option

  • Save dbrant/59d97a19465a0796d8613a3f19aac679 to your computer and use it in GitHub Desktop.

Select an option

Save dbrant/59d97a19465a0796d8613a3f19aac679 to your computer and use it in GitHub Desktop.
Using LibreOffice, convert all Macintosh WriteNow files inside a directory (recursively) into Docx format.
import os
import subprocess
def is_writenow_file(filepath):
with open(filepath, 'rb') as file:
header = file.read(8)
return header == bytes([0x57, 0x72, 0x69, 0x74, 0x65, 0x4E, 0x6F, 0x77])
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_writenow_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
]
print("Running: " + 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