如果頁面包含某些字串,我正在嘗試提取 PDF 頁碼,然后將選定的頁碼附加到串列中。例如,第 2、254、439 和 458 頁符合標準,我期望輸出為串列 [2,254,439,458]。我的代碼是:
object=PyPDF2.PdfFileReader(file_path)
NumPages = object.getNumPages()
String = 'specific string'
for i in range(0,NumPages):
PageObj=object.getPage(i)
Text = PageObj.extractText()
ReSearch = re.search(String,Text)
Pagelist=[]
if ReSearch != None:
Pagelist.append(i)
print(Pagelist)
我收到的輸出為:
- [2]
- [254]
- [439]
- [458]
有人可以看看,看看我怎么能解決它?謝謝
uj5u.com熱心網友回復:
現在,您在每次迭代中定義一個新的 llst,因此您只需在回圈之前定義一次串列。也在回圈外列印它:
Pagelist=[]
for i in range(0,NumPages):
# rest of the loop
print(Pagelist)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/473753.html
上一篇:回應中的Django模型默認值
