在将pyInstaller插入闪存驱动器后为PyPDF2指定文件路径

时间:2018-07-24 14:05:07

标签: python pyinstaller pypdf2

我编写了一个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)) 

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以将文件路径作为参数传递给函数。

所以:

def getDataFromPdf(filePath):
    acctNumberRegex = re.compile(r'\d\d\d\d\d-\d\d\d-\d\d\d\d')
    pdfFile = open(filePath + '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)) 

答案 1 :(得分:0)

通过r / learnpython解决方案

/*  Shake animation  */
@charset "UTF-8";

.animated {
  -webkit-animation-duration: 1s;
       -moz-animation-duration: 1s;
         -o-animation-duration: 1s;
            animation-duration: 1s;
    -webkit-animation-fill-mode: both;
       -moz-animation-fill-mode: both;
         -o-animation-fill-mode: both;
            animation-fill-mode: both;
}

.animated.hinges {
    -webkit-animation-duration: 2s;
       -moz-animation-duration: 2s;
         -o-animation-duration: 2s;
            animation-duration: 2s;
}

.animated.slow {
    -webkit-animation-duration: 3s;
       -moz-animation-duration: 3s;
         -o-animation-duration: 3s;
            animation-duration: 3s;
}

.animated.snail {
    -webkit-animation-duration: 4s;
       -moz-animation-duration: 4s;
         -o-animation-duration: 4s;
            animation-duration: 4s;
}

@-webkit-keyframes shake {
    0%, 100% {-webkit-transform: translateX(0);}
    10%, 30%, 50%, 70%, 90% {-webkit-transform: translateX(-10px);}
    20%, 40%, 60%, 80% {-webkit-transform: translateX(10px);}
}

@-moz-keyframes shake {
    0%, 100% {-moz-transform: translateX(0);}
    10%, 30%, 50%, 70%, 90% {-moz-transform: translateX(-10px);}
    20%, 40%, 60%, 80% {-moz-transform: translateX(10px);}
}

@-o-keyframes shake {
    0%, 100% {-o-transform: translateX(0);}
    10%, 30%, 50%, 70%, 90% {-o-transform: translateX(-10px);}
    20%, 40%, 60%, 80% {-o-transform: translateX(10px);}
}

@keyframes shake {
    0%, 100% {transform: translateX(0);}
    10%, 30%, 50%, 70%, 90% {transform: translateX(-10px);}
    20%, 40%, 60%, 80% {transform: translateX(10px);}
}

.shake {
    -webkit-animation-name: shake;
    -moz-animation-name: shake;
    -o-animation-name: shake;
    animation-name: shake;
}