简体   繁体   English

使用python将rtf转换为pdf

[英]Converting rtf to pdf using python

I am new to the python language and I am given a task to convert rtf to pdf using python.我是 python 语言的新手,我被赋予了使用 python 将 rtf 转换为 pdf 的任务。 I googled and found some code- (not exactly rtf to pdf) but I tried working on it and changed it according to my requirement.我用谷歌搜索并找到了一些代码-(不完全是 rtf 到 pdf)但我尝试处理它并根据我的要求对其进行了更改。 But I am not able to solve it.但我无法解决它。

I have used the below code:我使用了以下代码:

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()

But its throwing the below error但它抛出以下错误

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

Can anyone help me with this?谁能帮我这个? I would really appreciate if someone can find the better and fast way of doing it.如果有人能找到更好更快的方法,我将不胜感激。 I have around 200,000 files to convert.我有大约 200,000 个文件要转换。

Anisha阿尼莎

I used Marks's advice and changed it back to Word.Application and my source pointing to rtf files.我使用了 Marks 的建议并将其改回 Word.Application 和指向 rtf 文件的源代码。 Works perfectly!完美运行! - the process was slow but still faster than the JAVA application which my team was using. - 这个过程很慢,但仍然比我的团队使用的 JAVA 应用程序快。 I have attached the final code in my question.我在我的问题中附上了最终代码。

Final Code: Got it done using the code which works with Word application :最终代码:使用适用于 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()

If you have Libre Office in your system, you got the best solution.如果您的系统中有 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')

Commands may vary to different versions and platforms.命令可能因版本和平台而异。 But this would be the best solution ever I had.但这将是我曾经拥有的最好的解决方案。

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

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