Python 中的 Adob​​e Acrobat API [英] Adobe Acrobat API in Python

查看:58
本文介绍了Python 中的 Adob​​e Acrobat API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

系统:

Python 3.6
Windows 10

目标:

使用 Adob​​e Acrobat API 使用另存为"功能将 pdf 保存为 jpeg.

注意:出于我的目的,我不能使用 Wand 或其他软件包.

资源:

Adobe_API_文档

实施_示例 1

Error_Handling_Issue

VBA_Example

当前代码:

import winerror
import win32com
from win32com.client.dynamic import Dispatch, ERRORS_BAD_CONTEXT

ERRORS_BAD_CONTEXT.append(winerror.E_NOTIMPL)

my_dir = r"path\\to\\example\\"
my_pdf = "example.pdf"

os.chdir(my_dir)
src = os.path.abspath(my_pdf)

pdDoc = Dispatch("AcroExch.PDDoc")
pdDoc.Open(src)

jsObject = pdDoc.GetJSObject()

jsObject.SaveAs(os.path.abspath('./output_example.jpeg'), "com.adobe.acrobat.jpeg")

问题:

jsObject 为空

导致以下回溯:

Issue:

jsObject is Null

Resulting in the following traceback:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-26-9c82c454eb2a> in <module>()
----> 1 jsObject.SaveAs(os.path.abspath('./output_example.jpeg'), "com.adobe.acrobat.jpeg")

AttributeError: 'NoneType' object has no attribute 'SaveAs'

错误文档说明:

GetJSObject
Gets a dual interface to the JavaScript object associated with the PDDoc. This allows automation clients full access to both built-in and user-defined JavaScript methods available in the document. For more information on working with JavaScript, see Developing Applications Using Interapplication Communication.

Syntax
LDispatch* GetJSObject();

Returns
The interface to the JavaScript object if the call succeeded, NULL otherwise.

推荐答案

考虑与 AvDoc 对象接口作为您的链接之一显示其用法,然后构建 pdDocjsObject 来自它.请务必将进程包装在 try/except/finally 块中,以便有效地释放 COM 对象,而不管错误如何.

Consider interfacing with the AvDoc object as one of your links show its usage, and then build pdDoc and jsObject from it. Be sure to also wrap process in a try/except/finally block to effectively release COM objects regardless of error.

import os    
import winerror
from win32com.client.dynamic import Dispatch, ERRORS_BAD_CONTEXT

ERRORS_BAD_CONTEXT.append(winerror.E_NOTIMPL)

my_dir = r"C:\\path\\to\\example\\"
my_pdf = "example.pdf"

os.chdir(my_dir)
src = os.path.abspath(my_pdf)

try:
    AvDoc = Dispatch("AcroExch.AVDoc")    

    if AvDoc.Open(src, ""):            
        pdDoc = AvDoc.GetPDDoc()
        jsObject = pdDoc.GetJSObject()
        jsObject.SaveAs(os.path.join(my_dir, 'output_example.jpeg'), "com.adobe.acrobat.jpeg")

except Exception as e:
    print(str(e))

finally:        
    AvDoc.Close(True)

    jsObject = None
    pdDoc = None
    AvDoc = None

这篇关于Python 中的 Adob​​e Acrobat API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆