使用 python 将 rtf 转换为 pdf
Converting rtf to pdf using python
我是 python 语言的新手,我的任务是使用 python 将 rtf 转换为 pdf。我用谷歌搜索并找到了一些代码(不完全是 rtf 到 pdf),但我尝试对其进行处理并根据我的要求进行了更改。但是我无法解决它。
我使用了下面的代码:
import sys
import os
import comtypes.client
#import win32com.client
rtfFormatPDF = 17
in_file = os.path.abspath(sys.argv[1])
out_file = os.path.abspath(sys.argv[2])
rtf= comtypes.client.CreateObject('Rtf.Application')
rtf.Visible = True
doc = rtf.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=rtfFormatPDF)
doc.Close()
rtf.Quit()
但是它抛出以下错误
Traceback (most recent call last):
File "C:/Python34/Lib/idlelib/rtf_to_pdf.py", line 12, in <module>
word = comtypes.client.CreateObject('Rtf.Application')
File "C:\Python34\lib\site-packages\comtypes\client\__init__.py", line 227, in CreateObject
clsid = comtypes.GUID.from_progid(progid)
File "C:\Python34\lib\site-packages\comtypes\GUID.py", line 78, in from_progid
_CLSIDFromProgID(str(progid), byref(inst))
File "_ctypes/callproc.c", line 920, in GetResult
OSError: [WinError -2147221005] Invalid class string
谁能帮我解决这个问题?
如果有人能找到更好更快的方法,我将不胜感激。我有大约 200,000 个文件要转换。
阿妮莎
我采纳了 Marks 的建议并将其改回 Word.Application 并将我的源指向 rtf 文件。完美运行! - 这个过程很慢,但仍然比我的团队使用的 JAVA 应用程序快。我在问题中附上了最终代码。
最终代码:
使用与 Word 应用程序一起使用的代码完成它:
import sys
import os,os.path
import comtypes.client
wdFormatPDF = 17
input_dir = 'input directory'
output_dir = 'output directory'
for subdir, dirs, files in os.walk(input_dir):
for file in files:
in_file = os.path.join(subdir, file)
output_file = file.split('.')[0]
out_file = output_dir+output_file+'.pdf'
word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
如果您的系统中有 Libre Office,那么您就有了最佳解决方案。
import os
os.system('soffice --headless --convert-to pdf filename.rtf')
# os.system('libreoffice --headless -convert-to pdf filename.rtf')
# os.system('libreoffice6.3 --headless -convert-to pdf filename.rtf')
命令可能因版本和平台而异。但这将是我有史以来最好的解决方案。
我是 python 语言的新手,我的任务是使用 python 将 rtf 转换为 pdf。我用谷歌搜索并找到了一些代码(不完全是 rtf 到 pdf),但我尝试对其进行处理并根据我的要求进行了更改。但是我无法解决它。
我使用了下面的代码:
import sys
import os
import comtypes.client
#import win32com.client
rtfFormatPDF = 17
in_file = os.path.abspath(sys.argv[1])
out_file = os.path.abspath(sys.argv[2])
rtf= comtypes.client.CreateObject('Rtf.Application')
rtf.Visible = True
doc = rtf.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=rtfFormatPDF)
doc.Close()
rtf.Quit()
但是它抛出以下错误
Traceback (most recent call last):
File "C:/Python34/Lib/idlelib/rtf_to_pdf.py", line 12, in <module>
word = comtypes.client.CreateObject('Rtf.Application')
File "C:\Python34\lib\site-packages\comtypes\client\__init__.py", line 227, in CreateObject
clsid = comtypes.GUID.from_progid(progid)
File "C:\Python34\lib\site-packages\comtypes\GUID.py", line 78, in from_progid
_CLSIDFromProgID(str(progid), byref(inst))
File "_ctypes/callproc.c", line 920, in GetResult
OSError: [WinError -2147221005] Invalid class string
谁能帮我解决这个问题? 如果有人能找到更好更快的方法,我将不胜感激。我有大约 200,000 个文件要转换。
阿妮莎
我采纳了 Marks 的建议并将其改回 Word.Application 并将我的源指向 rtf 文件。完美运行! - 这个过程很慢,但仍然比我的团队使用的 JAVA 应用程序快。我在问题中附上了最终代码。
最终代码: 使用与 Word 应用程序一起使用的代码完成它:
import sys
import os,os.path
import comtypes.client
wdFormatPDF = 17
input_dir = 'input directory'
output_dir = 'output directory'
for subdir, dirs, files in os.walk(input_dir):
for file in files:
in_file = os.path.join(subdir, file)
output_file = file.split('.')[0]
out_file = output_dir+output_file+'.pdf'
word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
如果您的系统中有 Libre Office,那么您就有了最佳解决方案。
import os
os.system('soffice --headless --convert-to pdf filename.rtf')
# os.system('libreoffice --headless -convert-to pdf filename.rtf')
# os.system('libreoffice6.3 --headless -convert-to pdf filename.rtf')
命令可能因版本和平台而异。但这将是我有史以来最好的解决方案。