I installed Libreoffice on my win system and able to convert the .docx
file to pdf
.
import os
import sys
newdir = os.path.abspath(os.path.join(os.path.dirname(__file__)))
if not os.path.exists(newdir):
os.makedirs(newdir)
file_name = os.path.join(newdir, 'test.docx')
print(file_name)
pdf_filename = file_name.split(".docx")[0] + ".pdf"
pdf_file = os.path.join(pdf_filename)
from subprocess import Popen
if sys.platform == 'darwin':
LIBRE_OFFICE = '/Applications/LibreOffice.app/Contents/MacOS/soffice'
elif sys.platform == 'win32':
LIBRE_OFFICE = 'C:/Program Files/LibreOffice/program/soffice'
else:
LIBRE_OFFICE = 'libreoffice'
def convert_to_pdf(input_docx, out_folder):
p = Popen([LIBRE_OFFICE, '--convert-to', 'pdf', '--outdir',
out_folder, input_docx])
print([LIBRE_OFFICE, '--convert-to', 'pdf', input_docx])
out = p.communicate()
sample_doc = file_name
out_folder = newdir
convert_to_pdf(sample_doc, out_folder)
The pdf is not aligned properly and the left and right margin is also missing. Can anyone please tell if we can improve or maintain the alignment and styling in libreoffice while converting a docx file to pdf ?