精华内容
下载资源
问答
  • 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]) ...

    The oodocx module mentioned in the same page refers the user to an /examples folder that does not seem to be there.

    I have read the documentation of python-docx 0.7.2, plus everything I could find in Stackoverflow on the subject, so please believe that I have done my “homework”.

    Python is the only language I know (beginner+, maybe intermediate), so please do not assume any knowledge of C, Unix, xml, etc.

    Task : Open a ms-word 2007+ document with a single line of text in it (to keep things simple) and replace any “key” word in Dictionary that occurs in that line of text with its dictionary value. Then close the document keeping everything else the same.

    Line of text (for example) “We shall linger in the chambers of the sea.”

    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')

    I am not seeing anything in the documentation that allows me to do this—maybe it is there but I don’t get it because everything is not spelled-out at my level.

    I have followed other suggestions on this site and have tried to use earlier versions of the module (https://github.com/mikemaccana/python-docx) that is supposed to have "methods like replace, advReplace" as follows: I open the source-code in the python interpreter, and add the following at the end (this is to avoid clashes with the already installed version 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)

    Running this produces the following error message:

    NameError: name 'coreprops' is not defined

    Maybe I am trying to do something that cannot be done—but I would appreciate your help if I am missing something simple.

    If this matters, I am using the 64 bit version of Enthought's Canopy on OSX 10.9.3

    解决方案

    The current version of python-docx does not have a search() function or a replace() function. These are requested fairly frequently, but an implementation for the general case is quite tricky and it hasn't risen to the top of the backlog yet.

    Several folks have had success though, getting done what they need, using the facilities already present. Here's an example. It has nothing to do with sections by the way :)

    for paragraph in document.paragraphs:

    if 'sea' in paragraph.text:

    print paragraph.text

    paragraph.text = 'new text containing ocean'

    To search in Tables as well, you would need to use something like:

    for table in document.tables:

    for cell in table.cells:

    for paragraph in cell.paragraphs:

    if 'sea' in paragraph.text:

    ...

    If you pursue this path, you'll probably discover pretty quickly what the complexities are. If you replace the entire text of a paragraph, that will remove any character-level formatting, like a word or phrase in bold or italic.

    By the way, the code from @wnnmaw's answer is for the legacy version of python-docx and won't work at all with versions after 0.3.0.

    展开全文
  • 替换行中的文本的问题是,文本可能会在多个运行中拆分,这意味着简单地查找和替换文本并不总是有效的。在要替换的文本可以拆分为多个运行,因此需要通过部分匹配进行搜索,确定哪些运行需要替换文本,然后替换已标识...

    替换行中的文本的问题是,文本可能会在多个运行中拆分,这意味着简单地查找和替换文本并不总是有效的。在

    要替换的文本可以拆分为多个运行,因此需要通过部分匹配进行搜索,确定哪些运行需要替换文本,然后替换已标识的文本中的文本。在

    此函数用于替换字符串并保留原始文本样式。无论是否需要保留样式设置,此过程都是相同的,因为样式设置会导致文本可能被拆分为多个行,即使文本在视觉上缺乏样式。在

    代码import docx

    def docx_find_replace_text(doc, search_text, replace_text):

    paragraphs = list(doc.paragraphs)

    for t in doc.tables:

    for row in t.rows:

    for cell in row.cells:

    for paragraph in cell.paragraphs:

    paragraphs.append(paragraph)

    for p in paragraphs:

    if search_text in p.text:

    inline = p.runs

    # Replace strings and retain the same style.

    # The text to be replaced can be split over several runs so

    # search through, identify which runs need to have text replaced

    # then replace the text in those identified

    started = False

    search_index = 0

    # found_runs is a list of (inline index, index of match, length of match)

    found_runs = list()

    found_all = False

    replace_done = False

    for i in range(len(inline)):

    # case 1: found in single run so short circuit the replace

    if search_text in inline[i].text and not started:

    found_runs.append((i, inline[i].text.find(search_text), len(search_text)))

    text = inline[i].text.replace(search_text, str(replace_text))

    inline[i].text = text

    replace_done = True

    found_all = True

    break

    if search_text[search_index] not in inline[i].text and not started:

    # keep looking ...

    continue

    # case 2: search for partial text, find first run

    if search_text[search_index] in inline[i].text and inline[i].text[-1] in search_text and not started:

    # check sequence

    start_index = inline[i].text.find(search_text[search_index])

    check_length = len(inline[i].text)

    for text_index in range(start_index, check_length):

    if inline[i].text[text_index] != search_text[search_index]:

    # no match so must be false positive

    break

    if search_index == 0:

    started = True

    chars_found = check_length - start_index

    search_index += chars_found

    found_runs.append((i, start_index, chars_found))

    if search_index != len(search_text):

    continue

    else:

    # found all chars in search_text

    found_all = True

    break

    # case 2: search for partial text, find subsequent run

    if search_text[search_index] in inline[i].text and started and not found_all:

    # check sequence

    chars_found = 0

    check_length = len(inline[i].text)

    for text_index in range(0, check_length):

    if inline[i].text[text_index] == search_text[search_index]:

    search_index += 1

    chars_found += 1

    else:

    break

    # no match so must be end

    found_runs.append((i, 0, chars_found))

    if search_index == len(search_text):

    found_all = True

    break

    if found_all and not replace_done:

    for i, item in enumerate(found_runs):

    index, start, length = [t for t in item]

    if i == 0:

    text = inline[index].text.replace(inline[index].text[start:start + length], str(replace_text))

    inline[index].text = text

    else:

    text = inline[index].text.replace(inline[index].text[start:start + length], '')

    inline[index].text = text

    # print(p.text)

    # sample usage as per example

    doc = docx.Document('find_replace_test_document.docx')

    docx_find_replace_text(doc, 'Testing1', 'Test ')

    docx_find_replace_text(doc, 'Testing2', 'Test ')

    docx_find_replace_text(doc, 'rest', 'TEST')

    doc.save('find_replace_test_result.docx')

    样本输出

    下面是几个屏幕截图,显示源文档和替换文本后的结果:

    ^{pr2}$

    源文档:

    yTn9N.png

    结果文档:

    xyLMZ.png

    我希望这对某人有帮助。在

    展开全文
  • importdocxdefdocx_find_replace_text(doc,search_text,replace_text):paragraphs=list(doc.paragraphs)fortindoc.tables:forrowint.rows:forcellinrow.cells:forparagraphincell.paragraphs:paragraphs.append(para...

    importdocxdefdocx_find_replace_text(doc,search_text,replace_text):paragraphs=list(doc.paragraphs)fortindoc.tables:forrowint.rows:forcellinrow.cells:forparagraphincell.paragraphs:paragraphs.append(paragraph)forpinparagraphs:ifsearch_textinp.text:inline=p.runs# Replace strings and retain the same style.# The text to be replaced can be split over several runs so# search through, identify which runs need to have text replaced# then replace the text in those identifiedstarted=Falsesearch_index=0# found_runs is a list of (inline index, index of match, length of match)found_runs=list()found_all=Falsereplace_done=Falseforiinrange(len(inline)):# case 1: found in single run so short circuit the replaceifsearch_textininline[i].textandnotstarted:found_runs.append((i,inline[i].text.find(search_text),len(search_text)))text=inline[i].text.replace(search_text,str(replace_text))inline[i].text=text

    replace_done=Truefound_all=Truebreakifsearch_text[search_index]notininline[i].textandnotstarted:# keep looking ...continue# case 2: search for partial text, find first runifsearch_text[search_index]ininline[i].textandinline[i].text[-1]insearch_textandnotstarted:# check sequencestart_index=inline[i].text.find(search_text[search_index])check_length=len(inline[i].text)fortext_indexinrange(start_index,check_length):ifinline[i].text[text_index]!=search_text[search_index]:# no match so must be false positivebreakifsearch_index==0:started=Truechars_found=check_length-start_index

    search_index+=chars_found

    found_runs.append((i,start_index,chars_found))ifsearch_index!=len(search_text):continueelse:# found all chars in search_textfound_all=Truebreak# case 2: search for partial text, find subsequent runifsearch_text[search_index]ininline[i].textandstartedandnotfound_all:# check sequencechars_found=0check_length=len(inline[i].text)fortext_indexinrange(0,check_length):ifinline[i].text[text_index]==search_text[search_index]:search_index+=1chars_found+=1else:break# no match so must be endfound_runs.append((i,0,chars_found))ifsearch_index==len(search_text):found_all=Truebreakiffound_allandnotreplace_done:fori,iteminenumerate(found_runs):index,start,length=[tfortinitem]ifi==0:text=inline[index].text.replace(inline[index].text[start:start+length],str(replace_text))inline[index].text=textelse:text=inline[index].text.replace(inline[index].text[start:start+length],'')inline[index].text=text# print(p.text)# sample usage as per exampledoc=docx.Document('find_replace_test_document.docx')docx_find_replace_text(doc,'Testing1','Test ')docx_find_replace_text(doc,'Testing2','Test ')docx_find_replace_text(doc,'rest','TEST')doc.save('find_replace_test_result.docx')

    展开全文
  • WORD文档替换功能如果使用好的话,会给我们日常工作带来很大帮助,所以今天这篇文章主要讲解Word文档替换功能怎么使用,替换作用就是将查找一个内容可以使用别外一个内容进行替换。替换对话框快捷键是“CTRL+H”; ...

    WORD文档替换功能如果使用好的话,会给我们日常工作带来很大帮助,所以今天这篇文章主要讲解Word文档替换功能怎么使用,替换作用就是将查找一个内容可以使用别外一个内容进行替换。替换对话框快捷键是“CTRL+H”;

    【文本快速替换】

    文章中已有的文本可以使用另外的文本替换掉,如“读书屋”使用“读书屋OFFICE网”进行快速替换

    操作方法:打开替换对话框,在查找内容下填写“读书屋”,在替换为输入“读书屋OFFICE网”后单击“全部替换”按钮,这时WORD程序就会把文档中所有是读书屋文本进行替换。

    小技巧:如果文章某个词多处出现,而又想快速将其删除,那么可以使用替换命令快速完成。只输入查找内容,而替换为的文本框什么也不填写。

    【文本替换格式】

     

    替换命令可以将文档中只替换其格式,而对文本没有任何影响,通过一个示例,将文档中加粗的文本替换成加粗倾斜字号加大并且改变文字颜色

    操作方法:打开“替换对话框”然后先单击“更多按钮”,把光标定位在查找内容对应的文本框里,然后再单击下方“格式”按钮在列表命令框选择“字体”命令,如下图

     

    在弹出的字体对话框,选择加粗后确定,把鼠标光标定位在替换为文本框中,再次单击【格式】—【字体】然后根据需要设置格式即可,设置后效果如下图。

    最后去掉“使用通配符”选项,然后单击全部替换即可完成该替换效果。如下图

    小技巧:使用这个方法给文本添加格式,也是通过替换实现的,查找内容与替换为内容是一样的,不同的是先单击“更多按钮”然后定位光标在替换为后,再单击【格式】—【字体】设置即可。

    转载请注明出处:读书屋office教程网;作者陈飞;

    来源链接:http://www.dushuwu.net/xqkan.aspx?newsid=531

    展开全文
  • word文档快捷键

    2020-02-17 09:59:11
    1、标题分成两行,用“shift”+“enter”。 2、在文档中查找某变量出现在哪几处或者替换,“ctrl”+“H”。
  • 导语: Word我想大家应该都不陌生吧,不管是科技落后的20世纪,还是科技高度发达的今天,word文档从诞生之始就一直是我们电脑办公最为重要的工具。现在我们不论是学习、工作、还是休闲娱乐,到处都可以见到word的...
  • word的相关应用 Word中常用的快捷键 ctrl+n新建 v+2就可以打出这个 ctrl+空格可以快速实现中文输入法与英文输入法之间的切换 insert键是插入或改写的功能 ctrl+F就是快速实现查找与替换功能ctrl+h 制表位在拖住标尺...
  • word教程之查找替换功能使用非常频繁,因此掌握word查找和替换快捷键也是必须的。但是由于由于版本不一样,使用就略有所不同。 第一,word 2007版本查找替换快捷键  首先,我们看看word 2007版本,在“开始——...
  • Word无格式粘贴快捷键

    2021-02-20 10:40:36
    最近在使用word文档写东西,发现粘贴出来的总是带格式,要么手动选择,要么就是把复制的东西先转到文本编辑器中"中转"一下。 在网上找了一个宏的方法,但是录制下来几次都并不好使。所以在网上找到了一个宏的无格式...
  • 通过计算机二级考试,是大学的必修课,好比英语四六...Excel二级考试必备快捷键:——Word快捷键1)Ctrl+N创建新文档2)Ctrl+O打开文档3)Ctrl+W关闭文档4)Alt+Ctrl+S拆分文档窗口5)Alt+shift+C撤消拆分文档窗口6)Alt+C...
  • 一、空行替换在日常工作中,我们经常从网上下载一些...但有一些用上面的方法却是屡试屡败,每次都是提示:“Word已完成对文档的搜索并已完成0处替换”。为什么空行就在眼前,Word却不认识它呢?在用替换的办法删除...
  • 说到文档,相信大部分人的脑子里蹦出来的第一个词就是Word。无论是在上学的孩子,还是在工作的我们,总是习惯地使用Word去编辑文档,至少在我心中,Word还是一个无可取代的编辑软件。 今天我们就来聊一聊关于Word的...
  • Word快捷键

    千次阅读 2013-11-16 14:34:34
    常用快捷键 快捷键 作用 Ctrl+Shift+Spacebar创建不间断空格 Ctrl+ -(连字符) 创建不间断连字符 Ctrl+B使字符变为粗体 Ctrl+I使字符变为斜体 Ctrl+U为字符添加下划线 Ctrl+Shift+ 缩小字号 Ctrl+Shift+>...
  • ▲Ctrl组合快捷键[ctrl+1] 组合键:1倍行距[ctrl+2] 组合键:2倍行距[ctrl+5] 组合键:1.5倍行距[ctrl+shift+D] 组合键:分散对齐[ctrl+L] 组合键:左对齐[ctrl+R] 组合键:右对齐[ctrl+E] 组合键:段落居中[ctrl+J]...
  • Word快捷键大全

    2017-10-25 11:13:10
    WPS文字快捷键 WPS表格快捷键 WPS演示快捷键 WPS快捷键大全 显示和使用窗口 切换到下一个窗口。 Alt+Tab 切换到上一个窗口。 Alt+Shift+Tab 关闭活动窗口。 Ctr
  • 强烈建议开发者认真阅读本文档,掌握md及HBuilderX对md的强大支持。 窄屏幕下,可按Alt+滚轮横向滚动 很多人只把markdown用于网络文章发表,这糟蹋了markdown。 markdown不止是HTML的简化版,更重要的是txt的升级版...
  • 经常需要在word文档在粘贴纯文本内容,但操作繁琐,用快捷键简化。 1.打开word 2007,按Alt+F11打开标题为“Visaul Basic- Nornal“”的窗口,并右击工程面板中的“Normal“项,如下图,最后选“模块“” 此时,会...
  • word 快捷键

    2018-06-12 12:19:26
    1.查找加粗的字体 先选定加粗的字体, 之后点击右上角的 查找 下拉 的高级查找 , 之后 点击格式 ,之后选择 加粗...之后把所选的文字全部删去,之后点击查找就可以了 ...6.多级编号的使用技巧和快捷键: 1) shift...
  • Word文档用通配符批量替换日期格式

    千次阅读 2017-12-18 13:05:38
    = =搬砖的过程中要做一件很痛苦的事情就是把word上的任务转到团队任务管理的软件上去.....这里我们可以用word自带的批量替换功能,用通配符去匹配然后批量替换开始菜单栏 -> 替换或者使用快捷键’Ctrl+H’第一种格式的
  • WORD 快捷键

    2019-09-21 00:46:13
    WORD 快捷键 查询快捷键的方法 1、启动Word 2003后,单击“工具”菜单并指向其中的“宏”选项,再单击其级联菜单中的“宏”命令,系统弹出“宏”对话框,在“宏的位置”一栏中选择“Word命令”,然后在...
  • Word2016各种快捷键

    千次阅读 2019-05-11 17:26:31
    Word作为办公软件中常用的工具,如果能熟练的运用其快捷键,对工作有较大的帮助。接下来我将给大家介绍其快捷键快捷键 [F1]键: 帮助 [F2]键: 移动文字或图形,按回车键确认 [F4]键: 重复上一次的...
  • word快捷键

    2020-09-19 14:06:06
    word快捷键:bai ctrl+a 全选 ctrl+b 粗体 ctrl+c 拷贝 ctrl+d 字体格式 ctrk+e 居中对齐 ctrl+f 查找du ctrl+g 定位 ctrl+h 替换 ctrl+i 斜体 ctrl+j 两端对齐 ctrl+k 超级链接zhi ctrl+l 左对齐 ctrl+m 左缩进 ...
  • 一、空行替换  在日常工作中,我们经常从网上下载一些文字材料,往往因...但有一些用上面的方法却是屡试屡败,每次都是提示:“Word已完成对文档的搜索并已完成0处替换”。为什么空行就在眼前,Word却不认识它呢? 
  • Word常用快捷键

    2021-01-06 17:22:40
    Word 常用快捷键 word快捷键 对应功能 Ctrl + Shift + P 打开字体设置 Ctrl + B 字体变粗体 Ctrl + I 字体变斜体 Ctrl + U 字体添加下划线 Ctrl +[ 字号减小 1 磅 Ctrl +] 字号增大 1 磅 Ctrl +...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 9,992
精华内容 3,996
关键字:

word文档替换文字快捷键