文档

Excel

更新时间:

save

save(file=None)

方法描述

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

参数说明

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

调用样例- rpa.app.wps.excel.Excel.save-

# 注意事项:使用前需确认已安装WPS相关软件
# 进行保存操作并不会关闭Excel进程
# 如果excel文件为新建的,则进行save操作时必须指定路径
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.wps.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.wps.excel.Excel.close-

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

excel.close()

get_sheets

get_sheets()

方法描述

获得Excel所有的sheet列表

返回值说明

返回sheet列表<list>

调用样例- rpa.app.wps.excel.Excel.get_sheets-

# 注意事项:使用前需确认已安装WPS相关软件
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.wps.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.wps.excel.Excel.add_sheet-

# 注意事项:使用前需确认已安装WPS相关软件
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.wps.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,不传入时返回默认sheet<Sheet>

调用样例- rpa.app.wps.excel.Excel.get_sheet-

# 注意事项:使用前需确认已安装WPS相关软件
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.wps.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.wps.excel.Excel.remove_sheet-

# 注意事项:使用前需确认已安装WPS相关软件
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.wps.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.wps.excel.Excel.rename_sheet-

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

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

run_macro

run_macro(macro_name, file=None)

方法描述

运行宏

参数说明

file<str>宏文件路径

macro_name<str>宏命令名称

调用样例- rpa.app.wps.excel.Excel.run_macro-

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

excel.run_macro("宏1")

get_sheetnames

get_sheetnames()

方法描述

获取所有Sheet名

返回值说明

返回sheet名列表<list>

调用样例- rpa.app.wps.excel.Excel.get_sheetnames-

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

sheet_list = excel.get_sheetnames()
print(sheet_list)

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.wps.excel.Sheet.Excel.duplicate_sheet_to_current_excel-

# 注意事项:使用前需确认已安装WPS相关软件
# 代码调用样例如下:
excel_file_path = r"D:\2_测试文件归档\测试Excel.xlsx"
excel = rpa.app.wps.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.wps.excel.Sheet.Excel.duplicate_sheet_to_other_excel-

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

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

  • 本页导读 (0)
文档反馈