gpt4 book ai didi

Python合并pdf文件的工具

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 30 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Python合并pdf文件的工具由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

  如果你需要一个pdf文件合并工具,那么本文章完全可以满足您的要求。哈喽,大家好呀,这里是滑稽研究所。不多废话,本期我们利用python合并把多个pdf文件合并为一个。我们提前准备了5个pdf文件,来验证代码.

  源代码:

?
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
from pypdf2 import pdffilereader, pdffilewriter
 
# 使用os模块的walk函数,搜索出指定目录下的全部pdf文件
# 获取同一目录下的所有pdf文件的绝对路径
def getfilename(filedir):
 
     file_list = [os.path.join(root, filespath) \
                  for root, dirs, files in os.walk(filedir) \
                  for filespath in files \
                  if str (filespath).endswith( 'pdf' )
                  ]
     return file_list if file_list else []
 
# 合并同一目录下的所有pdf文件
def mergepdf(filepath, outfile):
 
     output = pdffilewriter()
     outputpages = 0
     pdf_filename = getfilename(filepath)
 
     if pdf_filename:
         for pdf_file in pdf_filename:
             print ( "路径:%s" % pdf_file)
 
             # 读取源pdf文件
             input = pdffilereader( open (pdf_file, "rb" ))
 
             # 获得源pdf文件中页面总数
             pagecount = input .getnumpages()
             outputpages + = pagecount
             print ( "页数:%d" % pagecount)
 
             # 分别将page添加到输出output中
             for ipage in range (pagecount):
                 output.addpage( input .getpage(ipage))
 
         print ( "合并后的总页数:%d." % outputpages)
         # 写入到目标pdf文件
         outputstream = open (os.path.join(filepath, outfile), "wb" )
         output.write(outputstream)
         outputstream.close()
         print ( "pdf文件合并完成!" )
 
     else :
         print ( "没有可以合并的pdf文件!" )
 
# 主函数
def main():
     file_dir = input ( '请输入存有pdf的文件夹' ).replace( '/' , '//' ) # 存放pdf的原文件夹
     outfile = "pick_me.pdf" # 输出的pdf文件的名称
     mergepdf(file_dir, outfile)
     print ( 'done' )
 
main()

Python合并pdf文件的工具
Python合并pdf文件的工具

  可以看到5个pdf文件合并到了一起,那么到这里就结束了吗?当然不是,代码运行遇到pdf文件中文件格式较多时,比如多图,word格式等,会出现以下报错.

Python合并pdf文件的工具

  最后一行报错的意思为:

pypdf2。utils.pdfreaderror:对于键/im82,字典中字节0xc0161处有多个定义 。

  通俗一点就是说遇到了一个多义词,程序不知道该取哪个意思了。我们点进pdf.py文件里,找到下图位置.

Python合并pdf文件的工具

  严格模式默认是打开的,我们改成false.

构造方法: pypdf2.pdffilereader(stream,strict = true,warndest = none,overwritewarnings = true) stream:file 对象或支持与 file 对象类似的标准读取和查找方法的对象,也可以是表示 pdf 文件路径的字符串。 strict(bool):确定是否应该警告用户所用的问题,也导致一些可纠正的问题是致命的,默认是 true warndest : 记录警告的目标(默认是 sys.stderr) overwritewarnings(bool):确定是否 warnings.py 用自定义实现覆盖 python 模块(默认为 true) 。

  我们重新运行程序. 。

Python合并pdf文件的工具

  打开文件夹,可以看到我们的文件已经合并好了,打开之后的格式也是没有错误的。   那么,问题解决.

Python合并pdf文件的工具

  如果你只是需要应该pdf合并工具代码直接拿走用即可,如果你想学习pypdf2这个实用的库,并且希望对这段代码进行改进来适配自己的情况 。

到此这篇关于python合并pdf文件的文章就介绍到这了,更多相关python合并pdf文件内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。

原文链接:https://blog.csdn.net/weixin_45067072/article/details/118372913 。

最后此篇关于Python合并pdf文件的工具的文章就讲到这里了,如果你想了解更多关于Python合并pdf文件的工具的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com