xbot.word.Document

概述

document接口主要是对工作簿的处理,如插入表格、读取表格、导出PDF等

详情

save

将工作簿保存为word文件

save(self)

参数

返回值

示例1

打开word文件'D:\test.docx'后保存

from xbot import word

def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    document.save()

save_as

将工作簿另存为word文件

save_as(self, filename)

参数

  • filename:另存为路径

返回值

示例1

打开word文件'D:\test.docx'后另存为'D:\test1.docx'

from xbot import word

def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    document.save_as('D:\\test1.docx')

close

关闭工作簿

close(self)

参数

返回值

示例1

打开word文件'D:\test.docx'后关闭

from xbot import word

def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    document.close()

export_to_pdf

Word导出PDF文件

export_to_pdf(self, pdf_name, export_range = 0, page_from = 0, page_to = 0, overwrite = True)

参数

  • pdf_name:要保存的PDF文件绝对路径
  • export_range:要导出的范围,0表示所有页面,1表示当前选择区域,2表示当前页面,3表示指定页面
  • page_from:导出指定页面的起始页面,当export_range=3时有效
  • page_to:导出指定页面的终止页面,当export_range=3时有效
  • override:若导出的PDF文件已存在,是否选择覆盖

返回值

示例1

将word文件'D:\test.docx'中全部导出到'D:\test.pdf'

from xbot import word

def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    document.export_to_pdf('D:\\test.pdf', 0)

get_selection

获取Word当前选择区域

get_selection(self)

参数

返回值

BaseSelection:Word选择区域对象

示例1

获取word文件'D:\test.docx'中当前选择区域对象

from xbot import word

def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    selection = document.get_selection()

插入超链接

insert_hyperlink(self, text, hyperlink_url, newline = False)

参数

  • text:超链显示文字
  • hyperlink_url:超链接地址
  • newline:插入超链前是否换行,True表示换行,False表示不换行

返回值

示例1

在word文件'D:\test.docx'中插入超链接

from xbot import word

def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    document.insert_hyperlink('百度一下', 'www.baidu.com', True)

insert_table

插入Word表格

insert_table(self, table_data, table_data_format, grid = True, newline = False)

参数

  • table_data:表格数据,采用二维列表数据格式
  • table_data_format:表格格式内容,可以从客户端中获取
  • grid:表格是否显示边框,True表示显示,False表示不显示
  • newline:插入表格前是否换行,True表示换行,False表示不换行

返回值

示例1

在word文件'D:\test.docx'中插入数据表格

from xbot import word

def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    document.insert_table([['1','2','3'],['4','5','6']], '{"font": {"name": "宋体","style": "常规","size": 11,"color": 0,"strikethrough": false,"subscript": false,"superscript": false,"bold": false,"italic": false},"alignment": {"horizontalAlignment": 1,"verticalAlignment": 1}}')

read_table_from_document

读取Word表格

read_table_from_document(self, find_way, table_index, table_content)

参数

  • find_way:表格查找方式,'index'表示根据位置查找,'content'表示根据内容查找
  • table_index:表格位置,find_way='index'时生效
  • table_content:表格内容,find_way='content'时生效

返回值

二维列表,表格内容文本

示例1

读取word文件'D:\test.docx'中第一个表格数据

from xbot import word

def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    table_data = document.read_table_from_document('index', 1)
    print(table_data)

locate_cursor_to_bookmark

定位光标到书签处

locate_cursor_to_bookmark(self, bookmark_name, bookmark_cursor_position = 'after_bookmark')

参数

  • bookmark_name:书签名
  • bookmark_cursor_position:定位到书签的位置,'before_bookmark'表示书签前,'after_bookmark'表示书签后

返回值

示例1

在word文件'D:\test.docx'中定位光标到书签'书签1'后面

from xbot import word

def main(args):
    document = word.open('D:\\test.docx', kind = 'office', visible = True)
    document.locate_cursor_to_bookmark('书签1')
问题没有解决?去社区提问 all right reserved,powered by Gitbook

results matching ""

    No results matching ""