python rtf读取_在Python PyRTF中读取RTFs

在尝试使用PyRTF库读取RTF文件时遇到问题。代码示例创建了一个简单的RTF文档,但运行时出现‘NameError:未定义名称“Renderer”’。尽管已确认安装了PyRTF的最新版本(0.47.5),但错误仍然存在。寻求帮助解决此导入错误。
摘要由CSDN通过智能技术生成

我需要读一些.rtf文件,所以我安装了PyRTF。我在网上查找了一些实践中的PyRTF示例,发现如下:import sys

sys.path.append( '../' )

from PyRTF import *

SAMPLE_PARA = """The play opens one year after the death of Richard II, and

King Henry is making plans for a crusade to the Holy Land to cleanse himself

of the guilt he feels over the usurpation of Richard's crown. But the

crusade must be postponed when Henry learns that Welsh rebels, led by Owen

Glendower, have defeated and captured Mortimer. Although the brave Henry

Percy, nicknamed Hotspur, has quashed much of the uprising, there is still

much trouble in Scotland. King Henry has a deep admiration for Hotspur and

he longs for his own son, Prince Hal, to display some of Hotspur's noble

qualities. Hal is more comfortable in a tavern than on the battlefield, and

he spends his days carousing with riff-raff in London. But King Henry also

has his problems with the headstrong Hotspur, who refuses to turn over his

prisoners to the state as he has been so ordered. Westmoreland tells King

Henry that Hotspur has many of the traits of his uncle, Thomas Percy, the

Earl of Worcester, and defying authority runs in the family."""

def MakeExample1() :

doc = Document()

ss = doc.StyleSheet

section = Section()

doc.Sections.append( section )

# text can be added directly to the section

# a paragraph object is create as needed

section.append( 'Example 1' )

# blank paragraphs are just empty strings

section.append( '' )

# a lot of useful documents can be created

# with little more than this

section.append( 'A lot of useful documents can be created '

'in this way, more advance formating is available '

'but a lot of users just want to see their data come out '

'in something other than a text file.' )

return doc

def OpenFile( name ) :

return file( '%s.rtf' % name, 'w' )

if __name__ == '__main__' :

DR = Renderer()

doc1 = MakeExample1()

DR.Write( doc1, OpenFile( '1' ) )

print('Finished')

但是,当我尝试运行代码时,它似乎无法识别导入:

“NameError:未定义名称”“Renderer”“”

我检查了安装,它说我已经安装了最新版本(0.47.5)。有人能告诉我我做错了什么吗?在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值