Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert Word to PDF by Python

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "Python how to achieve Word to PDF", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Python how to achieve Word to PDF" bar!

Pdf Toolkit-pdfkit

Installation of the pdfkit package:

Pip install pdfkit

Dependent tools:

Download compliance with your current system installation package after the completion of the installation can achieve the effect of compatibility.

Html to pdf

The method of converting html to pdf:

Pdfkit.from_file (html file, save path) uses the pdfkit.from_file () function to pass in the save path of "html" file and pdf

The code example is as follows:

# coding:utf-8import pdfkit # need to install pdfkit third party package "pip install pdfkit" and third party dependency "wkhtmltopdf" pdfkit.from_file ('html test file .html', 'html test file .pdf')

The running results are as follows:

The URL is transferred to pdf.

The method of transferring the website to pdf:

Pdfkit.from_url (URL, save path) use the pdfkit.from_url () function to pass in the "URL" file and the save path of pdf

The difference between the "html" file and the URL is that in fact, the html file may be generated by our local development, or it may be stored locally by "save the web page as". So the URL is a little different from the html file, but their essence is the same.

The code example is as follows:

# coding:utf-8import pdfkit # need to install pdfkit third party package "pip install pdfkit" and third party dependency "wkhtmltopdf" pdfkit.from_url ('https://www.163.com',' test1.pdf')

The running results are as follows:

String generation pdf

The method of transferring the website to pdf:

Pdfkit.from_string (string based on html, save path) uses the pdfkit.from_string () function to pass in the "URL" file and the save path of pdf

The string based on html is actually a hypertext file format of the front end, and the string generated by this front end specification is actually the string of html.

# coding:utf-8import pdfkit # need to install pdfkit third party package "pip install pdfkit" and third party dependency "wkhtmltopdf" html = "

Hello, this is a test file that converts html strings to pdf.

"" pdfkit.from_string (html, 'html_string_test.pdf')

The running results are as follows:

Transfer word to html and then to pdf combined with pydocx

First you need to install the pydocx dependency package-> pip install pydocx

Import PyDocX function-> from pydocx import PyDocX

Convert the word file to html format using PyDocX (will generate a string object of html)

Write the generated html string to a html file

Then use the pdfkit.from_file () function of the pdfkit package to convert it into a pdf file

The code example is as follows:

# coding:utf-8import pdfkit # pip install pdfkitfrom pydocx import PyDocX # pip install pydocxhtml = PyDocX.to_html ('resume 1.docx') f = open (' resume 1.htmlresume,'w') f.write (html) f.close () # pdfkit.from_file ('html1.html',' test3.pdf') pdfkit.from_string (html, 'resume 1.pdf')

The running results are as follows:

Thank you for your reading, the above is the content of "how to achieve Word to PDF in Python". After the study of this article, I believe you have a deeper understanding of how to achieve Word to PDF in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report