Search code examples
pythonpython-docx

How to make code of converting Docx to PDF in this code?


import aspose.slides as slides    
import time
from tkinter import filedialog, Tk
import os
import aspose.words as aw

root = Tk()
files = filedialog.askopenfilenames(parent = root, title = "Choose files")
files = root.tk.splitlist(files)
start = time.time()
for f in files:
    ext = os.path.splitext(f)
    if ext == ".ppt":
        pres = slides.Presentation(f)
        pres.save(f+".pdf", slides.export.SaveFormat.PDF)
    elif ext == ".docx":
        doc = aw.Document(f)     #problem part
        doc.save(f+".pdf")
end = time.time()
print("Time Taken:%f secs" %(end-start))

This is the code of converting ppt and docx to PDF.
I am trying to make the system to check whether the extension of selected files from file dialog are pptx or docx and do correct process.
But when I tried to make code of docx part, converting docx to pdf didn't work. I tried multiple times with different libraries.(docx2pdf, aspose)
Could someone help me out with this problem? Thank you


Solution

  • The main reason your program is not work is its not getting into the if, elif, else statement.

    ext = os.path.splitext(f) returns a list for example [C:\\hello\\filename",".docx"] but you are comparing whole list i.e if ext==".doc"`, which obviously returns false.

    Here's what you can do:

    • change if ext==".docx" to if ".docx" in ext. or,
    • change if ext==".docx" to if ext[1] == ".docx".