2014-12-18 3 views
0

Windows Powershell에서 pdfminer에서 스크립트를 실행하는 루프를 실행 중입니다. 파일 디렉터리에서 pdf2txt.py를 실행합니다. 루프가 있습니다 :IOError : Errno 13 Permission Denied D : Powershell에서 python 루프를 실행할 때/PDFs

$PATH="D:/PDFdirectory" 

foreach ($f in $PATH) 
{ 
python pdf2txt.py -o $f.txt "$f" "${f%.pdf}.txt" 
} 

Powershell에서 위의 코드를 실행하려고하면 권한이 거부되었습니다. 오류가 발생합니다. 오류는 아래의 pdf2txt 스크립트에서 outfp = file (outfile, 'w + b')을 가리 킵니다.

if outfile: 
    outfp = file(outfile, 'w+b') 
else: 
    outfp = sys.stdout 
if outtype == 'text': 
    device = TextConverter(rsrcmgr, outfp, codec=codec, laparams=laparams, 
          imagewriter=imagewriter) 
elif outtype == 'xml': 
    device = XMLConverter(rsrcmgr, outfp, codec=codec, laparams=laparams, 
          imagewriter=imagewriter) 
elif outtype == 'html': 
    device = HTMLConverter(rsrcmgr, outfp, codec=codec, scale=scale, 
          layoutmode=layoutmode, laparams=laparams, 
          imagewriter=imagewriter) 
elif outtype == 'tag': 
    device = TagExtractor(rsrcmgr, outfp, codec=codec) 
else: 
    return usage() 
for fname in args: 
    fp = file(fname, 'rb') 
    interpreter = PDFPageInterpreter(rsrcmgr, device) 
    for page in PDFPage.get_pages(fp, pagenos, 
            maxpages=maxpages, password=password, 
            caching=caching, check_extractable=True): 
     page.rotate = (page.rotate+rotation) % 360 
     interpreter.process_page(page) 
    fp.close() 
device.close() 
outfp.close() 
return 

if __name__ == '__main__': sys.exit(main(sys.argv)) 

pdf2txt.py의 읽기 및 쓰기 조건을 Windows와 호환되도록 바이너리로 변경했지만 지금은 붙어 있습니다. 아무도 나를 도울 수 있습니까?

감사합니다.

+0

Powershell을 관리자로 실행하고 있습니까? –

+0

예 관리자로 시작했습니다 – ChuckD

+0

'foreach ($ f in $ PATH)'로 원하는 것을 할 수 있습니까? 정확히 하나의 항목, 즉 디렉토리 이름이있는 것처럼 보이며 해당 디렉토리에있는 파일을 열거하지 않습니다. – dpw

답변

0

첫 번째 문제는 PowerShell 스크립트에서 잘못된 구문이 있다는 것입니다.

이 비트 :

"${f%.pdf}.txt" 

은 그 값 문자열을 만드는 "된 .txt" f%.pdf라는 변수를 찾아 추가 요구하고있다. 그런 변수가 없으므로 ".txt"만 있으면됩니다.

두 번째 문제점 (내가 추측하고있는)은 해당 디렉토리에있는 모든 PDF 파일을 반복하려는 것입니다. 그러나 PowerShell에 지시하지 않았습니다. 그래서 함께

, 나는이 코드를 원한다고 생각 :

$PATH="D:/PDFdirectory" 

foreach ($file in Get-ChildItem $PATH -Include *.pdf) { 
    python pdf2txt.py -o "$($f.BaseName).txt" -O $f.DirectoryName ($f.FullName -replace '.pdf$','.txt') 
} 

더 설명 :

  • $f.DirectoryName - 파일의 이름 - 파일
  • $f.BaseName이있는 디렉토리의 경로 확장자가없는 경우
  • "$($f.BaseName).txt" - 괄호는 이전에 실행될 하위 식을 표시합니다. 최종 문자열을 생성합니다.
  • ($f.FullName -replace '.pdf$','.txt') - 정규식 바꾸기를 사용하여 전체 파일 이름 (및 경로)의 끝에 .pdf을 찾아 .txt으로 바꿉니다.
+0

DirectoryName이 파일의 위치 또는 pdf2txt.py의 위치를 ​​가리 킵니까? 및 $ f.BaseName == 파일? – ChuckD

+0

PDF 파일의 위치. – JasonMArcher

+0

오류는 없지만 아무 일도 발생하지 않습니다. – ChuckD

관련 문제