Excel

更新时间:
复制 MD 格式

Save

save(file=None)

Description

Saves the current Excel file. To save the file with a new name, specify a path. If no path is specified, the method saves the currently open file.

Metric descriptions

file <str> The path to save the file.

Example - rpa.app.wps.excel.Excel.save -

# Note: Make sure that the WPS software is installed before you use this method.
# Saving the file does not close the Excel process.
# If you are saving a new Excel file, you must specify a path.
# The following code provides an example:
excel_file_path = r"D:\2_TestFileArchive\TestExcel.xlsx"
excel = rpa.app.wps.excel.open(excel_file_path,visible=True)

path = r"D:\2_TestFileArchive\TestExcel-Replica.xlsx"
excel.save()
excel.save(file=path)

Close

close(save=True)

Description

Close Excel.

Parameters

save<bool> Saving is enabled by default.

Example - rpa.app.wps.excel.Excel.close -

# Note: Make sure that the WPS software is installed before you use this method.
# The following code provides an example:
excel_file_path = r"D:\2_TestFileArchive\TestExcel.xlsx"
excel = rpa.app.wps.excel.open(excel_file_path,visible=True)

excel.close()

Get sheets

get_sheets()

Description

Retrieves a list of all sheets in the Excel file.

Return value

Returns a list of sheets <list>.

Example - rpa.app.wps.excel.Excel.get_sheets -

# Note: Make sure that the WPS software is installed before you use this method.
# The following code provides an example:
excel_file_path = r"D:\2_TestFileArchive\TestExcel.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')

Description

Adds a new sheet.

Parameters

sheet_name <str> The name of the new sheet.

location <str> The name of the sheet to use as a reference for inserting the new sheet.

relative <str> The position of the new sheet relative to the reference sheet.

Optional:

  • Before:

  • After:

Return value

Returns a <Sheet> object.

Example - rpa.app.wps.excel.Excel.add_sheet -

# Note: Make sure that the WPS software is installed before you use this method.
# The following code provides an example:
excel_file_path = r"D:\2_TestFileArchive\TestExcel.xlsx"
excel = rpa.app.wps.excel.open(excel_file_path,visible=True)

new_sheet_name = "RPA-NewSheet"
excel.add_sheet(new_sheet_name ,"Sheet1")

Get sheet

get_sheet(sheet_name=None)

Description

Retrieves a specified sheet.

Return value

Returns the specified sheet, or the default sheet <Sheet> if no sheet is specified.

Example - rpa.app.wps.excel.Excel.get_sheet -

# Note: Make sure that the WPS software is installed before you use this method.
# The following code provides an example:
excel_file_path = r"D:\2_TestFileArchive\TestExcel.xlsx"
excel = rpa.app.wps.excel.open(excel_file_path,visible=True)

sheet1 = excel.get_sheet("Sheet1")

Remove sheet

remove_sheet(sheet_name)

Description

Deletes a sheet.

Metric descriptions

sheet_name <str> The name of the sheet.

Example - rpa.app.wps.excel.Excel.remove_sheet -

# Note: Make sure that the WPS software is installed before you use this method.
# The following code provides an example:
excel_file_path = r"D:\2_TestFileArchive\TestExcel.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)

Description

Renames a sheet.

Parameters

old_sheet_name <str> The original name of the sheet.

new_sheet_name <str> The new name for the sheet.

Example - rpa.app.wps.excel.Excel.rename_sheet -

# Note: Make sure that the WPS software is installed before you use this method.
# The following code provides an example:
excel_file_path = r"D:\2_TestFileArchive\TestExcel.xlsx"
excel = rpa.app.wps.excel.open(excel_file_path,visible=True)

new_name = "RenamedSheet"
excel.rename_sheet("Sheet1",new_name)

Run macro

run_macro(macro_name, file=None)

Description

Runs a macro.

Parameters

file <str> The path to the macro file.

macro_name <str> The name of the macro command.

Example - rpa.app.wps.excel.Excel.run_macro -

# Note: Make sure that the WPS software is installed before you use this method.
# The following code provides an example:
excel_file_path = r"D:\2_TestFileArchive\TestExcel.xlsm"
excel = rpa.app.wps.excel.open(excel_file_path,visible=True)

excel.run_macro("Macro1")

Get sheetnames

get_sheetnames()

Description

Retrieves the names of all sheets.

Return value

A list of sheet names. <list>

Example - rpa.app.wps.excel.Excel.get_sheetnames -

# Note: Make sure that the WPS software is installed before you use this method.
# The following code provides an example:
excel_file_path = r"D:\2_TestFileArchive\TestExcel.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)

Description

Copies a sheet to the current Excel file.

Parameters

source_sheet_name <str> The name of the sheet to copy.

new_sheet_name <str> The name for the new sheet.

replace_sheet_when_exist <bool> Specifies whether to overwrite the destination sheet if a sheet with the same name exists.

Example - rpa.app.wps.excel.Sheet.Excel.duplicate_sheet_to_current_excel -

# Note: Make sure that the WPS software is installed before you use this method.
# The following code provides an example:
excel_file_path = r"D:\2_TestFileArchive\TestExcel.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)

Description

Copies a sheet to another Excel file.

Parameters

source_sheet_name <str> The name of the sheet to copy.

new_sheet_name <str> The name for the new sheet.

target_excel_obj <str> The target Excel object.

replace_sheet_when_exist <bool> Specifies whether to overwrite the destination sheet if a sheet with the same name exists.

Example - rpa.app.wps.excel.Sheet.Excel.duplicate_sheet_to_other_excel -

# Note: Make sure that the WPS software is installed before you use this method.
# The following code provides an example:
excel_file_path1 = r"D:\2_TestFileArchive\Test1Excel.xlsx"
excel_file_path2 = r"D:\2_TestFileArchive\Test2Excel.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)