如何合并两个PDF文件?

huangapple go评论56阅读模式
英文:

How to merge two PDF files?

问题

我正在尝试合并两个PDF文件。

这是项目所需的Python和Django库,使用pipdeptree提取,并保留树的顶层,所有库都是最新可用版本。

requirements.txt:

aiocontextvars==0.2.2
aiohttp==3.8.4
aiosignal==1.3.1
amqp==5.1.1
appnope==0.1.3
argparse==1.4.0
arrow==1.2.3
asgiref==3.6.0
asttokens==2.2.1
async-generator==1.10
async-timeout==4.0.2
attrs==23.1.0
autobahn==23.1.2
Automat==22.10.0
autopep8==2.0.2
backcall==0.2.0
bcrypt==4.0.1
beautifulsoup4==4.12.2
billiard==3.6.4.0 # 最大版本
...

我得到了这个错误。

File "/code/aca_django/certificates/utils.py", line 10, in <module>
from pypdf import PdfMerger
File "/usr/local/lib/python3.10/site-packages/pypdf/init.py", line 10, in <module>
from ._encryption import PasswordType
File "/usr/local/lib/python3.10/site-packages/pypdf/_encryption.py", line 68, in <module>
from Crypto.Cipher import AES, ARC4 # type: ignore[import]
File "/usr/local/lib/python3.10/site-packages/Crypto/Cipher/ARC4.py", line 119, in <module>
key_size = xrange(1,256+1)
NameError: name 'xrange' is not defined. Did you mean: 'range'?

我尝试了这些导入。

from PyPDF2 import PdfMerger
或者
from PyPDF2 import PdfFileMerger

from pypdf import PdfMerger

但是仍然得到相同的错误。
有什么想法吗?谢谢。

英文:

I'm trying to merge two PDF's.

Here are the required Python and Django libraries for the project, extracted with pipdeptree preserving the top level of the tree and all libraries are up to date for the maximum available versions.

requirements.txt:

aiocontextvars==0.2.2
aiohttp==3.8.4
aiosignal==1.3.1
amqp==5.1.1
appnope==0.1.3
argparse==1.4.0
arrow==1.2.3
asgiref==3.6.0
asttokens==2.2.1
async-generator==1.10
async-timeout==4.0.2
attrs==23.1.0
autobahn==23.1.2
Automat==22.10.0
autopep8==2.0.2
backcall==0.2.0
bcrypt==4.0.1
beautifulsoup4==4.12.2
billiard==3.6.4.0 # max version
boto3==1.26.134
boto3-stubs==1.26.134
botocore==1.29.135
botocore-stubs==1.29.130
Brotli==1.0.9
cachetools==5.3.0
cairocffi==1.5.1
CairoSVG==2.7.0
case-conversion==2.1.0
celery==5.2.7
celery-stubs==0.1.3
certifi==2023.5.7
cffi==1.15.1
channels==4.0.0
channels-redis==4.1.0
charset-normalizer==3.1.0
click==8.1.3
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.2.0
constantly==15.1.0
coverage==7.2.5
cryptography==40.0.2
cssselect2==0.7.0
daphne==4.0.0
debugpy==1.6.7
decorator==5.1.1
defusedxml==0.7.1
Deprecated==1.2.13
diff-match-patch==20230430
distlib==0.3.6
Django==4.1.9
django-admin-rangefilter==0.10.0
django-appconf==1.0.5
django-autoslug==1.9.9
django-cachalot==2.5.3
django-ckeditor==6.5.1
django-compressor==4.3.1
django-cors-headers==4.0.0
django-countries==7.5.1
django-debug-toolbar==4.1.0
django-email-extras==0.3.4
django-extensions==3.2.1
django-file-form==3.5.1
django-fixture-magic==0.1.5
django-formtools==2.4.1
django-grappelli==3.0.6
django-guardian==2.4.0
django-import-export==3.2.0
django-ipware==1.1.6 # update
django-js-asset==2.0.0
django-libsass==0.9
django-log-formatter-json==0.2.0
django-loginas==0.3.11
django-model-utils==4.3.1
django-modeltranslation==0.18.9
django-nested-inline==0.4.6
django-oauth-toolkit==2.2.0
django-otp==1.2.0
django-positions==0.6.0
django-recaptcha==3.0.0
django-redis==5.2.0
django-saml-sp==0.7.0
django-sesame==3.1
django-silk==5.0.3
django-social-share==2.3.0
django-sslserver==0.22
django-storages==1.13.2
django-stubs==4.2.0
django-stubs-ext==4.2.0
django-taggit==4.0.0
django-taggit-autosuggest==0.4.1
django-timezone-field==5.0
django-tinymce==3.6.1
django-tz-detect==0.4.0
django-webpack-loader==1.8.1
djangorestframework==3.14.0
djangorestframework-stubs==3.14.0
dnspython==2.3.0
#drf-api-tracking==1.8.2
easy-thumbnails==2.8.5
ecdsa==0.18.0
et-xmlfile==1.1.0
exceptiongroup==1.1.1
executing==1.2.0
extras==1.0.0
fabric==3.0.1
factory-boy==3.2.1
Faker==18.8.0
feedparser==6.0.10
filelock==3.12.0
fixtures==4.0.1
fonttools==4.39.4
frozenlist==1.3.3
funcy==2.0
furl==2.1.3
future==0.18.3
geographiclib==2.0
geoip2==4.7.0
geopy==2.3.0
google-api-core==2.11.0
google-auth==2.18.0
google-cloud-core==2.3.2
google-cloud-translate==3.11.1
googleapis-common-protos==1.59.0
gprof2dot==2022.7.29
greenlet==2.0.2
grpcio==1.54.2
grpcio-status==1.54.2
h11==0.14.0
html5lib==1.1
hyperlink==21.0.0
ics==0.7.2
idna==3.4
incremental==22.10.0
iniconfig==2.0.0
invoke==2.1.2
ipdb==0.13.13
ipython==8.13.2
ipython-genutils==0.2.0
isodate==0.6.1
jdcal==1.4.1
jedi==0.18.2
jmespath==1.0.1
josepy==1.13.0
Js2Py==0.74
jwcrypto==1.4.2
kombu==5.2.4
libcst==0.4.9
libsass==0.22.0
linecache2==1.0.0
lxml==4.9.2
mailchimp3==3.0.18
MarkupPy==1.14
matplotlib-inline==0.1.6
maxminddb==2.3.0
MonkeyType==23.3.0
more-itertools==9.1.0
mozilla-django-oidc==3.0.0
msgpack==1.0.5
msgpack-python==0.5.6
multidict==6.0.4
mypy==1.3.0
mypy-extensions==1.0.0
neovim==0.3.1
numpy==1.24.3
oauthlib==3.2.2
odfpy==1.4.1
openpyxl==3.1.2
orderedmultidict==1.0.1
outcome==1.2.0
packaging==23.1
pandas==2.0.1
paramiko==3.1.0
parso==0.8.3
pbr==5.11.1
pdfrw==0.4
pendulum==2.1.2
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.5.0
pip==23.1.2
pipdeptree==2.7.1
pipenv==2023.4.29
platformdirs==3.5.1
pluggy==1.0.0
profilehooks==1.12.0
prompt-toolkit==3.0.38
proto-plus==1.22.2
protobuf==4.23.0
psycopg2-binary==2.9.6
ptvsd==4.3.2
ptyprocess==0.7.0
pure-eval==0.2.2
py3-validate-email==1.0.5.post1
pyasn1==0.5.0
pyasn1-modules==0.3.0
pycairo==1.23.0
pycodestyle==2.10.0
pycparser==2.21
pycrypto==2.6.1
pydyf==0.6.0
Pygments==2.15.1
pyjsparser==2.7.1
PyJWT==2.7.0
PyNaCl==1.5.0
pynvim==0.4.3
pyOpenSSL==23.1.1
PyPDF2==3.0.1
pypdf==3.8.1
pyphen==0.14.0
pypng==0.20220715.0
PySocks==1.7.1
pytest==7.3.1
pytest-asyncio==0.21.0
pytest-django==4.5.2
python-dateutil==2.8.2
python-decouple==3.8
python-dotenv==1.0.0
python-gnupg==0.5.0
python-magic==0.4.27
python-mimeparse==1.6.0
python3-saml==1.15.0
pytz==2023.3
pytzdata==2020.1
PyVimeo==1.1.0
PyYAML==6.0
qrcode==7.4.2
rcssmin==1.1.1
redis==4.5.5
regex==2023.5.5
requests==2.30.0
requests-oauthlib==1.3.1
rjsmin==1.2.1
roman==4.0
rsa==4.9
s3transfer==0.6.1
selenium==4.9.1
sentry-sdk==1.23.0
service-identity==21.1.0
setuptools==67.7.2
sgmllib3k==1.0.0
simplegeneric==0.8.1
simplejson==3.19.1
six==1.16.0
sniffio==1.3.0
sorl-thumbnail==12.9.0
sortedcontainers==2.4.0
soupsieve==2.4.1
sphinx-me==0.3
sqlparse==0.4.4
stack-data==0.6.2
stripe==5.4.0
tablib==3.4.0
TatSu==5.8.3
tblib==1.7.0
testtools==2.6.0
tinycss2==1.2.1
tinydb==4.7.1
tomli==2.0.1
traceback2==1.4.0
traitlets==5.9.0
trio==0.22.0
trio-websocket==0.10.2
tuspy==1.0.0
Twisted==22.10.0
txaio==23.1.1
types-awscrt==0.16.17
types-pytz==2023.3.0.0
types-PyYAML==6.0.12.9
types-requests==2.30.0.0
types-s3transfer==0.6.1
types-urllib3==1.26.25.13
typing_extensions==4.5.0
typing-inspect==0.8.0
tzdata==2023.3
tzlocal==5.0.1
unicodecsv==0.14.1
Unidecode==1.3.6
unittest-xml-reporting==3.2.0
unittest2==1.1.0
urllib3==1.26.15 # max version
vine==5.0.0
virtualenv==20.23.0
virtualenv-clone==0.5.7
vishap==0.1.5
watchtower==3.0.1
wcwidth==0.2.6
weasyprint==59.0
webencodings==0.5.1
wheel==0.40.0
wrapt==1.15.0
wsproto==1.2.0
xlrd==2.0.1
xlwt==1.3.0
xmlsec==1.3.13
yarl==1.9.2
zope.interface==6.0
zopfli==0.2.2

And got this error.

File &quot;/code/aca_django/certificates/utils.py&quot;, line 10, in &lt;module&gt;
  from pypdf import PdfMerger
File &quot;/usr/local/lib/python3.10/site-packages/pypdf/__init__.py&quot;, line 10, in &lt;module&gt;
  from ._encryption import PasswordType
File &quot;/usr/local/lib/python3.10/site-packages/pypdf/_encryption.py&quot;, line 68, in &lt;module&gt;
  from Crypto.Cipher import AES, ARC4  # type: ignore[import]
File &quot;/usr/local/lib/python3.10/site-packages/Crypto/Cipher/ARC4.py&quot;, line 119, in &lt;module&gt;
  key_size = xrange(1,256+1)
NameError: name &#39;xrange&#39; is not defined. Did you mean: &#39;range&#39;?

I have tried with those imports.

from PyPDF2 import PdfMerger
  orr
from PyPDF2 import PdfFileMerger
  or 
from pypdf import PdfMerger

But got the same error.
Any idea ? Thanks.

答案1

得分: 1

如评论中所述,错误并非直接来自于 pypdf,而是来自于其依赖项。具体来说,是 pycrypto==2.6.1,这个版本已有10年之久,因此不支持Python 3。不确定这个依赖项是从何处来的,但有一个几乎可以无缝替代的选项叫做 PyCryptodome,因此您应该卸载 pycrypto 并安装 PyCryptodome,看看是否有所帮助:

pip uninstall pycrypto
pip install pycryptodome

欲了解更多信息,请参阅PyCryptodome文档

英文:

as said in the comment the error does not come directly from pypdf but rather from its dependency. Specifically pycrypto==2.6.1 which is 10 years old, hence no Python 3 support. Not sure where this dependency came from, but there's (almost) drop-in replacement called PyCryptodome, so you should uninstall pycrypto and install this one instead to see if that will help:

pip uninstall pycrypto
pip install pycryptodome

for more info see PyCryptodome docs

huangapple
  • 本文由 发表于 2023年5月17日 16:00:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定