xbot.word.Selection
概述
selection接口主要是对Word的处理,如读取文本、写入文本、替换文本、插入图片,换行等操作
详情
read_text
读取当前选择区域的文本内容
read_text(self)
参数
无
返回值
- any:返回文本字符串
示例1
读取word中选择区域的文本
from xbot import word
def main(args):
document = word.open('D:\\test.docx', kind = 'office', visible = True)
selection = document.get_selection()
text = selection.read_text()
write_text
在Word中写入文本,写入光标所在位置
write_text(self, text, text_format, newline = False)
参数
- text:写入文本内容
- text_format:写入文本格式
- newline:写入文本前是否换行
返回值
无
示例1
在word中写入文本
from xbot import word
def main(args):
document = word.open('D:\\test.docx', kind = 'office', visible = True)
selection = document.get_selection()
selection.write_text('我在大润发杀了十年的鱼,我的心早已经像我杀鱼的刀一样冷了','{"font": {"name": "宋体","size": 11,"color": 0,"subscript": false,"superscript": false,"bold": false,"italic": false}}')
insert_newline
在word中插入换行符
insert_newline(self)
参数
无
返回值
无
示例1
在word中插入换行符
from xbot import word
def main(args):
document = word.open('D:\\test.docx', kind = 'office', visible = True)
selection = document.get_selection()
selection.insert_newline()
insert_picture
在word中插入图片
insert_picture(self, image_source, image_path, image_scale, newline = False)
参数
- image_source:图片来源,'file'表示插入图片文件,'clipboard'表示剪切板,直接粘贴剪切板中的图像数据
- image_path:插入图片的路径
- image_scale:图片缩放百分比
- newline:写入文本前是否换行
返回值
无
示例1
在word中插入图片
from xbot import word
def main(args):
document = word.open('D:\\test.docx', kind = 'office', visible = True)
selection = document.get_selection()
selection.insert_picture('file','D:\\test.jpg', 100)
replace_text
在word中替换文本
replace_text(self, search_text, replace_text, replace_all = True, case_sensitive = False)
参数
- search_text:需要查找的文本
- replace_text:用于替换的文本
- case_sensitive:区分大小写
返回值
- int:返回替换文本的次数
示例1
在word中替换文本
from xbot import word
def main(args):
document = word.open('D:\\test.docx', kind = 'office', visible = True)
selection = document.get_selection()
selection.replace_text('1', '2')
move_cursor
移动word中的光标
move_cursor(self, move_direction, move_count, press_shift = False)
参数
- move_direction:移动方向,'move_left'表示向左,'move_up'表示向上,'move_right'表示向下,'move_down'表示向下
- move_count:移动单位长度
- press_shift:移动时是否按下shift键,若为True,移动的位置文本均选中
返回值
无
示例1
在word文本中向右移动5个单位长度
from xbot import word
def main(args):
document = word.open('D:\\test.docx', kind = 'office', visible = True)
selection = document.get_selection()
selection.move_cursor('move_right', 5)
locate_cursor_to_document
定位光标到word文档开头处和结尾处
locate_cursor_to_document(self, document_cursor_position = 'end_document')
参数
- document_cursor_position:'begin_document'表示文本开头处,'end_document'表示文本结尾处
返回值
无
示例1
在word中定位光标到文档结尾处
from xbot import word
def main(args):
document = word.open('D:\\test.docx', kind = 'office', visible = True)
selection = document.get_selection()
selection.locate_cursor_to_document('begin_document')
locate_cursor_to_text
定位光标到word中指定文本处
locate_cursor_to_text(self, text_content, text_index = 1, text_cursor_position = 'after_text', from_start = True)
参数
- text_content:定位的文本内容
- text_index:从起点查找到文本的位置
- text_cursor_position:定位到文本位置,'before_text'表示定位到文本前,'after_text'表示定位到文本后
- from_start:是否从头开始查找
返回值
无
示例1
在word中定位光标到文本处
from xbot import word
def main(args):
document = word.open('D:\\test.docx', kind = 'office', visible = True)
selection = document.get_selection()
selection.locate_cursor_to_text('我', 2)