2012-02-21 24 views
10

को नियंत्रित करने के लिए pywin32 का उपयोग करते समय अपवाद मैंने हाल ही में ठीक काम करने तक पीडीएफ फाइलों को सहेजने के लिए pywin32 का उपयोग करके पायथन में एक स्क्रिप्ट लिखी है। मैं Excel में समान तरीकों का उपयोग करता हूं। कोड के नीचे है:"लागू नहीं किया गया" एडोब एक्रोबैट

def __pdf2Txt(self, pdf, fileformat="com.adobe.acrobat.accesstext"): 
    outputLoc = os.path.dirname(pdf) 
    outputLoc = os.path.join(outputLoc, os.path.splitext(os.path.basename(pdf))[0] + '.txt') 

    try: 
     win32com.client.gencache.EnsureModule('{E64169B3-3592-47d2-816E-602C5C13F328}', 0, 1, 1) 
     adobe = win32com.client.DispatchEx('AcroExch.App') 
     pdDoc = win32com.client.DispatchEx('AcroExch.PDDoc') 
     pdDoc.Open(pdf) 
     jObject = pdDoc.GetJSObject() 
     jObject.SaveAs(outputLoc, "com.adobe.acrobat.accesstext") 
    except: 
     traceback.print_exc() 
     return False 
    finally: 
     del jObject 
     pdDoc.Close() 
     del pdDoc 
     adobe.Exit() 
     del adobe 

हालांकि इस कोड को अचानक काम करना बंद कर दिया है और मैं निम्नलिखित उत्पादन प्राप्त करें:

Traceback (most recent call last): 
    File "C:\Documents and Settings\ablishen\workspace\HooverKeyCreator\src\HooverKeyCreator.py", line 38, in __pdf2Txt 
    jObject.SaveAs(outputLoc, "com.adobe.acrobat.accesstext") 
    File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 505, in __getattr__ 
    ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1) 
com_error: (-2147467263, 'Not implemented', None, None) 
False 

मैं समान कोड VB में लिखा है कि सही ढंग से काम करता है तो मैं यह अनुमान लगा रहा हूँ कि COM इंटरफ़ेस के साथ कुछ उचित कार्य करने के लिए बाध्यकारी नहीं है? (मेरा COM ज्ञान पैची है)।

+2

इस पीडीएफ उपयोग अधिकार को बचाने के है? (डॉक्स से इस पर आधारित जंगली अनुमान: "यह विधि उन दस्तावेज़ों के लिए एडोब रीडर में उपलब्ध है जिनके पास उपयोग अधिकारों को बचाया गया है।) –

+1

ऐसा प्रतीत नहीं हुआ लेकिन मैंने उन्हें सक्षम किया और अभी भी वही त्रुटि प्राप्त की। इसके अलावा मैं एडोब एक्रोबैट का उपयोग कर रहा हूं कोड चलाने के लिए। – Blish

उत्तर

3

ब्लिश, this thread समाधान आप के लिए महत्वपूर्ण मानती है की तलाश कर रहे: https://mail.python.org/pipermail/python-win32/2002-March/000260.html

मैं स्वीकार करते हैं कि पोस्ट के ऊपर (सबसे आसान लगता है शायद नहीं है क्योंकि Google सामग्री की उम्र के आधार पर कम स्कोर करता है?)।

विशेष रूप से, सलाह के this piece लागू करने के मिल जाएगा आप के लिए चल बातें: https://mail.python.org/pipermail/python-win32/2002-March/000265.html

संदर्भ के लिए, कोड की पूरी टुकड़ा है कि आप मैन्युअल dynamic.py पैच करने के लिए की आवश्यकता नहीं है (टुकड़ा काफी से बाहर चलाने चाहिए बॉक्स):

# gets all files under ROOT_INPUT_PATH with FILE_EXTENSION and tries to extract text from them into ROOT_OUTPUT_PATH with same filename as the input file but with INPUT_FILE_EXTENSION replaced by OUTPUT_FILE_EXTENSION 
from win32com.client import Dispatch 
from win32com.client.dynamic import ERRORS_BAD_CONTEXT 

import winerror 

# try importing scandir and if found, use it as it's a few magnitudes of an order faster than stock os.walk 
try: 
    from scandir import walk 
except ImportError: 
    from os import walk 

import fnmatch 

import sys 
import os 

ROOT_INPUT_PATH = None 
ROOT_OUTPUT_PATH = None 
INPUT_FILE_EXTENSION = "*.pdf" 
OUTPUT_FILE_EXTENSION = ".txt" 

def acrobat_extract_text(f_path, f_path_out, f_basename, f_ext): 
    avDoc = Dispatch("AcroExch.AVDoc") # Connect to Adobe Acrobat 

    # Open the input file (as a pdf) 
    ret = avDoc.Open(f_path, f_path) 
    assert(ret) # FIXME: Documentation says "-1 if the file was opened successfully, 0 otherwise", but this is a bool in practise? 

    pdDoc = avDoc.GetPDDoc() 

    dst = os.path.join(f_path_out, ''.join((f_basename, f_ext))) 

    # Adobe documentation says "For that reason, you must rely on the documentation to know what functionality is available through the JSObject interface. For details, see the JavaScript for Acrobat API Reference" 
    jsObject = pdDoc.GetJSObject() 

    # Here you can save as many other types by using, for instance: "com.adobe.acrobat.xml" 
    jsObject.SaveAs(dst, "com.adobe.acrobat.accesstext") 

    pdDoc.Close() 
    avDoc.Close(True) # We want this to close Acrobat, as otherwise Acrobat is going to refuse processing any further files after a certain threshold of open files are reached (for example 50 PDFs) 
    del pdDoc 

if __name__ == "__main__": 
    assert(5 == len(sys.argv)), sys.argv # <script name>, <script_file_input_path>, <script_file_input_extension>, <script_file_output_path>, <script_file_output_extension> 

    #$ python get.txt.from.multiple.pdf.py 'C:\input' '*.pdf' 'C:\output' '.txt' 

    ROOT_INPUT_PATH = sys.argv[1] 
    INPUT_FILE_EXTENSION = sys.argv[2] 
    ROOT_OUTPUT_PATH = sys.argv[3] 
    OUTPUT_FILE_EXTENSION = sys.argv[4] 

    # tuples are of schema (path_to_file, filename) 
    matching_files = ((os.path.join(_root, filename), os.path.splitext(filename)[0]) for _root, _dirs, _files in walk(ROOT_INPUT_PATH) for filename in fnmatch.filter(_files, INPUT_FILE_EXTENSION)) 

    # patch ERRORS_BAD_CONTEXT as per https://mail.python.org/pipermail/python-win32/2002-March/000265.html 
    global ERRORS_BAD_CONTEXT 
    ERRORS_BAD_CONTEXT.append(winerror.E_NOTIMPL) 

    for filename_with_path, filename_without_extension in matching_files: 
     print "Processing '{}'".format(filename_without_extension) 
     acrobat_extract_text(filename_with_path, ROOT_OUTPUT_PATH, filename_without_extension, OUTPUT_FILE_EXTENSION) 

मैं WinPython 64 2.7.6.3 पर इस परीक्षण किया है, एक्रोबेट एक्स प्रो

+1

dynamic.py में ERRORS_BAD_CONTEXT सूची में winerror.E_NOTIMPL जोड़ना। बहुत बहुत धन्यवाद! – Blish

+1

हाय, मैं उसी फ़ंक्शन के लिए पाइथन और एक्रोबैट रीडर प्रो का उपयोग कर रहा हूं, और वर्तमान में यह कोड और उसके बाद भी पिछली टिप्पणीकर्ता ने जो किया, वह मुझे निम्नलिखित त्रुटि देता है: "NotAllowedError: सुरक्षा सेटिंग्स इस संपत्ति या विधि तक पहुंच को रोकती हैं।" क्या आप जानते हैं कि इसका क्या कारण है? धन्यवाद – dasen

+2

मैं आपको 'ERRORS_BAD_CONTEXT.append (winerror.E_NOTIMPL) 'लाइन। – Fenikso

1

makepy.py एक स्क्रिप्ट है जो win32com पायथन पैकेज के साथ आता है।

विंडोज़ में COM/OLE ऑब्जेक्ट में आपके संस्थापन "तार" पायथन के लिए इसे चला रहा है। निम्नलिखित कुछ कोड का एक अंश है जो मैं एक्सेल से बात करता था और इसमें कुछ सामान करता था। इस उदाहरण को वर्तमान कार्यपुस्तिका में शीट 1 का नाम मिलता है। यह स्वचालित रूप से makepy अगर यह एक अपवाद है चलाता है:

import win32com; 
import win32com.client; 
from win32com.client import selecttlb; 

def attachExcelCOM(): 
    makepyExe = r'python C:\Python25\Lib\site-packages\win32com\client\makepy.py'; 
    typeList = selecttlb.EnumTlbs(); 
    for tl in typeList: 
     if (re.match('^Microsoft.*Excel.*', tl.desc, re.IGNORECASE)): 
      makepyCmd = "%s -d \"%s\"" % (makepyExe, tl.desc); 
      os.system(makepyCmd); 
     # end if 
    # end for 
# end def 

def getSheetName(sheetNum): 
    try: 
     xl = win32com.client.Dispatch("Excel.Application"); 
     wb = xl.Workbooks.Item(sheetNum); 
    except Exception, detail: 
     print 'There was a problem attaching to Excel, refreshing connect config...'; 
     print Exception, str(detail); 
     attachExcelCOM(); 
     try: 
     xl = win32com.client.Dispatch("Excel.Application"); 
     wb = xl.Workbooks.Item(sheetNum); 
     except: 
     print 'Could not attach to Excel...'; 
     sys.exit(-1); 
     # end try/except 
    # end try/except 

    wsName = wb.Name; 
    if (wsName == 'PERSONAL.XLS'): 
     return(None); 
    # end if 
    print 'The target worksheet is:'; 
    print '  ', wsName; 
    print 'Is this correct? [Y/N]',; 
    answer = string.strip(sys.stdin.readline()); 
    answer = answer.upper(); 
    if (answer != 'Y'): 
     print 'Sheet not identified correctly.'; 
     return(None); 
    # end if 
    return((wb, wsName)); 
# end def 

# -- Main -- 
sheetInfo = getSheetName(sheetNum); 
if (sheetInfo == None): 
    print 'Sheet not found'; 
    sys.exit(-1); 
else: 
    (wb, wsName) = sheetInfo; 
# end if 
संबंधित मुद्दे