Word
Close
close()
Method description
Closes the Word document.
Code example- rpa.app.wps.word.Word.close-
# Note: Before using this method, ensure that the WPS software is installed.
# The close() method does not save changes automatically. Use the save() method to save the document before closing it.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.wps.word.open(word_file_path, visible=True)
word.save()
word.close()Save
save(file=None)
Method description
Saves the current document or saves it as a new file.
Parameters
file<str> The path for saving the document as a new file. If omitted, saves changes to the current file.
Code example- rpa.app.wps.word.Word.save-
# Note: Before using this method, ensure that the WPS software is installed.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.wps.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)
Method description
Writes content to the document at the current cursor position.
Parameters
text<str> The content to write.
size<int> The font size.
family<str> The font family.
color<str> The number that represents the font color.
Code example- rpa.app.wps.word.Word.write-
# Note: Before using this method, ensure that the WPS software is installed.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.wps.word.open(word_file_path, visible=True)
text = "RPA test for Word document write function."
word.write(text)Read
read()
Method description
Reads the full content of the Word document.
Return value
The document's content as a string.<str>
Code example- rpa.app.wps.word.Word.read-
# Note: Before using this method, ensure that the WPS software is installed.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.wps.word.open(word_file_path, visible=True)
text = word.read()Search
search(key, from_pos='start', index=1, relative='left')
Method description
Finds a keyword and moves the cursor to its position.
Parameters
key<str> The keyword to search for.
from_pos<str> The starting position for the search.
options:
start: Searches from the beginning of the document.current: Searches from the current cursor position.
index<int> The 1-based index of the match to find. For example, 1 finds the first occurrence. Defaults to 1.
relative<str> The cursor's position relative to the found keyword.
options:
left: Places the cursor to the left of the keyword.right: Places the cursor to the right of the keyword.
Code example- rpa.app.wps.word.Word.search-
# Note: Before using this method, ensure that the WPS software is installed.
# The following example finds a keyword in the document and, by default, moves the cursor to the left of the keyword.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.wps.word.open(word_file_path, visible=True)
word.search("write")Replace
replace(key, replacement, match_case=False, match_whole_word=False)
Method description
Finds and replaces text in the document.
Parameters
key<str> The keyword to replace.
replacement<str> The new text to insert.
match_case<bool> Specifies whether the search is case-sensitive. The default value is False.
match_whole_word<bool> Specifies whether to match whole words only. The default value is False.
Code example- rpa.app.wps.word.Word.replace-
# Note: Before using this method, ensure that the WPS software is installed.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.wps.word.open(word_file_path, visible=True)
new_text = "Test update"
word.replace("write", new_text)Cursor move
cursor_move(step_count, direction='right')
Method description
Moves the cursor.
Parameters
step_count<int> The number of units to move. A unit is a character for left/right movement and a line for up/down movement.
direction<str> The direction to move the cursor.
options:
leftrightupdown
Code example- rpa.app.wps.word.Word.cursor_move-
# Note: Before using this method, ensure that the WPS software is installed.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.wps.word.open(word_file_path, visible=True)
word.cursor_move(1, direction="top")File name
file_name()
Method description
Gets the file name of the Word document.
Return value
The file name of the document.<str>
Code example- rpa.app.wps.word.Word.file_name-
# Note: Before using this method, ensure that the WPS software is installed.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.wps.word.open(word_file_path, visible=True)
word_name = word.file_name()Get table
get_table(index)
Method description
Gets a table by its index.
Parameters
index<int> The 0-based index of the table.
This 0-based indexing is different from rpa.app.microsoft.word.Word.get_table.
Return value
A Table object.<Table>
Code example- rpa.app.wps.word.Word.get_table-
# Note: Before using this method, ensure that the WPS software is installed.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.wps.word.open(word_file_path, visible=True)
# Note that in WPS, the index of the first table is 0.
table = word.get_table(0)Get table content by keys
get_table_content_by_keys(keys, combine=False)
Method description
Gets the content of the first table found between a pair of start and end keywords.
Parameters
keys<list> A list of keyword pairs. The format is [(start1, end1), (start2, end2)].
combine<bool> If true, processes merged cells. The default value is False.
Return value
A two-dimensional array representing the table's content.<list>
Code example- rpa.app.wps.word.Word.get_table_content_by_keys-
# Note: Before using this method, ensure that the WPS software is installed.
# This method gets the content of the first table located between the paragraphs that contain the two keywords. It does not match keywords within the table itself.
# In this example, the Word document contains the text "Table 1" on one line, followed by a table, and then the text "Table 2" on a new line.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.wps.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)
Method description
Inserts a table at the current cursor position.
Parameters
row_num<int> The number of rows in the new table.
col_num<int> The number of columns in the new table.
Return value
The newly created Table object.<Table>
Code example- rpa.app.wps.word.Word.add_table-
# Note: Before using this method, ensure that the WPS software is installed.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.wps.word.open(word_file_path, visible=True)
table = word.add_table(2, 3)Add picture
add_picture(file)
Method description
Inserts a picture at the current cursor position.
Parameters
file<str> The path to the picture file.
Code example- rpa.app.wps.word.Word.add_picture-
# Note: Before using this method, ensure that the WPS software is installed.
# This method inserts the picture at the current cursor position.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.wps.word.open(word_file_path, visible=True)
pic_path = r"D:\2_TestFileArchive\TestPicture.png"
word.add_picture(pic_path)Convert to PDF
to_pdf(file)
Method description
Converts the document to a PDF file.
Parameters
file<str> The path where the PDF file will be saved.
Code example- rpa.app.wps.word.Word.to_pdf-
# Note: Before using this method, ensure that the WPS software is installed.
word_file_path = r"D:\2_TestFileArchive\TestWord.docx"
word = rpa.app.wps.word.open(word_file_path, visible=True)
pdf_path = r"D:\2_TestFileArchive\word_document.pdf"
word.to_pdf(pdf_path)