2

专家们,

我有一个模板 docx 报告,里面有图像和标准格式。我使用 docx 所做的只是搜索一些标签,并使用配置文件中的值替换它。

搜索和替换按预期工作,但输出文件丢失了所有图像和格式。你知道出了什么问题吗?我所做的只是修改 example-makedocument.py,并将其替换为我的 docx 文件。

我搜索了关于 python.docx librelist 的讨论,以及他们在 github 上的页面,有很多这样的问题,但仍未得到解答。

谢谢你。

--- 我的脚本很简单 ---

from docx import *
from ConfigParser import SafeConfigParser

filename = "template.docx"

document = opendocx(filename)
relationships = relationshiplist()
body = document.xpath('/w:document/w:body',namespaces=nsprefixes)[0]

####### get config file
parser = SafeConfigParser()
parser.read('../TESTING1-config.txt')

######## Search and replace
print 'Searching for something in a paragraph ...',
if search(body, ''):
print 'found it!'
else:
print 'nope.'

print 'Replacing ...',
body = advReplace(body, '', parser.get('ASD', 'ASD'))
print 'done.'

####### #Create our properties, contenttypes, and other support files
title = 'Python docx demo'
subject = 'A practical example of making docx from Python'
creator = 'Mike MacCana'
keywords = ['python', 'Office Open XML', 'Word']

coreprops = coreproperties(title=title, subject=subject, creator=creator,keywords=keywords)
appprops = appproperties()
contenttypes = contenttypes()
websettings = websettings()
wordrelationships = wordrelationships(relationships)

savedocx(document, coreprops, appprops, contenttypes, websettings, wordrelationships, 'Welcome to the Python docx module.docx')
4

2 回答 2

2

Python-docx only copies over the document.xml file in the original Docx zip. Everything else is discarded and recreated either from a function or from a preexisting template file. This unfortunately includes the document.xml.rels file that is responsible for mapping images.

The oodocx module that I have developed copies over everything from the old Docx and, at least in my experience, plays nicely with images.

于 2013-12-11T15:39:59.280 回答
0

我已经回答了一个关于 python-docx 的类似问题。Python docx 并不意味着存储 docx 图像并将它们导出。

Python Docx 不是 Docx 的模板引擎。

于 2013-07-26T08:37:42.007 回答