Looping over a list - 'int' object is not subscriptable

Looping over a list - 'int' object is not subscriptable



I've uploaded a PDF using PyPDF2. Here's my code:


PageNo=list(range(0,pfr.numPages))

for i in PageNo:
pg = writer.addPage(i)



PageNo creates a list of all the page numbers and I'm trying to add each page from the list into a new PDF, but when I run this script, I'm getting 'int' object is not subscriptable. I know it's an error in my for loop, and I'm sure it's obvious, but any insight on my mistake above would be appreciated.



Thanks!



Edit - here's the solution:


for i in list(range(0,pfr.numPages))
pg = pfr.getPage(i)
writer.addPage(pg)





What about for i in range(0,len(PageNo)):? Will you show us the list PageNo made ?
– Nouman
yesterday



for i in range(0,len(PageNo)):


PageNo





Could you post the stack trace for the error? (e.g. the complete error dump)
– payne
yesterday





Got it working! I needed to to define a new variable in my for loop before I passed it through writer.addPage
– user2744315
16 hours ago





2 Answers
2



Without seeing more code, like what exactly is pfr.numPages? Where is it created? How is it used? the error is happening because PageNo is being evaluated as an int, so its less likely its an error in your for loop and more likely an error in your list. Assuming pfr.numPages is an array of some type, try this:


PageNo


int


pfr.numPages


for i in range(0, len(pfr.numPages)):
pg = writer.addPage(prf.numPages[i])



However as mentioned before, if PageNo is being evaluated as an int, then pfr.numPages isn't an array or its an array with one element. Pay close attention to the trace if this errors, it will tell you which line, and if it says int is not subscriptable on the line with prf.numPages, then you know that your problem is deeper than this for loop.


PageNo


prf.numPages





You are using prf.numPages[i] in your loop, it means that prf.numPages is a list. So how can use use pfr.numPages as integer in for i in range(0, pfr.numPages):?
– Nouman
yesterday


prf.numPages[i]


prf.numPages


pfr.numPages


for i in range(0, pfr.numPages):





Good catch! Thank you I forgot to add len .
– darrahts
17 hours ago


len





Thanks for your comment! Turns out the issue was that I needed to define the variable before it was passed through writer.addPage. for i in list(range(0,pfr.numPages)): pg = pfr.getPage(i) writer.addPage(pg)
– user2744315
16 hours ago



I am assuming that writer is a PdfFileWriter object. In that case, you will have to pass a page retrieved from pfr to the writer.addPage method, instead of passing an integer.


writer


PdfFileWriter


pfr


writer.addPage


for i in pfr.pages:
writer.addPage(i)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

ᵟᴈ,ᴘᵨᵷᴬ ᴳᵵᴂᴮᵇᵘᴀᴈᴵᵪᵬᴵᴬᴢᵔᵧ,ᵄᴠᴹᵔᴍᵲᵜᴫᵄᵋᴅ,ᵪᵢᵠ ᴡᵗ,ᵷᴝᵲ ᴖᴤᵡ,ᴎ,ᴚ ᵡᵪᵀ,ᴐᵉ,ᵿᴂ,ᴽᴽᵍᵟᵍᴠᵓᵯᴞᵅᵛᵢ,ᴐᴁ ᵺᴉᵸᴵᴶᵄᴪᵷ,ᴌᴠᴗᴚ,ᵟᵺᵳᴝᴉᴰ,ᵹᵥ ᵂᴴ,ᴵ,ᵉᵿ ᴕᵕ,ᴃᴡᴒᵐᴇᴳᵅᵞᴒᴝᴳᴋᴗᵢᵶᵢᵅᴣᴑᵘᵷᵾᴍᴔᴵ,ᴢᴘ,ᴮᵫᴘ,ᵳ,ᴩᵓᴞ