- Use
ScriptEditor.app
to create the final fileConvertDOCtoPDF.scpt
- Save the
ConvertDOCtoPDF.scpt
script and set the final path in the Python script - Set the source and output folders in the Python script
- Enjoy!
Last active
May 20, 2024 13:21
-
-
Save nogara/bc92663e18c353d8a6492868af64648d to your computer and use it in GitHub Desktop.
Convert old Microsoft .doc files to PDFs
This file contains 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 convert_doc_to_pdf_applescript(input_file, output_file): | |
script_path = '/path/to/applescript/ConvertDOCtoPDF.scpt' | |
subprocess.call(['osascript', script_path, input_file, output_file]) | |
def convert_all_docs_to_pdfs(source_dir, target_dir): | |
if not os.path.exists(target_dir): | |
os.makedirs(target_dir) | |
convert_batch = 5 | |
i = 0 | |
for file_name in os.listdir(source_dir): | |
if file_name.endswith('.doc'): | |
# check if the pdf is not already created | |
if os.path.exists(os.path.join(target_dir, os.path.splitext(file_name)[0] + '.pdf')): | |
print(f'PDF already exists: {file_name}') | |
continue | |
input_file = os.path.join(source_dir, file_name) | |
output_file = os.path.join(target_dir, os.path.splitext(file_name)[0] + '.pdf') | |
convert_doc_to_pdf_applescript(input_file, output_file) | |
print(f'Converted: {file_name} to PDF') | |
i = i + 1 | |
if i >= convert_batch: | |
print(f'Converted {convert_batch} files, stopping') | |
break | |
if __name__ == '__main__': | |
source_dir = '/folder/with/doc/files' # Path to the source directory containing .doc files | |
target_dir = '/folder/with/pdfs' # Path to the target directory for .pdf files | |
convert_all_docs_to_pdfs(source_dir, target_dir) |
This file contains 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
on run {input_file, output_file} | |
try | |
-- Ensure input and output paths are POSIX paths (macOS paths) | |
set input_path to POSIX file input_file | |
set output_path to POSIX file output_file | |
tell application "Microsoft Word" | |
-- Start Microsoft Word if not already running | |
if not application "Microsoft Word" is running then | |
launch | |
delay 2 | |
end if | |
open input_path | |
set theDoc to the front document | |
save as theDoc file format format PDF file name output_path | |
close theDoc | |
end tell | |
return "Success" | |
on error errMsg number errNum | |
return "Error: " & errMsg & " (" & errNum & ")" | |
end try | |
end run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment