専門家、
テンプレートの 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')