OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python: Convert several(multiple) docx files to pdf from a particular folder

  • Thread starter Thread starter Hellena Crainicu
  • Start date Start date
H

Hellena Crainicu

Guest
I want to convert multiple/several files from .docx to PDF using Python. I made a code, but is not working. I am beginner. Can anyone help me a little bit?

Code:
import re
import os
from pathlib import Path
import sys
from docx2pdf import convert

# The location where the files are located
input_path = r'c:\Folder7\input'
# The location where we will write the PDF files
output_path = r'c:\Folder7\output'
# Creeaza structura de foldere daca nu exista
os.makedirs(output_path, exist_ok=True)

# Verifica existenta folder-ului
directory_path = Path(input_path)
if directory_path.exists() and directory_path.is_dir():
    print(directory_path, "exists")
else:
    print(directory_path, "is invalid")
    sys.exit(1)

for file_path in directory_path.glob("*"):
    # file_path is a Path object

    print("Procesez fisierul:", file_path)
    document = Document()
    # file_path.name is the name of the file as str without the Path
    document.add_heading(file_path.name, 0)

    file_content = file_path.read_text(encoding='UTF-8')
    document.add_paragraph(file_content)

    # build the new path where we store the files
    output_file_path = os.path.join(output_path, file_path.name + ".pdf")

    document.save(output_file_path)
    print("Am convertit urmatorul fisier:", file_path, "in: ", output_file_path)

I get this error:

Code:
Traceback (most recent call last):
  File "D:\Convert docx to pdf.py", line 26, in <module>
    document = Document()
NameError: name 'Document' is not defined

I am beginner. Can anyone help me to make this code working ?
<p>I want to convert multiple/several files from .docx to PDF using Python. I made a code, but is not working. I am beginner. Can anyone help me a little bit?</p>
<pre><code>import re
import os
from pathlib import Path
import sys
from docx2pdf import convert

# The location where the files are located
input_path = r'c:\Folder7\input'
# The location where we will write the PDF files
output_path = r'c:\Folder7\output'
# Creeaza structura de foldere daca nu exista
os.makedirs(output_path, exist_ok=True)

# Verifica existenta folder-ului
directory_path = Path(input_path)
if directory_path.exists() and directory_path.is_dir():
print(directory_path, "exists")
else:
print(directory_path, "is invalid")
sys.exit(1)

for file_path in directory_path.glob("*"):
# file_path is a Path object

print("Procesez fisierul:", file_path)
document = Document()
# file_path.name is the name of the file as str without the Path
document.add_heading(file_path.name, 0)

file_content = file_path.read_text(encoding='UTF-8')
document.add_paragraph(file_content)

# build the new path where we store the files
output_file_path = os.path.join(output_path, file_path.name + ".pdf")

document.save(output_file_path)
print("Am convertit urmatorul fisier:", file_path, "in: ", output_file_path)
</code></pre>
<p><strong>I get this error:</strong></p>
<pre><code>Traceback (most recent call last):
File "D:\Convert docx to pdf.py", line 26, in <module>
document = Document()
NameError: name 'Document' is not defined
</code></pre>
<p>I am beginner. Can anyone help me to make this code working ?</p>
 

Latest posts

Top