Click on the Upload button to locate a file from a computer. Once the file is converted you can download it directly on the website. Uploaded files are sent to the servers and deleted within 24 hours after conversion. In the above code, we create the output file with file = open(outputFile, "w") before calling the convert() function.PDF to Word converter is a web-based tool and you need to have internet connection to be able to use it. We can automate this process as we did in the previous section using file handling.
#PDC TO DOCX CODE#
The only drawback of this code is that we still need to create the output file before executing this code. We converted document.docx to document.pdf with the convert() function of docx2pdf package in the above code.
#PDC TO DOCX HOW TO#
The following code example shows us how to convert a docx file to a pdf file with the docx2pdf package.
#PDC TO DOCX INSTALL#
The command to install docx2pdf package is given below. The docx2pdf is also an external package. The docx2pdf package provides us simple functions that take the file names and take care of all the low-level conversion stuff discussed in the previous section. If we need to quickly convert a docx file to a pdf file without worrying too much about any low-level details, we can use the docx2pdf package in Python. The only drawback is that we have to write a lot of code for it. The pywin32 method works just fine and gives us a lot of control over the nitty-gritty details. Convert Docx to PDF With the docx2pdf Package in Python In the above code, we create the output file with file = open(outputFile, "w") before opening Microsoft Word with the win32com.client library. The following code snippet shows how we can further automate this whole process. This process can also be automated with the help of file handling in Python. This means that we have to manually create a file named document.pdf before executing the above code. Notice that the output file must already be created for this code to work properly. In the end, we closed the opened document with doc.Close() function and exited Microsoft Word with word.Quit() function. We opened the docx file with doc = (inputFile) and saved it as a pdf file with doc.SaveAs(outputFile, FileFormat=wdFormatPDF).
We converted the document.docx to document.pdf with the win32com.client library in the above code.
Word = ('Word.Application')ĭoc.SaveAs(outputFile, FileFormat=wdFormatPDF) OutputFile = os.path.abspath("document.pdf") InputFile = os.path.abspath("document.docx") The following code example shows us how to convert a docx file to a pdf file with the pywin32 package. We can use the Microsoft Word application with this package to open the docx file and save it as a pdf file. The command to install pywin32 is given below. As it is an external package, we have to install pywin32 before using it. The pywin32 package is generally used for creating and initializing COM objects and using windows services in Python. Convert Docx to PDF With the pywin32 Package in Python This tutorial will discuss the methods to convert a docx file to a pdf file in Python. Convert Docx to PDF With the docx2pdf Package in Python.Convert Docx to PDF With the pywin32 Package in Python.