【转】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
【转】Python编程: 多个PDF文件合并以及网页上自动下载PDF文件的更多相关文章
- Python编程:从入门到项目实践高清版附PDF百度网盘免费下载|Python入门编程免费领取
百度网盘:Python编程:从入门到项目实践高清版附PDF免费下载 提取码:oh2g 第一部分 基础知识第1章 起步 21.1 搭建编程环境 21.1.1 Python 2和Python 3 21 ...
- python - 将数据转换成 excl 表格, json 等文件 (dajngo - 打开网页后自动下载)
本篇只讲述怎么用. 具体 tablib 更多详细用法可参考博客 : https://blog.csdn.net/liangyuannao/article/details/41476277 # 不得不 ...
- Xshell5下利用sftp上传下载传输文件
sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp 与 ftp 有着几乎一样的语法和功能.SFTP 为 SSH ...
- selenium3 无人工干预地自动下载某个文件
一:主要内容 下载效果展示 代码内容展示 saveToDisk不生效说明,即文件没有下载下来解决办法 二:展示效果 1.下载效果展示 用selenium3无人工干预的自动下载该文件到指定路径下,如:D ...
- SpringMVC文件上传下载(单文件、多文件)
前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...
- IDM下载器添加支持自动下载的文件类型
不知道各位读者老爷有没有试过IDM下载器的自动下载功能,对于经常需要下载素材资源的朋友来说,一个个的选择图片或者其他什么素材来下载也是够烦的,IDM的自动下载功能可谓是十分好用,而且自动下载+批量下载 ...
- 如何获得网页上的swf视频教程文件?
网上有很多免费的视频教程,但有的视频教程这能在线观看,无法离线下在,如何获得网页上的swf视频教程文件呢? 我问以"我要自学网"的视频教程为例进行讲解.这是一个我要自学网的PS视频 ...
- js上传文件带参数,并且,返回给前台文件路径,解析上传的xml文件,存储到数据库中
ajaxfileupload.js jQuery.extend({ createUploadIframe: function(id, uri) { //create frame var frameId ...
- (转)GitHub上想下载单个文件方法
找到该文件,单机raw,如下图: 然后会在网页打开该文件,复制URL,下载即可(如果是不可预览文件,会自动下载). 转自: GitHub上想下载单个文件方法 - Smallcaff的博客 - CSDN ...
随机推荐
- ceph luminous版部署bluestore
简介 与filestore最大的不同是,bluestore可以直接读写磁盘,即对象数据是直接存放在裸设备上的,这样解决了一直被抱怨的数据双份写的问题 Bluestore直接使用一个原始分区来存放cep ...
- javascript_15-undefined 和 is not defined 的区别
undefined 和 is not defined //1 console.log(a); // is not defined //2 var a; console.log(a); //undefi ...
- 修改ActiveMQ的内存大小
有时我们需要修改ActiveMQ的内存大小,防止内存溢出! 修改配置文件下-Xmx参数然后重启mq即可: /fs01/apache-activemq-5.15.0/bin/env ACTIVEMQ_O ...
- Crypto模块中的签名算法
因为支付宝当中需要自行实现签名,所以就用到了SHA265和RSA2,将拼接好的信息用私钥进行签名,并进行Base64编码,然后解密就用支付宝回传给用户的公钥解密就ok了,所以我就使用Crypto模块, ...
- 设置Django生产环境系统重启后的自动启动项
前面,作者已经介绍了把Django部署到生产环境中的主要方法,现在我们来看一下如何设置项目开机启动. 在把Django项目部署到生产环境中时,我们前面使用安装包和源码安装了Nginx.uwsgi.re ...
- SSH安全协议
SSHD服务 介绍:SSH 协议:安全外壳协议.为 Secure Shell 的缩写.SSH 为建立在应用层和传输层基础上的安全协议. 默认端口22 作用 sshd服务使用SSH协议可以用来进行远程控 ...
- mysql简单优化的一些总结
mysql对cpu的利用特点: 5.1之前,多核支持较弱:5.1可利用4个核:5.5可利用24核:5.6可利用64个核:每个连接对应一个线程,每个并发query只能使用一个核 mysql对内存的利用特 ...
- Python开发笔记之-字符串函数
1.首字母大写 >>> s = 'yuanzhumuban' >>> s.capitalize() 'yuanzhumuban' 2.replace,替换 > ...
- Hive-2.3.6 安装
本安装依赖Haddop2.8安装 https://www.cnblogs.com/xibuhaohao/p/11772031.html 一.下载Hive与MySQL jdbc 连接驱动 apache- ...
- Kubernetes 学习25 创建自定义chart及部署efk日志系统
一.概述 1.我们说过在helm架构中有这么几个关键组件,helm,tiller server,一般托管运行于k8s之上,helm能够通过tiller server在目标k8s集群之上部署应用程序,而 ...