Python docx2pdf cannot convert docx to pdf when bundled with pyinstaller

283 views Asked by At

I am using Customtkinter to build GUI which reads data from an excel file, then creates a word document with this file, converts it to pdf and encrypts it. I am using the python-docx module to write the word document, then the convert function of the docx2pdf module to convert the docx file to pdf. This works very well when the code is run in PyCharm or Jupyter Notebook. When the code is bundled with pyinstaller as an exe, this function does not work and the error produced from the log file is

'NoneType' object has no attribute 'write'

The code block for this part of the GUI is below

## this function has been defined at the top of the script
def get_script_directory():
    return os.path.dirname(os.path.abspath(sys.argv[0]))

## some relevant modules imported
from datetime import datetime
from docx2pdf import convert
from docx import Document
from docx.oxml.ns import qn
from docx.oxml.shared import OxmlElement
from docx.shared import Inches, Pt
from pikepdf import Pdf, Encryption
from spire.doc import Document as spiredoc, FileFormat
from tkinter import messagebox

import docx, io, sys

## portion of code to generate pdf from word document
folder = fr'\{month.title()} {year} Payslips'

doc.save(get_script_directory() + fr"{folder}\{name} {month} {year} Payslip.docx")
try:
    convert(get_script_directory() + fr"{folder}\{name} {month} {year} Payslip.docx",
            get_script_directory() + fr"{folder}\{name} {month} {year} Payslip.pdf")
except Exception as Argument:
    logging.warning(f'{datetime.now()}: {Argument}')
    raise
file_name = get_script_directory() + fr"{folder}\{name} {month} {year} Payslip.pdf"
pdf = Pdf.open(file_name)
try:
    password = str(staff_details.loc[name, 'PASSWORDS'])
except:
    password = name.title().split()[0].casefold()
pdf.save(get_script_directory() + fr"\{folder}\{name} {month} {year} Encrypted Payslip.pdf",
         encryption=Encryption(owner=password, user=password, R=4))
pdf.close()

I changed to the aspose and the spire.doc modules but I get the error

'NoneType' object has no attribute 'Document_CreateDocument'.

Also those put watermarks on the pdf. I would like to avoid these as much as possible and only use them as a last resort.

0

There are 0 answers