Word

更新时间:
复制 MD 格式

close

close()

Description

Close Word

Example: rpa.app.microsoft.word.Word.close

# Note: Confirm that Microsoft software is installed before use.
# The close method does not save automatically. Save the document using the save method before you close it.
# The following code provides an example:
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)

word.save()
word.close()

save

save(file=None)

Description

Save or Save As

Metric description

file<str> The path to save the new file.

Example: rpa.app.microsoft.word.Word.save

# Note: Confirm that Microsoft software is installed before use.
# The following code provides an example:
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)
new_path = r"D:\2_TestFileArchive\TestWord-Copy.docx"

word.save()
word.save(file=new_path)

word.close()

write

write(text, size=8, family='Microsoft YaHei', color=1)

Description

Enter the content

Parameters

text<str> The content to write.

size<int> The font size.

family<str> The font family.

color<int> The font color code.

Example: rpa.app.microsoft.word.Word.write

# Note: Confirm that Microsoft software is installed before use.
# This method writes a line at the end of the document by default.
# The following code provides an example:
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)

text = "RPA test for Word document write feature"
word.write(text)

read

read()

Description

Reads the entire content of the Word document.

Return value

Returns the content that is read <str>

Example: rpa.app.microsoft.word.Word.read

# Note: Confirm that Microsoft software is installed before use.
# If the Word document contains tables, this method reads them as text.
# The following code provides an example:
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)

text = word.read()

search

search(key, from_pos='start', index=1, relative='left')

Description

Searches for a specified keyword and moves the cursor relative to the found keyword.

Parameters

key<str> The keyword to search for.

from_pos<str> The starting position for the search.

Options:

  • Start: The beginning.

  • current: Searches from the current cursor position.

index<int> The instance of the match to find. The default value is 1.

relative<str> The desired cursor position relative to the found keyword.

Optional:

  • left: To the left of the keyword.

  • right: To the right of the keyword.

Example: rpa.app.microsoft.word.Word.search

# Note: Confirm that Microsoft software is installed before use.
# The following code provides an example. It finds the specified keyword in the Word document and moves the cursor to the left of the keyword by default.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)

word.search("write")

replace

replace(key, replacement, match_case=False, match_whole_word=False)

Description

Replace

Metric description

key<str> The text to be replaced.

replacement<str> The replacement text.

match_case <bool> Specifies whether matching is case-sensitive.

match_whole_word<bool> Specifies whether to match whole words only. The default value is `False`.

Example: rpa.app.microsoft.word.Word.replace

# Note: Confirm that Microsoft software is installed before use.
# The following code provides an example:
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)

new_text = "Test update"
word.replace("write",new_text)

file_name

file_name()

Description

Retrieves the name of the Word file.

Return value

The name of the Word file.<str>

Example: rpa.app.microsoft.word.Word.file_name

# Note: Confirm that Microsoft software is installed before use.
# The following code provides an example:
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)

word_name = word.file_name()

cursor_move

cursor_move(step_count, direction='right')

Description

Moves the cursor from its current position.

Parameters

step_count<int> The number of steps to move.

direction<str> The direction in which to move the cursor.

Options:

  • left: To the left.

  • right: To the right.

  • top: Upward.

  • down: Downward.

Example: rpa.app.microsoft.word.Word.cursor_move

# Note: Confirm that Microsoft software is installed before use.
# The following code provides an example:
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)

word.cursor_move(1,direction="top")

get_table

get_table(index)

Description

Retrieves a table object based on its index.

Parameters

index<int> The index of the table in the document.

Return value

Returns a Table object.

Example: rpa.app.microsoft.word.Word.get_table

# Note: Confirm that Microsoft software is installed before use.
# The following code provides an example:
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)
# Note: In Microsoft Word, the index of the first table starts from 1.
table = word.get_table(1)

add_picture

add_picture(file=None, source='local', percentage=100, add_in_new_line=False)

Description

Inserts a picture into the document.

Metric description

file<str> The file path of the picture.

source<str> The source of the picture.

Options:

  • local: A picture from a local file.

  • Clipboard: clipboard

percentage<int> The scaling percentage for the picture size.

add_in_new_line <bool> Specifies whether to add a line break to the input.

Example: rpa.app.microsoft.word.Word.add_picture

# Note: Confirm that Microsoft software is installed before use.
# This method inserts the picture at the current cursor position.
# The following code provides an example:
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)

pic_path = r"D:\2_TestFileArchive\TestPicture.png"
word.add_picture(file=pic_path, source='local', percentage=100, add_in_new_line=False)

to_pdf

to_pdf(file)

Description

Convert to PDF

Parameters

file<str> The path where the PDF file is saved.

Example: rpa.app.microsoft.word.Word.to_pdf

# Note: Confirm that Microsoft software is installed before use.
# The following code provides an example:
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)

pdf_path = r"D:\2_TestFileArchive\WordDocument.pdf"
word.to_pdf(pdf_path)

get_table_content_by_keys

get_table_content_by_keys(keys, combine=False)

Description

Retrieves the content of a table that is located between two specified keywords.

Parameters

keys<list> A list of keyword pairs that define the start and end boundaries of the table. The format is `[(start1,end1),(start2,end2)]`.

combine<bool> Specifies whether to merge cells. The default value is `False`.

Return value

Returns a two-dimensional array<list>.

Example: rpa.app.microsoft.word.Word.get_table_content_by_keys

# Note: Confirm that Microsoft software is installed before use.
# This method gets the content of the first table between the paragraphs that contain the two keywords. It does not match keywords within the table.
# The following code provides an example. In this example, a table is inserted after a line with the text title "Table 1" in the Word document. Then, a new line is started with the text title "Table 2".
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)

keys = [("Table 1","Table 2")]
tables = word.get_table_content_by_keys(keys)

add_table

add_table(row_num, col_num)

Description

Inserts a table at the current cursor position.

Parameters

row_num<int> The number of rows in the table.

col_num<int> The number of columns in the table.

Return value

Returns a <Table> object.

Example: rpa.app.microsoft.word.Word.add_table

# Note: Confirm that WPS Office software is installed before use.
# The following code provides an example:
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)

table = word.add_table(2,3)