文档

Excel

更新时间:

save

save(file=None)

方法描述

保存或者另存当前Excel文件,当不输入另存路径的时候就是保存当前已打开的Excel

参数说明

file<str>另存为的文件路径

调用样例- rpa.app.microsoft.excel.Sheet.Excel.save-

# 注意事项:使用前需确认已安装MicroSoft相关软件
# 进行保存操作并不会关闭Excel进程
# 如果excel文件为新建的,则进行save操作时必须指定路径。
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.microsoft.excel.open(excel_file_path,visible=True)

path = r"D:\2_测试文件归档\测试Excel-副本.xlsx"
excel.save()
excel.save(file=path)

close

close(save=True)

方法描述

关闭Excel

参数说明

save<bool>默认保存

调用样例- rpa.app.microsoft.excel.Sheet.Excel.close-

# 注意事项:使用前需确认已安装MicroSoft相关软件
# 执行关闭动作默认自动保存excel,如果是新建excel,建议先通过save方法指定保存路径保存文件。
# 建议只要涉及excel操作的,在代码末行均加上close方法,否则可能出现excel重复打开产生只读状态而无法正常处理excel的现象。
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.microsoft.excel.open(excel_file_path,visible=True)

excel.close()

get_sheets

get_sheets()

方法描述

获得Excel所有的sheet列表

返回值说明

返回sheet列表<list>

调用样例- rpa.app.microsoft.excel.Sheet.Excel.get_sheets-

# 注意事项:使用前需确认已安装MicroSoft相关软件
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.microsoft.excel.open(excel_file_path,visible=True)

sheets = excel.get_sheets()

add_sheet

add_sheet(sheet_name, location, relative='before')

方法描述

新增sheet

参数说明

sheet_name<str>sheet名称

location<str>定位插入位置的sheet的名称

relative<str>对于目标sheet的方向

可选项:

  • before : 前

  • after : 后

返回值说明

返回sheet对象<Sheet>

调用样例- rpa.app.microsoft.excel.Sheet.Excel.add_sheet-

# 注意事项:使用前需确认已安装MicroSoft相关软件
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.microsoft.excel.open(excel_file_path,visible=True)

new_sheet_name = "RPA-新建表"
excel.add_sheet(new_sheet_name ,"Sheet1")

get_sheet

get_sheet(sheet_name=None)

方法描述

获取指定sheet

参数说明

sheet_name<str>sheet名称

返回值说明

返回指定Sheet,不传入时返回默认sheet<Sheet>

调用样例- rpa.app.microsoft.excel.Sheet.Excel.get_sheet-

# 注意事项:使用前需确认已安装MicroSoft相关软件
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.microsoft.excel.open(excel_file_path,visible=True)

sheet1 = excel.get_sheet("Sheet1")

remove_sheet

remove_sheet(sheet_name)

方法描述

删除sheet

参数说明

sheet_name<str>sheet名称

调用样例- rpa.app.microsoft.excel.Sheet.Excel.remove_sheet-

# 注意事项:使用前需确认已安装MicroSoft相关软件
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.microsoft.excel.open(excel_file_path,visible=True)

excel.remove_sheet("Sheet2")

rename_sheet

rename_sheet(old_sheet_name, new_sheet_name)

方法描述

重命名sheet

参数说明

old_sheet_name<str>原sheet名称

new_sheet_name<str>重命名后的sheet名称

调用样例- rpa.app.microsoft.excel.Sheet.Excel.rename_sheet-

# 注意事项:使用前需确认已安装MicroSoft相关软件
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.microsoft.excel.open(excel_file_path,visible=True)

new_name = "表格改名"
excel.rename_sheet("Sheet1",new_name)

duplicate_sheet_to_current_excel

duplicate_sheet_to_current_excel(source_sheet_name, new_sheet_name, replace_sheet_when_exist=True)

方法描述

拷贝Sheet页到当前Excel

参数说明

source_sheet_name<str>待拷贝的Sheet页名称

new_sheet_name<str>新sheet名称

replace_sheet_when_exist<bool>存在同名Sheet时是否覆盖

调用样例- rpa.app.microsoft.excel.Sheet.Excel.duplicate_sheet_to_current_excel-

# 注意事项:使用前需确认已安装MicroSoft相关软件
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.microsoft.excel.open(excel_file_path,visible=True)

excel.duplicate_sheet_to_current_excel('Sheet1','Sheet2')

duplicate_sheet_to_other_excel

duplicate_sheet_to_other_excel(source_sheet_name, new_sheet_name, target_excel_obj, replace_sheet_when_exist=True)

方法描述

拷贝Sheet页到另外的Excel

参数说明

source_sheet_name<str>待拷贝的Sheet页名称

new_sheet_name<str>新sheet名称

target_excel_obj<str>目标Excel对象

replace_sheet_when_exist<bool>存在同名Sheet时是否覆盖

调用样例- rpa.app.microsoft.excel.Sheet.Excel.duplicate_sheet_to_other_excel-

# 注意事项:使用前需确认已安装MicroSoft相关软件
# 代码调用样例如下:
excel_file_path1 = r"D:\2_测试文件归档\测试1Excel.xlsx"
excel_file_path2 = r"D:\2_测试文件归档\测试2Excel.xlsx"
excel1 = rpa.app.microsoft.excel.open(excel_file_path1,visible=True)
excel2 = rpa.app.microsoft.excel.open(excel_file_path2,visible=True)

excel1.duplicate_sheet_to_other_excel('Sheet1','Sheet1',excel2)

get_sheetnames

get_sheetnames()

方法描述

获取所有Sheet名

返回值说明

返回sheet名列表<list>

调用样例- rpa.app.microsoft.excel.Sheet.Excel.get_sheetnames-

# 注意事项:使用前需确认已安装MicroSoft相关软件
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.microsoft.excel.open(excel_file_path,visible=True)

sheet_list = excel.get_sheetnames()
print(sheet_list)
  • 本页导读 (0)
文档反馈