Excel

更新时间:
复制 MD 格式

Save

save(file=None)

Method description

Saves the current Excel file. If you provide a file path, the method saves the file to the specified path. Otherwise, the method saves the currently open file.

Parameter description

file <str> The path to which the file is saved.

Example call rpa.app.microsoft.excel.Sheet.Excel.save

# Note: Make sure that you have installed the relevant Microsoft software before use.
# The save operation 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.microsoft.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)

Method description

Close Excel.

Parameter description

save <bool> Specifies whether to save. The default value is true.

Example call rpa.app.microsoft.excel.Sheet.Excel.close

# Note: Make sure that you have installed the relevant Microsoft software before use.
# The close operation saves the Excel file by default. If you are closing a new Excel file, first use the `save` method to specify a path.
# Add the `close` method at the end of your code for all Excel operations. This prevents the file from being opened multiple times, which can cause it to become read-only and uneditable.
# The following code provides an example:
excel_file_path = r"D:\2_TestFileArchive\TestExcel.xlsx"
excel = rpa.app.microsoft.excel.open(excel_file_path,visible=True)

excel.close()

Get sheets

get_sheets()

Method description

Retrieves a list of all sheets in the Excel file.

Return value description

Returns a list of sheet objects. <list>

Example call rpa.app.microsoft.excel.Sheet.Excel.get_sheets

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

Method description

Adds a new sheet.

Parameter description

sheet_name <str> The name of the new sheet.

location <str> The name of the reference sheet for insertion.

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

Options:

  • before: Inserts the new sheet before the reference sheet.

  • After

Return value description

Returns a sheet object. <Sheet>

Example call rpa.app.microsoft.excel.Sheet.Excel.add_sheet

# Note: Make sure that you have installed the relevant Microsoft software before use.
# The following code provides an example:
excel_file_path = r"D:\2_TestFileArchive\TestExcel.xlsx"
excel = rpa.app.microsoft.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)

Method description

Retrieves a specified sheet.

Parameter description

sheet_name <str> The name of the sheet.

Return value description

Returns the specified sheet object. If you do not specify a sheet name, the method returns the default sheet. <Sheet>

Example call rpa.app.microsoft.excel.Sheet.Excel.get_sheet

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

sheet1 = excel.get_sheet("Sheet1")

Remove sheet

remove_sheet(sheet_name)

Method description

Deletes a sheet.

Parameter description

sheet_name <str> The name of the sheet.

Example call rpa.app.microsoft.excel.Sheet.Excel.remove_sheet

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

Method description

Renames a sheet.

Parameter description

old_sheet_name <str> The original name of the sheet.

new_sheet_name <str> The new name for the sheet.

Example call rpa.app.microsoft.excel.Sheet.Excel.rename_sheet

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

new_name = "SheetRenamed"
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)

Method description

Copies a sheet to the current Excel file.

Parameter description

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 sheet if a sheet with the same name exists.

Example call rpa.app.microsoft.excel.Sheet.Excel.duplicate_sheet_to_current_excel

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

Method description

Copies a sheet to another Excel file.

Parameter description

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

new_sheet_name <str> The name for the new sheet.

target_excel_obj <Excel> The target Excel object.

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

Example call rpa.app.microsoft.excel.Sheet.Excel.duplicate_sheet_to_other_excel

# Note: Make sure that you have installed the relevant Microsoft software before use.
# 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.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()

Method description

Retrieves the names of all sheets.

Return value description

Returns a list of sheet names. <list>

Example call rpa.app.microsoft.excel.Sheet.Excel.get_sheetnames

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

sheet_list = excel.get_sheetnames()
print(sheet_list)