Baldev
Baldev

Reputation: 25

How open multiple encrypted PDF and save without password in Python

I am a newbee just started my first language as Python.

I am trying to write code to open multiple encrypted pdf files and save them without password.

All files are in a folder, I have a csv file filePassword.csv with columns filename and password.

But my code is not working. Please guide me on how to solve this error.

import pikepdf as pdf
import pandas as pd
df = pd.read_csv('filePassword.csv')
filename, password = df['filename'], df['password']
for file in filename:
    for code in password:
        file1 = pdf.open(file,code)
        file1.save('1_'+filename)

I get this error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Upvotes: 0

Views: 527

Answers (2)

ggaurav
ggaurav

Reputation: 1804

You can loop over the dataframe df using iterrows() and then access filename and password in following way. There is no need of nested loops.

import pikepdf as pdf
import pandas as pd
df = pd.read_csv('filePassword.csv')
for index, row in df.iterrows():
    file1 = pdf.open(row['filename'], row['password'])
    file1.save('1_'+row['filename'])

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71570

Try using file instead of filename:

import pikepdf as pdf
import pandas as pd
df = pd.read_csv('filePassword.csv')
for file in df['filename']:
    for code in df['password']:
        file1 = pdf.open(file,code)
        file1.save('1_' + file)

Upvotes: 0

Related Questions