如何根据公式提取特定页面?

时间:2019-06-12 15:39:57

标签: python-3.x dataframe pdf tabula

我正在尝试从1000页长的PDF中提取页面,但是我只需要[9,10,17,18,25,26,33,34,... etc]模式的页面。这些数字可以用公式pg = 1/2 (7 - 3 (-1)^n + 8*n)表示。

我试图定义公式并插入tabula.read_pdf,但是我不确定如何定义'n'变量,其中'n'的范围从0到25。现在我将其定义为列表我认为是问题所在...

n = list(range(25+1))
pg = 1/2 (7 - 3 (-1)^n + 8*n) 

df = tabula.read_pdf(path, pages = 'pg',index_col=0, multiple_tables=False)

尝试执行时,出现TypeError:在行pg = 1/2 (7 - 3 (-1)^n + 8*n)上不能调用'int'对象。我该如何定义变量,以便表格提取符合公式条件的页面?

1 个答案:

答案 0 :(得分:0)

公式为 x = 1/2(8n-3(-1)^ n + 7)

第一步:

pg = []  #Empty list to store the pages numbers calculated by formula
for i in range(1, 25+1):     # For 1000 pages pdf use 1000 instead of 25
    k = int(1/2*((8*n[i])-3*((-1)**n[i])+7))
    pg.append(k)

print(pg, end = '')  # This will give you list of page numbers

#[9, 10, 17, 18, 25, 26, 33, 34, 41, 42, 49, 50, 57, 58, 65, 66, 73, 74, 81, 82, 89, 90, 97, 98, 105]

第2步:

# Now run the loop through each of the pages with the table

df=pd.DataFrame([])
df_combine=pd.DataFrame([])

for pageiter in range(pg):
            df = tabula.read_pdf(path, pages=pageiter+1 ,index_col=0, multiple_tables=False, guess=False) #modify it as per your requirement
            df_combine=pd.concat([df,df_combine]) #you can choose between merge or concat as per your need

OR

df_data = []
for pageiter in range(pg):
            df = tabula.read_pdf(path, pages=pageiter+1 ,index_col=0, multiple_tables=False, guess=False) #modify it as per your requirement
            df_data.append(df)

df_combine= pd.concat(df_data, axis=1)

创建公式的参考链接 https://www.wolframalpha.com/widgets/view.jsp?id=a3af2e675c3bfae0f2ecce820c2bef43