簡體   English   中英

使用python-docx搜索和替換后丟失格式和圖像

[英]Lost formatting and image after search and replace using python-docx

專家介紹,

我有一個模板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')

Python-docx僅復制原始Docx zip中的document.xml文件。 從函數或從現有的模板文件中刪除所有其他內容並重新創建。 不幸的是,這包括負責映射圖像的document.xml.rels文件。

我開發的oodocx模塊可以覆蓋舊Docx的所有內容,並且至少以我的經驗,它可以很好地處理圖像。

我已經回答了有關python-docx類似問題 Python docx並不意味着存儲docx圖像並將其導出。

Python Docx不是Docx的模板引擎。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM