报错如下:

Traceback (most recent call last):
File "./pdf_merge.py", line 68, in <module>
main(sys.argv[1], sys.argv[2])
File "./pdf_merge.py", line 51, in main
blankpage.mergeTranslatedPage(pageobj, 0, 0)
File "/Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py", line 2377, in mergeTranslatedPage
tx, ty], expand)
File "/Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py", line 2328, in mergeTransformedPage
PageObject._addTransformationMatrix(page2Content, page2.pdf, ctm), ctm, expand)
File "/Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py", line 2284, in _mergePage
page2Content, rename, self.pdf)
File "/Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py", line 2189, in _contentStreamRename
op = operands[i]
KeyError: 0

解决:

  1. 打开 /Users/root/Library/Python/2.7/lib/python/site-packages/PyPDF2/pdf.py
  2. 在文件开头加上 import types
  3. 找到函数 _contentStreamRename

    修改函数:
    def _contentStreamRename(stream, rename, pdf):
if not rename:
return stream
stream = ContentStream(stream, pdf)
for operands, operator in stream.operations:
# for i in range(len(operands)):
# op = operands[i]
# if isinstance(op, NameObject):
# operands[i] = rename.get(op,op)
if type(operands) == types.ListType:
for i in range(len(operands)):
op = operands[i]
if isinstance(op, NameObject):
operands[i] = rename.get(op,op)
elif type(operands) == types.DictType:
for i in operands:
op = operands[i]
if isinstance(op, NameObject):
operands[i] = rename.get(op,op)
else:
raise KeyError("type of operands is %s" % type(operands))
return stream
_contentStreamRename = staticmethod(_contentStreamRename)

参考链接:https://github.com/mstamy2/PyPDF2/issues/196

PyPDF2.py 合并pdf时报错问题的更多相关文章

  1. word 转 PDF时报错

    利用微软自带的com组件,把word转化成Pdf,利用vs2012调试时没有问题,但是发布到IIS时出错,错误为: 检索 COM 类工厂中 CLSID 为 {} 的组件时失败,原因是出现以下错误: 8 ...

  2. django 运行python manage.py sqlall books 时报错 app has migration

    出现这个问题的原因是版本之前的不兼容,我用的django版本是1.8.6 而 这条python manage.py sqlall books 是基于django1.0版本的. 在django1.8.6 ...

  3. py+selenium运行时报错Can not connect to the Service IEDriverServer.exe

    问题: 运行用例时,出现报错(host文件已加入127.0.0.1 localhost): raise WebDriverException("Can not connect to the ...

  4. 使用PyPdf2合并PDF文件(没有空白、报错)

    使用PyPdf2合并PDF文件(没有空白.报错) 对于合并之后pdf空白,或者出现 'latin-1' codec can't encode characters in position 8-11: ...

  5. pypdf2:下载Americanlife网页生成pdf合并pdf并添加书签

    初步熟悉 安装 pip install pypdf2 合并并添加书签 #!/usr/bin/env python3.5 # -*- coding: utf-8 -*- # @Time : 2019/1 ...

  6. python 运行python manege.py runserver时报错:“no module named djangorestframework” 的解决方案

    python 运行python manege.py runserver时报错:“no module named djangorestframework” 的解决方案 importerror:no mo ...

  7. 配置python+mod_wsgi+apache 时 在浏览器中访问服务器时报错:Invalid HTTP_HOST header: 'XXXXX'. You may need to add u'XXXXX' to ALLOWED_HOSTS,在setting.py中添加‘*”无效的原因

    配置python+mod_wsgi+apache 时 在浏览器中访问服务器时报错:Invalid HTTP_HOST header: 'XXXXX'. You may need to add u'XX ...

  8. 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'

    在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: ...

  9. 使用Python批量合并PDF文件(带书签功能)

    网上找了几个合并pdf的软件,发现不是很好用,一般都没有添加书签的功能. 又去找了下python合并pdf的脚本,发现也没有添加书签的功能的. 于是自己动手编写了一个小工具,使用了PyPDF2. 下面 ...

随机推荐

  1. infomation_schema基本使用

    一.infomation_schema库 把 information_schema 看作是一个数据库,确切说是信息数据库.其中保存着关于MySQL服务器所维护的所有其他数据库的信息.如数据库名,数据库 ...

  2. FactoryBean简介以及Mybatis-Spring应用

    一.BeanFactory和FactoryBean区别? BeanFactory是工厂类,提供了获取和检索Bean的接口.它代表着Spring的IoC容器,负责Bean实例化以及管理Bean之间的依赖 ...

  3. ;~ 小部分AutoHotkey源代码片段测试模板2019年10月9日.ahk

    ;~ 小部分AutoHotkey源代码片段测试模板2019年10月9日.ahk ;~ 此脚本用于测试执行一行或多行AHK脚本源代码的效果;~ 此脚本最后修改于2019年9月22日20时03分;~ 把此 ...

  4. 面试了一位33岁Android程序员,只会面向百度编程,居然要25k,脸呢?

    最近逛论坛看到这样一个帖子: 面试了一位工作12年的程序员, 这位老哥有3年java开发经验,2年H5,7年Android开发经验,简历上写着精通Java,Android,熟悉H5开发.没有具体的技术 ...

  5. windows的基本命令和环境配置

    刚开始学的时候整理的基本知识 一.windows的基础知识: 1.常用的DOS命令 盘符切换: D: 回车 进入目录: cd xxx 回退目录 返回到上一级: cd ..  返回到根目录: cd / ...

  6. 使用Cobertura做代码覆盖率测试

    经验总结:首先要把cobertura.jar包含ant的classpath路径中,其次要求它包含在测试用例的classpath中: 使用cobertura做代码覆盖率测试中出现的问题:覆盖率始终为0, ...

  7. Git-08-标签管理

    标签管理 Git的标签虽然是版本库的快照,但其实它就是指向某个commit的指针 跟分支很像对不对?但是分支可以移动,标签不能移动 所以,创建和删除标签都是瞬间完成的 Git有commit,为什么还要 ...

  8. Visio2013安装报错 1935 问题解决

    最近安装Visio2013,奈何一直报错,出现1935的错误并且回滚 试了试网上的方法,无论是安装.netframework4.0也好,下载.net修复工具也好,都不行 最后尝试删除一个注册表路径 H ...

  9. SeacmsV10.7版代码审计笔记

    data: 2020.11.9 10:00AM description: seacms代码审计笔记 0X01前言 seacms(海洋cms)在10.1版本后台存在多处漏洞,事实上当前最新版V10.7这 ...

  10. Pytest+Allure 示例

    0. 前言 简介 Allure 框架是一个灵活的.轻量级的.支持多语言的测试报告工具,它不仅以 Web 的方式展示了简介的测试结果,而且允许参与开发过程的每个人可以从日常执行的测试中,最大限度地提取有 ...