Replace this: What happened? What were you trying to achieve?
Environment
Which environment were you using when you encountered the problem?
$ python -m platform
Windows-10-10.0.22000-SP0
$ python -c "import PyPDF2;print(PyPDF2.__version__)"
2.6.0
Code + PDF
This is a minimal, complete example that shows the issue:
import os
import PyPDF2
pdf_filename="ST辅仁:2019年年度报告.PDF"
txt_filename="ST辅仁:2019年年度报告.txt"
with open(pdf_filename, 'rb') as pdfFileObj:
pdfReader = PyPDF2.PdfReader(pdfFileObj)
x=pdfReader.numPages
with open(txt_filename,"a",encoding='utf-8') as file1:
for i in range(x):
pageobj=pdfReader.pages[i]
text=pageobj.extract_text()
file1.writelines(text)
Share here the PDF file(s) that cause the issue. The smaller they are, the
better. Let us know if we may add them to our tests!
ST辅仁:2019年年度报告.PDF
Traceback
This is the complete Traceback I see:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
e:\pdf2txt\test\pdf2txt.ipynb Cell 4 in <cell line: 1>()
----> [1](vscode-notebook-cell:/e%3A/pdf2txt/test/pdf2txt.ipynb#ch0000004?line=0) dir_pdf2txt(dirlist[1])
e:\pdf2txt\test\pdf2txt.ipynb Cell 4 in dir_pdf2txt(dir_name)
[15](vscode-notebook-cell:/e%3A/pdf2txt/test/pdf2txt.ipynb#ch0000004?line=14) pageobj=pdfReader.pages[i]
[16](vscode-notebook-cell:/e%3A/pdf2txt/test/pdf2txt.ipynb#ch0000004?line=15) print(i)
---> [17](vscode-notebook-cell:/e%3A/pdf2txt/test/pdf2txt.ipynb#ch0000004?line=16) text=pageobj.extract_text()
[18](vscode-notebook-cell:/e%3A/pdf2txt/test/pdf2txt.ipynb#ch0000004?line=17) file1.writelines(text)
File c:\Users\Baiyi Yu\AppData\Local\Programs\Python\Python310\lib\site-packages\PyPDF2\_page.py:1433, in PageObject.extract_text(self, Tj_sep, TJ_sep, space_width)
1415 def extract_text(
1416 self, Tj_sep: str = "", TJ_sep: str = "", space_width: float = 200.0
1417 ) -> str:
1418 """
1419 Locate all text drawing commands, in the order they are provided in the
1420 content stream, and extract the text.
(...)
1431 :return: The extracted text
1432 """
-> 1433 return self._extract_text(self, self.pdf, space_width, PG.CONTENTS)
File c:\Users\Baiyi Yu\AppData\Local\Programs\Python\Python310\lib\site-packages\PyPDF2\_page.py:1135, in PageObject._extract_text(self, obj, pdf, space_width, content_key)
1133 if "/Font" in resources_dict:
...
File c:\Users\Baiyi Yu\AppData\Local\Programs\Python\Python310\lib\site-packages\PyPDF2\generic.py:681, in DictionaryObject.__getitem__(self, key)
680 def __getitem__(self, key: Any) -> PdfObject:
--> 681 return dict.__getitem__(self, key).get_object()
KeyError: '/W'
Replace this: What happened? What were you trying to achieve?
Environment
Which environment were you using when you encountered the problem?
$ python -m platform Windows-10-10.0.22000-SP0 $ python -c "import PyPDF2;print(PyPDF2.__version__)" 2.6.0Code + PDF
This is a minimal, complete example that shows the issue:
Share here the PDF file(s) that cause the issue. The smaller they are, the
better. Let us know if we may add them to our tests!
ST辅仁:2019年年度报告.PDF
Traceback
This is the complete Traceback I see: