Use python local file conversion|pdf and docx format mutual conversion|pdf2docx docx2pdf library

1. Introduction to PDF to Word

There are many online pdf to word and word to pdf sites online, but their security is unknown, and it is very unsafe to upload files to the Internet if they contain sensitive information.

The pdf2docx library in python allows you to natively convert PDF files to .docx documents that can be opened and edited in Microsoft Word; correspondingly, the docx2pdf library allows you to natively convert Word files to PDF files.

2. Install the pdf2docx / docx2pdf library

pip install pdf2docx
pip install docx2pdf

3. File format conversion python code

Here is a sample code to convert a PDF file to a Word file using Python and libraries:

############## PDF 转 Word ##############
# Import the necessary libraries
from pdf2docx import parse

# Specify the path of the PDF file to be converted
pdf_path = "path/to/pdf_file.pdf"

# Specify the path and filename of the Word file to be created
docx_path = "path/to/docx_file.docx"

# Convert the PDF file to a Word file
parse(pdf_path, docx_path)

############## Word 转 PDF ##############

from docx2pdf import convert

# Specify the path of the PDF file to be converted
pdf_path = "path/to/pdf_file.pdf"

# Specify the path and filename of the Word file to be created
docx_path = "path/to/docx_file.docx"

# Convert the Word file to a PDF file
convert(docx_path, pdf_path)

When using pdf_path, docx_pathreplace and with the path of your own file.

that's all.

Guess you like

Origin blog.csdn.net/qq_41608408/article/details/129827205