I’m encountering an issue with the docx2pdf library while trying to convert a DOCX file to PDF.
The conversion process completes, but I receive the following error:
2024-08-02 11:44:26,978 – ERROR – Erreur lors de la conversion DOCX en PDF : ‘NoneType’ object has no attribute ‘write’
import os
import logging
from docx2pdf import convert
# Configure logging
logging.basicConfig(
filename='conversion.log',
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def convert_docx_to_pdf(docx_path, output_path):
logging.debug(f"Début de la conversion : {docx_path} -> {output_path}")
try:
# Check if DOCX file exists
if not os.path.exists(docx_path):
raise FileNotFoundError(f"Le fichier DOCX spécifié n'existe pas : {docx_path}")
# Convert DOCX to PDF
convert(docx_path, output_path)
# Check if PDF file was created
if not os.path.exists(output_path):
raise FileNotFoundError(f"Le fichier PDF n'a pas été créé : {output_path}")
logging.info(f"Conversion réussie : {output_path}")
except FileNotFoundError as e:
logging.error(f"Erreur de fichier : {e}")
except PermissionError:
logging.error(f"Erreur de permission lors de la conversion : {docx_path}")
except Exception as e:
logging.error(f"Erreur lors de la conversion DOCX en PDF : {e}")
def main():
docx_file = r'C:UsershbabDesktopdocument.docx'
pdf_from_docx = r'C:UsershbabDesktopdocument.pdf'
logging.debug(f"Début de la conversion de {docx_file} vers {pdf_from_docx}")
convert_docx_to_pdf(docx_file, pdf_from_docx)
if os.path.exists(pdf_from_docx):
logging.info(f"Le fichier PDF a été créé avec succès : {pdf_from_docx}")
else:
logging.error(f"Le fichier PDF n'a pas été créé : {pdf_from_docx}")
if __name__ == '__main__':
main()
Name: docx2pdf
Version: 0.1.8
Summary: Convert docx to pdf on Windows or macOS directly using Microsoft Word (must be installed).
Home-page: https://github.com/AlJohri/docx2pdf
Author: Al Johri
Author-email: [email protected]
License: MIT
Location: C:UsershbabAppDataLocalProgramsPythonPython312Libsite-packages
Requires: pywin32, tqdm
Required-by:
Has anyone encountered this issue before? How can I resolve the ‘NoneType’ object has no attribute ‘write’ error in docx2pdf? Any help or insights would be greatly appreciated!
I tried using the docx2pdf library to convert a DOCX file to PDF.
Hbab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.