use*_*815 21 python text replace ms-word python-docx
同一页面中提到的oodocx模块将用户引用到似乎不存在的/ examples文件夹.
我已经阅读了python-docx 0.7.2的文档,以及我在Stackoverflow中可以找到的关于这个主题的所有内容,所以请相信我已经完成了我的"功课".
Python是我所知道的唯一语言(初学者+,也许是中级),所以请不要假设任何C,Unix,xml等知识.
任务:打开一个带有单行文本的ms-word 2007+文档(为了简单起见),并用字典值替换在该行文本中出现的Dictionary中的任何"关键"字.然后关闭文档,保持其他所有内容相同.
文本行(例如)"我们将在海中徘徊."
from docx import Document
document = Document('/Users/umityalcin/Desktop/Test.docx')
Dictionary = {‘sea’: “ocean”}
sections = document.sections
for section in sections:
print(section.start_type)
#Now, I would like to navigate, focus on, get to, whatever to the section that has my
#single line of text and execute a find/replace using the dictionary above.
#then save the document in the usual way.
document.save('/Users/umityalcin/Desktop/Test.docx')
Run Code Online (Sandbox Code Playgroud)
我没有在文档中看到任何允许我这样做的东西 - 也许它就在那里但是我没有得到它,因为我的关卡并没有拼写出来.
我已经关注了这个网站上的其他建议,并尝试使用该模块的早期版本(https://github.com/mikemaccana/python-docx)应该有"像replace,advReplace这样的方法",如下所示:我打开python解释器中的源代码,并在最后添加以下内容(这是为了避免与已安装的0.7.2版本冲突):
document = opendocx('/Users/umityalcin/Desktop/Test.docx')
words = document.xpath('//w:r', namespaces=document.nsmap)
for word in words:
if word in Dictionary.keys():
print "found it", Dictionary[word]
document = replace(document, word, Dictionary[word])
savedocx(document, coreprops, appprops, contenttypes, websettings,
wordrelationships, output, imagefiledict=None)
Run Code Online (Sandbox Code Playgroud)
运行此命令会产生以下错误消息:
NameError:未定义名称"coreprops"
也许我正在尝试做一些无法做到的事情 - 但如果我错过了一些简单的事情,我将非常感谢你的帮助.
如果这很重要,我在OSX 10.9.3上使用64位版本的Enthought's Canopy
sca*_*nny 31
当前版本的python-docx没有search()函数或replace()函数.这些都是相当频繁的请求,但是对于一般情况的实现是相当棘手的,并且还没有上升到积压的顶部.
然而,有些人已经取得了成功,使用已经存在的设施完成了他们需要的工作.这是一个例子.顺便说一句,这与部分无关:)
for paragraph in document.paragraphs:
if 'sea' in paragraph.text:
print paragraph.text
paragraph.text = 'new text containing ocean'
Run Code Online (Sandbox Code Playgroud)
要在Table中搜索,您还需要使用以下内容:
for table in document.tables:
for cell in table.cells:
for paragraph in cell.paragraphs:
if 'sea' in paragraph.text:
...
Run Code Online (Sandbox Code Playgroud)
如果你追求这条道路,你很快就会发现复杂性是什么.如果替换段落的整个文本,则会删除任何字符级格式,例如粗体或斜体的单词或短语.
顺便说一句,来自@ wnnmaw的答案的代码是针对python-docx的遗留版本,并且在0.3.0之后的版本中根本不起作用.
poi*_*oin 17
我从前面的答案中得到了很多帮助,但对我来说,以下代码的功能就像 word 中的简单查找和替换功能一样。希望这可以帮助。
#!pip install python-docx
#start from here if python-docx is installed
from docx import Document
#open the document
doc=Document('./test.docx')
Dictionary = {"sea": "ocean", "find_this_text":"new_text"}
for i in Dictionary:
for p in doc.paragraphs:
if p.text.find(i)>=0:
p.text=p.text.replace(i,Dictionary[i])
#save changed document
doc.save('./test.docx')
Run Code Online (Sandbox Code Playgroud)
上述解决方案有局限性。1) 包含“find_this_text”的段落将变成没有任何格式的纯文本,2) 与“find_this_text”在同一段落中的上下文控件将被删除,3) 上下文控件或表格中的“find_this_text”将被删除不被改变。
szu*_*zum 11
我需要一些东西来替换docx中的正则表达式.我接受了scannys的回答.为了处理样式,我使用了以下答案: Python docx在段落中替换字符串,同时保持样式 添加递归调用以处理嵌套表.并提出了这样的事情:
import re
from docx import Document
def docx_replace_regex(doc_obj, regex , replace):
for p in doc_obj.paragraphs:
if regex.search(p.text):
inline = p.runs
# Loop added to work with runs (strings with same style)
for i in range(len(inline)):
if regex.search(inline[i].text):
text = regex.sub(replace, inline[i].text)
inline[i].text = text
for table in doc_obj.tables:
for row in table.rows:
for cell in row.cells:
docx_replace_regex(cell, regex , replace)
regex1 = re.compile(r"your regex")
replace1 = r"your replace string"
filename = "test.docx"
doc = Document(filename)
docx_replace_regex(doc, regex1 , replace1)
doc.save('result1.docx')
Run Code Online (Sandbox Code Playgroud)
迭代字典:
for word, replacement in dictionary.items():
word_re=re.compile(word)
docx_replace_regex(doc, word_re , replacement)
Run Code Online (Sandbox Code Playgroud)
请注意,只有当整个正则表达式在文档中具有相同的样式时,此解决方案才会替换正则表达式.
此外,如果在保存相同样式文本后编辑文本,则可能在单独的运行中.例如,如果您打开具有"testabcd"字符串的文档并将其更改为"test1abcd"并保存,即使面团采用相同的样式,还有3个单独的运行"test","1"和"abcd",在这种情况下替换test1将无法正常工作.
这用于跟踪文档中的更改.要将其发送到一次运行,在Word中您需要转到"选项","信任中心"和"隐私选项",不要"存储随机数以提高组合准确度"并保存文档.
Jos*_*ush 11
分享我写的一个小脚本 - 帮助我生成.docx带有变量的法律合同,同时保留原始风格。
pip install python-docx
Run Code Online (Sandbox Code Playgroud)
例子:
pip install python-docx
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53687 次 |
| 最近记录: |