Word:使用Python删除表和标题之间的行 [英] Word : Deleting Lines in between a Table and a Heading using Python

查看:527
本文介绍了Word:使用Python删除表和标题之间的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方案,其中在整个Word文档中都出现一个标题/常量文本,例如调用树:" ,此后有几行,一旦行结束,有一个表格.因此,我希望使用python/win32组件删除表与标题/常量文本调用树:" 之间的线.

I have a scenario in which there is a Heading/Constant Text like "Call Tree:" present all over the Word document and after that there are some lines, and once the line are over, there is a Table. So I want the lines between the table and the Heading/Constant Text "Call Tree:" to be deleted using python/win32 component.

例如:

输入为:

...

Call Tree :

Line 1 ...

Line 2 ...

....

....

....

....

Line N ....

Table # 1

.....

输出为(即删除表和调用树"之间的所有行):

Output is (i.e. all the lines in between the table and "Call Tree" are deleted):

...

Call Tree :


Table # 1

.....

我不确定,是否可以通过某种方式在常量文本(即调用树"和表格)之间选择多行. 我知道,可以使用以下命令选择行并删除:

I'm not sure, if there is any way by which I can select multiple lines between a Constant Text i.e "Call Tree" and a Table. I know, that selection of a line and deletion can be done using this :

..

    app = win32com.client.DispatchEx("Word.Application")
    app.Visible = 0
    app.DisplayAlerts = 0

    # select a Text
    app.Selection.Find.Execute("TEXT TO BE SELECTED")
    #  extend it to end
    app.Selection.EndKey(Unit=win32com.client.constants.wdLine,Extend=win32com.client.constants.wdExtend)

    # check what has been selected
    app.Selection.Range()

    # and then delete it
    app.Selection.Delete()

..

但是我不确定如何根据此条件选择多行.

But I'm not sure, how to select multiple lines like this, with this criteria.

对此有任何想法/建议吗?

Any idea/suggestion on this ?

推荐答案

使用MoveRight和EndKey查找文本后,您可以遍历各行,然后可以使用属性Tables来测试所选内容是否是表.有点像

You can iterate over the lines after you find the text using MoveRight and EndKey, then you can test if the selection is a table using the property Tables. Someting like

import win32com.client as com
from win32com.client import constants as wcons

app = com.Dispatch('Word.Application')
# Find the text
app.Selection.Find.Execute('Call Tree',
        False, False, False, False, False,
        True, wcons.wdFindContinue, False,
        False,False,)
# Extend the selection to the end
app.Selection.EndKey(Unit=wcons.wdLine)
while True:
    # Move the selection to the next character
    app.Selection.MoveRight(Unit=wcons.wdCharacter, Count=1)
    # And Select the whole line
    app.Selection.EndKey(Unit=wcons.wdLine, Extend=wcons.wdExtend)
    # If I hit the table...
    if app.Selection.Tables.Count:
        print "Table found... Stop"
        # I leave the loop
        break
    # Otherwise delete the selection
    print "Deleting ...",app.Selection.Text
    app.Selection.Delete()
    # And delete the return
    app.Selection.TypeBackspace()

我建议先尝试使用宏(如果不熟悉VBA,请记录宏),然后再将代码转换为Python.

I would recommend trying to do what you want first in word using macros (Try recording the macros if you are not familiar with VBA) and then translate the code to Python.

这篇关于Word:使用Python删除表和标题之间的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆