如何停止python中pdf转换的内存错误?

2024-06-02 16:06:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用pdf2image(一种poppler包装器)将pdf转换为枕头图像,以便查看pdf。但是,无论我使用什么pdf,我总是会遇到以下错误:

Traceback (most recent call last):
File "C:\Users\karee\AppData\Local\Programs\Python\Python39-32\lib\threading.py", line 954, in _bootstrap_inner
    self.run()
  File "C:\Users\karee\AppData\Local\Programs\Python\Python39-32\lib\threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\karee\AppData\Local\Programs\Python\Python39-32\lib\subprocess.py", line 1479, in _readerthread
    buffer.append(fh.read())
MemoryError

有没有办法解决这个问题,改变我的电脑规格或得到一个新的? 这是我的密码:

root = Tk()
pdf_frame = Frame(root).pack(fill=BOTH, expand=1)
scrol_y = Scrollbar(pdf_frame, orient=VERTICAL)
pdf = Text(pdf_frame, yscrollcommand=scrol_y.set, bg="grey")
scrol_y.pack(side=RIGHT, fill=Y)
scrol_y.config(command=pdf.yview)
pdf.pack(fill=BOTH, expand=1)
pages = convert_from_path('Books/Keeper Of The Lost Cities/Everblaze ( PDFDrive ).pdf', size=(800, 900),
                          poppler_path=r"poppler-21.03.0\Library\bin")
photos = []
for i in range(len(pages)):
    photos.append(ImageTk.PhotoImage(pages[i]))
for photo in photos:
    pdf.image_create(END, image=photo)

    pdf.insert(END, '\n\n')
mainloop()

任何帮助都将不胜感激


Tags: inpyselfpdfliblocallineusers
1条回答
网友
1楼 · 发布于 2024-06-02 16:06:18

只需批量转换PDF文件,每次5个。i、 e

from pdf2image import pdfinfo_from_path,convert_from_path
info = pdfinfo_from_path(pdf_file, userpw=None, poppler_path=None)

maxPages = info["Pages"]
for page in range(1, maxPages+1, 10) : 
   convert_from_path(pdf_file, dpi=200, first_page=page, 

相关问题 更多 >