Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions docx2pdf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
import json
import subprocess
Expand All @@ -12,7 +13,6 @@

__version__ = version(__package__)


def windows(paths, keep_active):
import win32com.client

Expand Down Expand Up @@ -45,17 +45,16 @@ def windows(paths, keep_active):
if not keep_active:
word.Quit()


def macos(paths, keep_active):
script = (Path(__file__).parent / "convert.jxa").resolve()
def linux(paths, keep_active):
# Use LibreOffice for conversion on Linux
cmd = [
"/usr/bin/osascript",
"-l",
"JavaScript",
str(script),
str(paths["input"]),
str(paths["output"]),
str(keep_active).lower(),
"libreoffice",
"--headless",
"--convert-to",
"pdf",
"--outdir",
paths["output"],
paths["input"]
]

def run(cmd):
Expand All @@ -79,7 +78,6 @@ def run(cmd):
print(msg)
sys.exit(1)


def resolve_paths(input_path, output_path):
input_path = Path(input_path).resolve()
output_path = Path(output_path).resolve() if output_path else None
Expand All @@ -105,19 +103,19 @@ def resolve_paths(input_path, output_path):
output["output"] = output_path
return output


def convert(input_path, output_path=None, keep_active=False):
paths = resolve_paths(input_path, output_path)
if sys.platform == "darwin":
return macos(paths, keep_active)
elif sys.platform == "win32":
return windows(paths, keep_active)
elif sys.platform.startswith("linux"):
return linux(paths, keep_active)
else:
raise NotImplementedError(
"docx2pdf is not implemented for linux as it requires Microsoft Word to be installed"
"docx2pdf is not implemented for this platform"
)


def cli():

import textwrap
Expand Down Expand Up @@ -176,3 +174,4 @@ def cli():
args = parser.parse_args()

convert(args.input, args.output, args.keep_active)