简体   繁体   English

使用python与MS Word打开pdf文件时,如何抑制“ Microsoft PDF Reflow已停止工作”错误?

[英]How to suppress the “Microsoft PDF Reflow has stopped working” error when using python to open pdf file with MS Word?

I was trying to automate the pdf-to-docx process using python. 我正在尝试使用python自动化pdf-to-docx流程。 PDF Reflow will automatically opened if I open pdf files with MS Word. 如果我使用MS Word打开pdf文件,则会自动打开PDF Reflow。 Therefore, I used it as an OCR tool. 因此,我将其用作OCR工具。 I have suppressed the message boxes from Word by using word_app.DisplayAlerts=0 and try-except. 我已通过使用word_app.DisplayAlerts = 0和try-except禁止了Word中的消息框。 However, message box "Microsoft PDF Reflow has stopped working" still popped out sometimes. 但是,有时仍然弹出消息框“ Microsoft PDF Reflow停止工作”。

Now I should manually close those message boxes and it is not reasonable for an automated process. 现在,我应该手动关闭这些消息框,这对于自动化过程而言是不合理的。 Is there anyway to suppress the errors from PDF Reflow? 是否有任何方法可以抑制PDF重排中的错误?

Below is the code I use: 下面是我使用的代码:

import pythoncom
import win32com
from win32com.client import Dispatch, constants
#import logging

word_app = win32com.client.gencache.EnsureDispatch("Word.Application")

word_app.Visible = 0
word_app.DisplayAlerts = 0
wc = win32com.client.constants
try:
    word_doc = word_app.Documents.Open('file.pdf')
except pythoncom.com_error as e:
    print(e)
    #logger.info(e)
word_doc.SaveAs(FileName  = 'file.docx', FileFormat = wc.wdFormatXMLDocument)
word_doc.Close(SaveChanges = wc.wdDoNotSaveChanges)

word_app.Quit()

Thank you for any help! 感谢您的任何帮助!

Found answer by myself: 自己找到答案:

import win32com
shell_app = win32com.client.Dispatch('WScript.Shell')
RegKey = r'HKEY_CURRENT_USER\Software\Microsoft\Windows\Windows Error Reporting'
GetKeyValue = shell_app.RegRead(os.path.join(RegKey, "DontShowUI"))
EditKeyValue = shell_app.RegWrite(os.path.join(RegKey, "DontShowUI"), 1, "REG_DWORD")

##use this to restore your registry
RestoreKeyValue = shell_app.RegWrite(os.path.join(RegKey, "DontShowUI"), GetKeyValue, "REG_DWORD")

Be careful when using this code because it edits registry and may damage your computer. 使用此代码时请小心,因为它会编辑注册表并可能损坏您的计算机。

This code is not an ideal solution as it will suppress not only PDF Reflow's error reporting but ALL of them. 此代码不是理想的解决方案,因为它不仅会抑制PDF Reflow的错误报告,而且会抑制所有错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM