我试图创建Element类的一个子类。不过,我开始有点困难。在
from lxml import etree
try:
import docx
except ImportError:
from docx import docx
class File(etree.ElementBase):
def _init(self):
etree.ElementBase._init(self)
self.body = self.append(docx.makeelement('body'))
f = File()
relationships = docx.relationshiplist()
title = 'File'
subject = 'A very special File'
creator = 'Me'
keywords = ['python', 'Office Open XML', 'Word']
coreprops = docx.coreproperties(title=title, subject=subject, creator=creator,
keywords=keywords)
appprops = docx.appproperties()
contenttypes = docx.contenttypes()
websettings = docx.websettings()
wordrelationships = docx.wordrelationships(relationships)
docx.savedocx(f, coreprops, appprops, contenttypes, websettings,
wordrelationships, 'file.docx')
当我试图打开从这段代码输出的文档时,我的Word(2003 with compatibility pack)版本给出了以下错误:“此文件是由Word 2007的早期测试版创建的,无法在此版本中打开。”当我用创建的其他元素替换file对象时docx.newdocument文件(),的文件出来没问题。有什么想法/建议吗?在
我真不明白你为什么要使用一个名为File的单独类。在
正如Michael0x2a所说,您没有放置文档标记,因此它无法工作(我认为word2007也无法读取您的文件)
但这是正确的代码:
相关问题 更多 >
编程相关推荐