Home >Backend Development >Python Tutorial >How to generate word and PDF documents using python program
This article mainly introduces you to the method of using python program to generate word and PDF documents. The article gives a detailed introduction and sample code. I believe it has certain reference value for everyone. Friends in need can join us below. Let's see.
1. How to export word documents through the program
Export web/html content into world documents. There are many solutions in Java. For example, using various methods such as Jacob, Apache POI, Java2Word, iText, etc., as well as using template engines such as freemarker. There are also some corresponding methods in PHP, but there are very few ways to generate world documents from web/html content in Python. The most difficult thing to solve is how to use js code to asynchronously obtain the filled data and export the pictures into a word document.
1. unoconv
Function:
#1. Supports converting local html documents to Documents in docx format, so you need to save the html files in the web page locally and then call unoconv for conversion. The conversion effect is also good and the method of use is very simple.
\# 安装 sudo apt-get install unoconv \# 使用 unoconv -f pdf *.odt unoconv -f doc *.odt unoconv -f html *.odt
Disadvantages:
1. Only static html can be converted. For pages with Where ajax is used to obtain data asynchronously, it cannot be converted (mainly to ensure that there is data in the html file saved from the web page).
2. Only html can be converted. If there are pictures generated using echarts, highcharts and other js codes in the page, these pictures cannot be converted into word documents;
3. The content format of the generated word document is not easy to control.
2. python-docx
Function:
1.python- docx is a python library that can read and write word documents.
Usage:
1. Obtain the data from the web page and add it to the word document using python manual typesetting.
from docx import Document from docx.shared import Inches document = Document() document.add_heading('Document Title', 0) p = document.add_paragraph('A plain paragraph having some ') p.add_run('bold').bold = True p.add_run(' and some ') p.add_run('italic.').italic = True document.add_heading('Heading, level 1', level=1) document.add_paragraph('Intense quote', style='IntenseQuote') document.add_paragraph( 'first item in unordered list', style='ListBullet' ) document.add_paragraph( 'first item in ordered list', style='ListNumber' ) document.add_picture('monty-truth.png', width=Inches(1.25)) table = document.add_table(rows=1, cols=3) hdr_cells = table.rows[0].cells hdr_cells[0].text = 'Qty' hdr_cells[1].text = 'Id' hdr_cells[2].text = 'Desc' for item in recordset: row_cells = table.add_row().cells row_cells[0].text = str(item.qty) row_cells[1].text = str(item.id) row_cells[2].text = item.desc document.add_page_break() document.save('demo.docx')
from docx import Document from docx.shared import Inches document = Document() for row in range(9): t = document.add_table(rows=1,cols=1,style = 'Table Grid') t.autofit = False #很重要! w = float(row) / 2.0 t.columns[0].width = Inches(w) document.save('table-step.docx')
Disadvantages:
Features Very weak. There are many limitations, such as not supporting templates, etc., and can only generate simple format word documents.
2. Method of exporting PDF documents from the program
1.pdfkit
Function:
1.wkhtmltopdf is mainly used to generate PDF from HTML.
2.pdfkit is a python package based on wkhtmltopdf, which supports the conversion of URLs, local files, and text content to PDF. It ultimately calls the wkhtmltopdf command. It is the best python that I have come across so far to generate pdf.
Advantages:
1.wkhtmltopdf: Convert HTML to PDF using webkit kernel
Webkit is an efficient, open source The browser kernel, which is used by browsers including Chrome and Safari. Chrome's function of printing the current web page has an option to directly "save as PDF".
2.wkhtmltopdf uses the PDF rendering engine of the webkit core to convert HTML pages to PDF. High fidelity, great conversion quality, and very easy to use.
Usage:
\# 安装 pip install pdfkit \# 使用 import pdfkit pdfkit.from_url('http://google.com', 'out.pdf') pdfkit.from_file('test.html', 'out.pdf') pdfkit.from_string('Hello!', 'out.pdf')
Disadvantages:
1. For js codes such as echarts and highcharts The generated icon cannot be converted to pdf (because its function is mainly to convert html to pdf, not js to pdf). The conversion effect for purely static pages is still good.
2. Others
Other plug-ins for generating pdf include: weasyprint, reportlab, PyPDF2, etc. After simple tests, they are not as effective as pdfkit, and some are complicated to use.
For more related articles on how to use python programs to generate word and PDF documents, please pay attention to the PHP Chinese website!