【轉】Python編程: 多個PDF文件合並以及網頁上自動下載PDF文件


1. 多個PDF文件合並
1.1 需求描述
有時候,我們下載了多個PDF文件, 但希望能把它們合並成一個PDF文件。例如:你下載的數個PDF文件資料或者電子發票,你可以使用python程序合並成一個PDF文件,無論是閱讀或是打印都更方便些。

1.2. 技術分析
首先,我們要讀取某一個目錄(為了簡化,我們假設Python代碼和PDF文件都放在此目錄下)的所有PDF文件,然后調用 PdfFileMerger 庫進行合並,最后打印輸出文件完成。

1.3. 代碼實現
remove_pdf_file(file): 刪除一個pdf 文件,主要用來刪除合並生成的pdf文件
get_all_pdf_files(path): 讀取路徑path下所有的pdf文件,返回一個列表
merge_pdf_file(pdfs): 把在列表pdfs里包含的多個pdf文件合並成一個pdf文件 merged_pdf_file.pdf
Python 代碼

# merge_pdf_files.py

from PyPDF2 import PdfFileMerger
import os, sys

def remove_pdf_file(file):
os.remove(file)

def get_all_pdf_files(path):
pdfs = [ file for file in os.listdir(path) if '.pdf' in file ]
return pdfs

def merge_pdf_file(pdfs):
pdf_file_merger = PdfFileMerger()
merged_pdf = 'merged_pdf_file.pdf'

for pdf in pdfs:
if merged_pdf == pdf:
remove_pdf_file(pdf)
try:
pdf_file_merger.append(open(pdf, 'rb'))
except:
print("merging pdf file %s failed." % pdf)

with open(merged_pdf, 'wb') as fout:
pdf_file_merger.write(fout)

return merged_pdf

def main():
pdfs = get_all_pdf_files(sys.path[0]) # current path

print('The file', merge_pdf_file(pdfs), 'is generated.')

if __name__ == "__main__":
main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2. 網頁上自動下載多個PDF文件並合並PDF文件
2.1 需求描述
如果有一個新需求是從某個網頁上自動下載多個PDF文件,最后把多個PDF文件合並成一個PDF文件,該如何實現呢?

2.2 技術分析
我們使用requests庫來抓取網頁,使用正則表達式分析網頁上的所有PDF文件,然后使用requests庫把所有的PDF文件自動下載下來,最后把多個PDF文件合並生成一個文件merged_pdf_file.pdf

2.3 代碼實現
get_page_source(url): 讀取網頁的內容
get_all_pdfs_url(html): 分析網頁內容找到所有PDF文件的列表
download_all_pdf_files(url, pdfs): 從網上下載所有PDF文件到當前目錄
remove_pdf_file(file): 刪除一個pdf 文件,主要用來刪除合並生成的pdf文件
remove_pdf_file(pdfs): 刪除列表里的pdf 文件,主要用來刪除臨時下載的pdf文件
merge_pdf_file(pdfs): 把在列表pdfs里包含的多個pdf文件合並成一個pdf文件merged_pdf_file.pdf
Python 代碼

# download_merge_pdf_files.py

import re
import requests
import sys
import os
from PyPDF2 import PdfFileMerger

def get_page_source(url):
r = requests.get(url)
return r.text

def get_all_pdfs_url(html):
all_files = re.findall('<li><a href=(.*?)class="title">', html, re.S)

return [item.strip()[1:-1] for item in all_files if "pdf" in item]

def download_all_pdf_files(url, pdfs):
print("The following pdf files are downloaded from", url)
for index, pdf in enumerate(pdfs, 1):
print("%d. %s" %(index, pdf))

response = requests.get(url + pdf , stream=True)

try:
new_pdf_file = str(index)+'. '+pdf
with open(new_pdf_file, 'wb') as handle:
for block in response.iter_content(1024):
handle.write(block)
except:
print("writing pdf file %s failed." % new_pdf_file)

def merge_pdf_files(pdfs):
pdf_file_merger = PdfFileMerger()
merged_pdf = 'merged_pdf_file.pdf'

for index, pdf in enumerate(pdfs, 1):
if merged_pdf == pdf:
remove_pdf_file(pdf)
try:
new_pdf_file = str(index)+'. '+pdf
pdf_file_merger.append(open(new_pdf_file, 'rb'))
except:
print("merging pdf file %s failed." % new_pdf_file)

with open(merged_pdf, 'wb') as fout:
pdf_file_merger.write(fout)

return merged_pdf

def remove_pdf_file(file):
os.remove(file)

def remove_pdf_files(pdfs):
for file in pdfs:
remove_pdf_file(file)

def main():
URL = "http://www.cs.brandeis.edu/~cs134/"
html = get_page_source(URL)
pdfs = get_all_pdfs_url(html)

download_all_pdf_files(URL, pdfs)
print('The file', merge_pdf_files(pdfs), 'is generated.')
#remove_pdf_files(pdfs) # if we want to remove those original pdf files

if __name__ == "__main__":
main()

#The following pdf files are downloaded from http://www.cs.brandeis.edu/~cs134/
#1. Lecture1-Overview.pdf
#2. Lecture2-ProbabilityFundementals.pdf
#3. Lecture3-TextClassification-NB-Part1.pdf
#4. TextClassification-NB-MaxEnt.pdf
#5. K_F_Ch3.pdf
#6. Handout1.pdf
#7. Quiz1Topics.pdf
#8. HW1.pdf
#PdfReadWarning: Xref table not zero-indexed. ID numbers for objects will be corrected. [pdf.py:1736]
#The fil

from:https://blog.csdn.net/weixin_43379056/article/details/88020504


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM