当前位置:网站首页>Do you charge for PDF merging software? Programmers make one by themselves
Do you charge for PDF merging software? Programmers make one by themselves
2022-06-24 17:24:00 【Programmer fish skin】
lately , Fish skin is in trouble , Need to be right Thousands PDF The documents shall be processed uniformly , For example, delete all PDF The first few pages of 、 Or to all PDF Add cover, etc .
If it's a few files , It can be operated manually one by one , But for thousands of files , We must use software to realize automatic batch processing .
I also searched a lot on the Internet PDF Batch processing software , There are a lot to meet the demand , But the problem is , There is a charge for everything ! and , One is more expensive than the other !
As a programmer , Why not do it yourself 、 Have ample food and clothing , Develop a batch process by yourself PDF File tool ?
To determine the ! I don't just have to do , And live broadcast !
Why do you choose to develop it yourself ?
On the one hand, my needs are relatively simple , In addition, there are now various programming class libraries , Presumably, the cost of self-development will not be very high , At least it is better than manual processing one by one PDF A lot better .
On the other hand, I haven't written gadgets for a long time , It's itchy in my heart , Want to practice .
Of course , And the most important point , It is to bring all the friends in the live studio to experience the fun of programming ~
Let's get started !
The production process
Before that , I don't know what to do PDF Class library of , So it's really starting from scratch research + Development !
The whole development process lasted about half an hour , If you exclude the installation environment 、 Find source file 、 Live communication and other things take up time , Probably 10 Minutes to complete , It's really very simple .
Technology selection
First , To choose which programming language and class library to use to write programs . Mention gadgets and scripts , The first thing I think of is using Python, May be Python There are too many advertisements for file processing, haha . There is one saying. ,Python The grammar of is simple , Rich class library , It is really very convenient to use it to develop gadgets .
How to deal with PDF What about the documents ? There must be a tool class library developed by others on the Internet ! So I went to GitHub A simple search on , Is there any support for PDF modify 、 Merge 、 Page deleted Library , Sure enough, I found it PyPDF2, Read the project introduction , It's perfect for my needs , And the usage is very simple .
therefore , To determine the , Use Python Language + PyPDF2 Development .
install Python And the class library
Python The installation of is very simple , Download the installation package of the corresponding operating system directly from the official website . It should be noted that , In the installation Python when , It is better to check automatic environment variable configuration , It saves the trouble of operation .
Install well Python after , You can enter commands on the console to verify that the installation is successful .
Because our tools and programs depend on PyPDF2 Class library , So use Python Self contained installation tools pip To install it .
pip install PyPDF2
After installing these , Start writing code .
Write code
If you just want to implement two PDF File merge , You don't have to write a single line of code ! because PyPDF2 The class library has already provided us with a file merge Demo, Just copy and paste it directly .
The code is as follows , It's very easy to understand :
from PyPDF2 import PdfFileMerger
# Define a container , Store all pages
merger = PdfFileMerger()
# Read the file
input1 = open("document1.pdf", "rb")
input2 = open("document2.pdf", "rb")
# Insert pdf1 First three pages of
merger.append(fileobj = input1, pages = (0,3))
# Insert pdf2 First page of
merger.merge(fileobj = input2, pages = (0,1))
# Write a new PDF file , Output
output = open("document-output.pdf", "wb")
merger.write(output)We can find any two PDF File to test , Go ahead Demo Make further changes .
After the above code runs successfully , We can support users to input the data to be merged PDF file name , And customize the second one to be spliced PDF Start page number of .
Like the first one PDF Only one cover page , the second PDF The start page of splicing is the second page , The two merge , The replacement is realized PDF The effect of the document cover .
The final code is as follows :
from PyPDF2 import PdfFileMerger, PdfFileReader
merger = PdfFileMerger()
# input Function accepts user input
inputStr1 = input(" Please enter cover ")
inputStr2 = input(" Please enter pdf")
startNum = input(" Please enter pdf The start page ")
input1 = open(inputStr1, "rb")
input2 = open(inputStr2, "rb")
# obtain pdf object
pdf = PdfFileReader(input2)
# Splicing pdf1 First page of
merger.append(fileobj = input1, pages = (0,1))
# Splicing pdf2 Second of ~ Tail page
merger.append(fileobj = input2, pages = (int(startNum), pdf.getNumPages()))
# Write to an output PDF document
output = open("document-output.pdf", "wb")
merger.write(output)All the above parameters can be modified , You can even Develop an interface , Users can set parameters in the input box , This makes the tool easier to use !
debugging
In the process of development , It needs constant debugging , Here we simply verify the results , Observe PDF Whether the merger of is in line with expectations !
Okay , That's all PDF The process of making processing tools . For programmers , Programming languages are the best tools , We can usually use them to solve problems 、 Improve work efficiency , Can also improve their programming skills , Beauty is not true !
Video demo :https://www.bilibili.com/video/BV1zV411Y7AU/
边栏推荐
- Building a cross public chain platform to solve DAPP development problems
- Several schemes of traffic exposure in kubernetes cluster
- Coding enhances security vulnerability scanning capability and helps the team "move left safely"
- New MySQL 8.0 feature - enhanced logical backup recovery
- About with admin option and with grant option
- 重新定义存储架构,华为用了不止5颗“芯”
- TVP experts talk about geese factory middleware: innovating forward and meeting the future
- Today, Tencent safety and SAIC Group officially announced!
- Audio knowledge (I)
- [log service CLS] Tencent cloud game battle engine mgobe accesses CLS
猜你喜欢
随机推荐
Markdown syntax -- Formula
TCB series learning articles - using redis extension in cloud functions
liver failure! My friend made a programming navigation website!
CentOS 7 installing SQL server2017 (Linux)
Try catch finally implementation mechanism
Explore cloudera manager management software tuning (1)
Radiology: contralateral preoperative resting state MRI functional network integration is related to the surgical results of temporal lobe epilepsy
When the game meets NFT, is it "chicken ribs" or "chicken legs"?
Pagoda activities, team members can enjoy a lightweight server 1 core 2g5m 28 yuan for two years
Yiwen teaches you to understand the stack operation in go
Tiktok Kwai, e-commerce enters the same river
集体突破之后,中国公有云的下一步落在哪里?
Quick view of product trends in February 2021
What is the reason for the worse website SEO ranking?
Explanation of pod DNS configuration & cases of DNS resolution failure
实现TypeScript运行时类型检查
Live broadcast Preview - on April 1, I made an appointment with you to explore tcapulusdb with Tencent cloud
What securities dealers recommend? Is it safe to open an account online now?
Following the previous SYSTEMd pit
Create a green city and 3D visualization of digital twin natural gas stations

