- Converting docx to pdf with pure python (on linux, without libreoffice)
- Convert Word DOCX or DOC to PDF in Python
- Python Library for Word to PDF Conversion — Free Download#
- Convert Word DOCX to PDF in Python#
- Python Word to PDF with a Particular Standard#
- Python DOCX to PDF — Convert Range of Pages#
- DOC DOCX to PDF in Python — Apply Image Compression#
- Python DOCX to PDF Library — Get a Free Library License#
- Conclusion#
- See Also#
- docx2pdf 0.1.8
- Навигация
- Ссылки проекта
- Статистика
- Метаданные
- Сопровождающие
- Классификаторы
- Описание проекта
- docx2pdf
- Install
- CLI
- Library
- Jupyter Notebook
Converting docx to pdf with pure python (on linux, without libreoffice)
Another one you could use is libreoffice, however as the first responder said the quality will never be as good as using the actual comtypes.
anyways, after you have installed libreoffice, here is the code to do it.
from subprocess import Popen LIBRE_OFFICE = r"C:\Program Files\LibreOffice\program\soffice.exe" def convert_to_pdf(input_docx, out_folder): p = Popen([LIBRE_OFFICE, '--headless', '--convert-to', 'pdf', '--outdir', out_folder, input_docx]) print([LIBRE_OFFICE, '--convert-to', 'pdf', input_docx]) p.communicate() sample_doc = 'file.docx' out_folder = 'some_folder' convert_to_pdf(sample_doc, out_folder)
The PythonAnywhere help pages offer information on working with PDF files here: https://help.pythonanywhere.com/pages/PDF
Summary: PythonAnywhere has a number of Python packages for PDF manipulation installed, and one of them may do what you want. However, shelling out to abiword seems easiest to me. The shell command abiword —to=pdf filetoconvert.docx will convert the docx file to a PDF and produce a file named filetoconvert.pdf in the same directory as the docx. Note that this command will output an error message to the standard error stream complaining about XDG_RUNTIME_DIR (or at least it did for me), but it still works, and the error message can be ignored.
Here is docx to pdf code for linux (for windows just download libreoffice and put soffice path instead of soffice)
import subprocess def generate_pdf(doc_path, path): subprocess.call(['soffice', # '--headless', '--convert-to', 'pdf', '--outdir', path, doc_path]) return doc_path generate_pdf("docx_path.docx", "output_path")
Convert Word DOCX or DOC to PDF in Python
Word to PDF is one of the most popular and immensely performed document conversions. The DOCX or DOC files are converted to PDF format before they are printed or shared. In this article, we will automate Word to PDF conversion in Python. The steps and code samples will demonstrate how to convert Word DOCX or DOC to PDF with Python. Also, you will learn about different options to customize Word to PDF conversion.
Python Library for Word to PDF Conversion — Free Download#
For converting Word documents to PDF format, we will use Aspose.Words for Python. It is a feature-rich Python library for creating and manipulating Word documents. Moreover, it lets you convert DOCX and DOC files to PDF format with high fidelity. The library is hosted on PyPI and you can install it using the following pip command.
Convert Word DOCX to PDF in Python#
The following are the steps to convert a Word document to PDF in Python.
- Load the Word document using Document class.
- Convert Word document to PDF using Document.save() method.
The following code sample shows how to convert a Word DOCX file to PDF.
Python Word to PDF with a Particular Standard#
You can also specify the particular standard for the converted PDF document such as PDF/A. The following are the steps to specify the PDF standard in Word to PDF conversion using Python.
- Load the Word document using Document class.
- Create an object of PdfSaveOptions class and set PDF standard using PdfSaveOptions.compliance property.
- Convert Word document to PDF using Document.save() method.
The following code sample shows how to set a particular standard in Word DOCX to PDF conversion.
Python DOCX to PDF — Convert Range of Pages#
You can also specify the range of pages you want to convert to PDF format. For this, you can use PdfSaveOptions.page_set property. The following code sample shows how to convert a range of pages in Word document to PDF.
DOC DOCX to PDF in Python — Apply Image Compression#
Aspose.Words for Python also lets you apply image compression in the converted PDF document. In addition, you can specify the JPEG quality for the images. The following are the steps to set image compression while converting a Word DOCX to PDF in Python.
- Load the Word document using Document class.
- Create an object of PdfSaveOptions class.
- Set image compression using PdfSaveOptions.image_compression property.
- Set JPEG quality using PdfSaveOptions.jpeg_quality property.
- Convert Word document to PDF using Document.save() method.
The following code sample shows how to set image compression in Word to PDF conversion.
Python DOCX to PDF Library — Get a Free Library License#
You can get a temporary license in order to use Aspose.Words for Python without evaluation limitations.
Conclusion#
In this article, you have learned how to convert Word DOCX or DOC files to PDF in Python. Moreover, you have seen different options to customize the DOC or DOCX to PDF conversion in Python. You can learn more about Aspose.Words for Python using documentation. In case you would have any questions, feel free to let us know via our forum.
See Also#
docx2pdf 0.1.8
Convert docx to pdf on Windows or macOS directly using Microsoft Word (must be installed).
Навигация
Ссылки проекта
Статистика
Метаданные
Лицензия: MIT License (MIT)
Требует: Python >=3.5
Сопровождающие
Классификаторы
Описание проекта
docx2pdf
Convert docx to pdf on Windows or macOS directly using Microsoft Word (must be installed).
On Windows, this is implemented via win32com while on macOS this is implemented via JXA (Javascript for Automation, aka AppleScript in JS).
Install
brew install aljohri/-/docx2pdf
CLI
usage: docx2pdf [-h] [--keep-active] [--version] input [output] Example Usage: Convert single docx file in-place from myfile.docx to myfile.pdf: docx2pdf myfile.docx Batch convert docx folder in-place. Output PDFs will go in the same folder: docx2pdf myfolder/ Convert single docx file with explicit output filepath: docx2pdf input.docx output.docx Convert single docx file and output to a different explicit folder: docx2pdf input.docx output_dir/ Batch convert docx folder. Output PDFs will go to a different explicit folder: docx2pdf input_dir/ output_dir/ positional arguments: input input file or folder. batch converts entire folder or convert single file output output file or folder optional arguments: -h, --help show this help message and exit --keep-active prevent closing word after conversion --version display version and exit
Library
See CLI docs above (or in docx2pdf --help ) for all the different invocations. It is the same for the CLI and python library.
Jupyter Notebook
If you are using this in the context of jupyter notebook, you will need ipywidgets for the tqdm progress bar to render properly.
pip install ipywidgets jupyter nbextension enable --py widgetsnbextension ``