import sys
import os.path
from pyth.plugins.rtf15.reader import Rtf15Reader
from pyth.plugins.xhtml.writer import XHTMLWriter
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
filename = os.path.normpath(os.path.join(
os.path.dirname(__file__),
'HW1_TRPL_Stranks.rtf'))
doc = Rtf15Reader.read(open(filename, "r"))
print(XHTMLWriter.write(doc, pretty=True).read())有人知道如何将.rtf文件导入python吗?我正在尝试堆栈中的一些东西,但它不起作用。
更新:我安装了pyth模块,但它一直告诉我模块没有找到This is the error I got
发布于 2020-02-12 22:07:48
从围绕stackoverflow提出的几个选项中,我认为最可靠的是使用命令行LibreOffice。
shell中的命令应如下所示:
soffice --headless --convert-to html file.rtf以防你想转换成html,而不是松散的格式化。txt也是一个选择。
在Python3.6中,您将拥有
from subprocess import call
call(["soffice", "--headless", "--convert-to", "html", "file.rtf"])然后将文件读入一个变量:
with open('file.html', 'r') as myfile:
data=myfile.read().replace('\n', '')重要提示:如果LibreOffice的任何实例处于打开状态,则此操作将不起作用。因此,在运行它之前,请关闭所有LibreOffice文件。
https://stackoverflow.com/questions/48388460
复制相似问题