0

好吧,我测试了我的 jython 程序,它做了一些简洁的 [".xls"、".doc"、".rtf"、".tif"、".tiff"、".pdf" 文件] -> pdf(中间文件) -> 使用 Open Office 进行 tif(最终输出)转换。由于自动化方面的问题,我们离开了 MS Office。现在看来,我们已经击倒了许多与显示塞子错误相关的瓶子,只剩下一瓶。OO 会在一段时间后挂起。

它发生在你在代码中看到这一行 '<<<<<<<<<<<'

什么是我处理停滞不前的 Open Office 流程的正确方法。您能否提供有用的链接,并在出路时给我一个好的建议。
还有一个问题。

总结:
* 如何处理停滞的 Open Office 实例?
* 如何使用 java headless 进行转换,所以我不会一直弹出 GUI 浪费内存。
* 任何关于代码质量、优化和通用编码标准的一般性建议也将不胜感激。


Traceback(最里面的最后一个):
文件“dcmail.py”,第 184 行,在?
文件“dcmail.py”,第 174 行,主
文件“C:\DCMail\digestemails.py”,第 126 行,process_inbox
文件“C:\DCMail\digestemails.py”,第 258 行,_convert
文件“C: \DCMail\digestemails.py",第 284 行,在 _choose_conversion_type
文件中 "C:\DCMail\digestemails.py",第 287 行,在 _open_office_convert
文件中 "C:\DCMail\digestemails.py",第 299 行,在 _load_attachment_to_convert
com.sun .star.lang.DisposedException:
com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge$MessageDi spatcher.run(java_remote_bridge.java:176)处的 java.io.EOFException

com.sun.star.lang.DisposedException: com.sun.star.lang.DisposedException: java.i o.EOFException

只是为了清除此异常仅在我终止打开的办公室进程时引发。否则程序只是等待开放办公室完成。无限期


代码(带有非功能代码标签)

[代码]

#ghost script handles these file types  
GS_WHITELIST=[".pdf"]  
#Open Office handles these file types  
OO_WHITELIST=[".xls", ".doc", ".rtf", ".tif", ".tiff"]   
#whitelist is used to check against any unsupported files.  
WHITELIST=GS_WHITELIST + OO_WHITELIST   
def _get_service_manager(self):
    try:
        self._context=Bootstrap.bootstrap();
        self._xMultiCompFactory=self._context.getServiceManager()
        self._xcomponentloader=UnoRuntime.queryInterface(XComponentLoader, self._xMultiCompFactory.createInstanceWithContext("com.sun.star.frame.Desktop", self._context))
    except:
        raise OpenOfficeException("Exception Occurred with Open Office")

def _choose_conversion_type(self,fn):
    ext=os.path.splitext(fn)[1]
    if ext in GS_WHITELIST:
        self._ghostscript_convert_to_tiff(fn)
    elif ext in OO_WHITELIST:
        self._open_office_convert(fn)

def _open_office_convert(self,fn):
    self._load_attachment_to_convert(fn)
    self._save_as_pdf(fn)
    self._ghostscript_convert_to_tiff(fn)

def _load_attachment_to_convert(self, file):
    file=self._create_UNO_File_URL(file)
    properties=[]
    p=PropertyValue()
    p.Name="Hidden"
    p.Value=True
    properties.append(p)
    properties=tuple(properties) 
    self._doc=self._xcomponentloader.loadComponentFromURL(file, "_blank",0, properties) <<<<<<<<<<<<<<< here is line 299

def _create_UNO_File_URL(self, filepath):
    try:
        file=str("file:///" + filepath)
        file=file.replace("\\", "/")
    except MalformedURLException, e:
        raise e
    return file

def _save_as_pdf(self, docSource):
    dirName=os.path.dirname(docSource)
    baseName=os.path.basename(docSource)
    baseName, ext=os.path.splitext(baseName)
    dirTmpPdfConverted=os.path.join(dirName + DIR + PDF_TEMP_CONVERT_DIR)
    if not os.path.exists(dirTmpPdfConverted):
        os.makedirs(dirTmpPdfConverted)
    pdfDest=os.path.join(dirTmpPdfConverted + DIR + baseName + ".pdf")
    url_save=self._create_UNO_File_URL(pdfDest)
    properties=self._create_properties(ext)
    try:
        try:
            self._xstorable=UnoRuntime.queryInterface(XStorable, self._doc);
            self._xstorable.storeToURL(url_save, properties)
        except AttributeError,e:
                self.logger.info("pdf file already created (" + str(e) + ")")
                raise e
    finally:
        try:
            self._doc.dispose()
        except:
            raise

def _create_properties(self,ext):
    properties=[]
    p=PropertyValue()
    p.Name="Overwrite"
    p.Value=True
    properties.append(p)
    p=PropertyValue()
    p.Name="FilterName"
    if   ext==".doc":
        p.Value='writer_pdf_Export'
    elif ext==".rtf":
        p.Value='writer_pdf_Export'
    elif ext==".xls":
        p.Value='calc_pdf_Export'
    elif ext==".tif":
        p.Value='draw_pdf_Export'
    elif ext==".tiff":
        p.Value='draw_pdf_Export'
    properties.append(p)
    return tuple(properties)

def _ghostscript_convert_to_tiff(self, docSource):
    dest, source=self._get_dest_and_source_conversion_file(docSource)
    try:
        command = ' '.join([
            self._ghostscriptPath + 'gswin32c.exe',
           '-q',
           '-dNOPAUSE',
           '-dBATCH',
           '-r500',
           '-sDEVICE=tiffg4',
           '-sPAPERSIZE=a4',
           '-sOutputFile=%s %s' % (dest, source),
           ])
        self._execute_ghostscript(command)
        self.convertedTifDocList.append(dest)
    except OSError, e:
        self.logger.info(e)
        raise e
    except TypeError, (e):
        raise e
    except AttributeError, (e):
        raise e
    except:
        raise

[/代码]

4

2 回答 2

1

OpenOffice.org 有一个“-headless”参数,可以在没有 GUI 的情况下运行它。我不确定这是否真的会释放所有用于 GUI 的资源。这是我运行服务器端无头实例的方式:

soffice -headless -accept="socket,port=1234;urp" -display :25

我不知道是什么导致了您的 Python 脚本出现停滞问题,但您可能想查看PyODConverter,看看这个脚本有什么不同,可能会发现导致您的麻烦的错误。

于 2008-11-19T09:48:45.890 回答
1

棘手的解决方案是为 OpenOffice 进程设置一个监视器。如果您的监视器知道 PID 并具有权限,它可以每隔几秒获取一次 CPU 时间。如果 OO 处于停滞状态(没有更多 CPU),则监视器可以杀死它。

处理此问题的最简单方法是让启动开放式办公室任务的“包装器”在它运行时监视它,并在它挂起时杀死它。父进程无论如何都必须等待,所以它也可以监视。

如果 OpenOffuce 陷入循环,则更难发现。CPU 通常会突破,停留在那里,优先级会下降到可能的最低优先级。处理或挂起?判决电话。你必须让它像这样挂“一段时间”(选择一个随机的持续时间,例如 432 秒(3 打);你总是会怀疑自己。)

于 2008-11-19T11:26:53.210 回答