Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在闪存驱动器上使用pyInstaller后指定PyPDF2的文件路径_Python_Pyinstaller_Pypdf2 - Fatal编程技术网

Python 在闪存驱动器上使用pyInstaller后指定PyPDF2的文件路径

Python 在闪存驱动器上使用pyInstaller后指定PyPDF2的文件路径,python,pyinstaller,pypdf2,Python,Pyinstaller,Pypdf2,我已经编写了一个python脚本,它可以从pdf文档中提取账号,在本地运行效果非常好。我使用pyinstaller将脚本提取到可执行文件中,并将其保存到闪存驱动器中。我将pdf文档移动到我认为是CWD的位置,但当我尝试运行它时,我的终端告诉我无法找到要打开的pdf 最终,我希望我的客户端能够将闪存驱动器放入,将最新版本的pdf移到同一目录,然后运行脚本。我需要在正确的方向上进行微调,以了解如何指定PyPDF2的文件路径。这是到目前为止我的代码 def getDataFromPdf(): acct

我已经编写了一个python脚本,它可以从pdf文档中提取账号,在本地运行效果非常好。我使用pyinstaller将脚本提取到可执行文件中,并将其保存到闪存驱动器中。我将pdf文档移动到我认为是CWD的位置,但当我尝试运行它时,我的终端告诉我无法找到要打开的pdf

最终,我希望我的客户端能够将闪存驱动器放入,将最新版本的pdf移到同一目录,然后运行脚本。我需要在正确的方向上进行微调,以了解如何指定PyPDF2的文件路径。这是到目前为止我的代码

def getDataFromPdf():
acctNumberRegex = re.compile(r'\d\d\d\d\d-\d\d\d-\d\d\d\d')
pdfFile = open('records.pdf', 'rb')
reader = PyPDF2.PdfFileReader(pdfFile)
for pageNum in range(0,10):
    page = reader.getPage(pageNum).extractText()
    accounts = acctNumberRegex.findall(page)
    for acct in accounts:
        if acct not in results:
            results.append(acct)
print(len(results)) 

谢谢

可以将文件路径作为参数传递到函数中

因此:

def getDataFromPdf(文件路径):
acctNumberRegex=re.compile(r'\d\d\d\d-\d\d\d-\d\d-\d\d\d\d')
pdfFile=open(filePath+'records.pdf','rb')
reader=PyPDF2.PdfFileReader(pdfFile)
对于范围(0,10)内的pageNum:
page=reader.getPage(pageNum.extractText())
accounts=acctNumberRegex.findall(第页)
对于账户中的账户:
如果账户未显示结果:
结果追加(acct)
打印(len(结果))

通过r/learnpython解决方案

SCRIPT_DIR = Path(sys.executable).parent

def getDataFromPdf():
pdf_file = SCRIPT_DIR / 'records.pdf'
print(pdf_file.resolve())
with open(pdf_file.resolve(),'rb') as records:
    acctNumberRegex = re.compile(r'\d\d\d\d\d-\d\d\d-\d\d\d\d')
    reader = PyPDF2.PdfFileReader(records)
    for pageNum in range(0,reader.numPages):
        page = reader.getPage(pageNum).extractText()
        accounts = acctNumberRegex.findall(page)
        for acct in accounts:
            if acct not in results:
                results.append(acct)
    print(str(len(results)) + " account numbers pulled from PDF")