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 は、元の Docx zip 内の document.xml ファイルのみをコピーします。それ以外はすべて破棄され、関数または既存のテンプレート ファイルから再作成されます。残念ながら、これには画像のマッピングを担当する document.xml.rels ファイルが含まれています。

私が開発したodocxモジュールは、古い Docx のすべてをコピーしたものであり、少なくとも私の経験では、画像をうまく処理します。

于 2013-12-11T15:39:59.280 に答える