Close
close()
method description
Closes the Word document.
code example- rpa.app.microsoft.word.Word.close-
# Note: Ensure Microsoft Word is installed before use.
# Use the save() method before closing.
word_file_path = r"D:\TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)
word.save()
word.close()Save
save(file=None)
method description
Saves the document. If a file path is provided, it saves the document to a new location (save as).
parameter description
file<str> The path to save the new file.
code example- rpa.app.microsoft.word.Word.save-
# Note: Ensure Microsoft Word is installed before use.
word_file_path = r"D:\TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)
new_path = r"D:\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.
parameter description
text<str> The content to write.
size<int> The font size.
family<str> The font family.
color<int> The font color number.
Value | Description |
-1 | wdByAuthor |
0 | wdAuto |
1 | wdBlack |
2 | wdBlue |
3 | wdTurquoise |
4 | wdBrightGreen |
5 | wdPink |
6 | wdRed |
7 | wdYellow |
8 | wdWhite |
9 | wdDarkBlue |
10 | wdTeal |
11 | wdGreen |
12 | wdViolet |
13 | wdDarkRed |
14 | wdDarkYellow |
15 | wdGray50 |
16 | wdGray25 |
code example- rpa.app.microsoft.word.Word.write-
# Note: Ensure Microsoft Word is installed before use.
# By default, this method inserts text at the end of the document.
word_file_path = r"D:\TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)
word.write("Hello World\r\n",size=10,family='SimSun',color=1)
word.write("Hello World\r\n",size=20,family='SimHei',color=16)Read
read()
method description
Reads the content of the Word document. Any tables are converted to plain text.
return value description
Returns the document content as a string.<str>
code example- rpa.app.microsoft.word.Word.read-
# Note: Ensure Microsoft Word is installed before use.
word_file_path = r"D:\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')
method description
Finds a keyword and moves the cursor to its position.
parameter description
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> Specifies which occurrence of the keyword to find (e.g., 1 for the first). Defaults to 1.
relative<str> The cursor 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.microsoft.word.Word.search-
# Note: Ensure Microsoft Word is installed before use.
# Finds the specified keyword and, by default, moves the cursor to its left.
word_file_path = r"D:\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)
method description
Replaces all occurrences of a specified keyword in the document.
parameter description
key<str> The keyword to replace.
replacement<str> The replacement string.
match_case<bool> Specifies whether the search is case-sensitive. Defaults to False.
match_whole_word<bool> Specifies whether to match whole words only. Defaults to False.
code example- rpa.app.microsoft.word.Word.replace-
# Note: Ensure Microsoft Word is installed before use.
word_file_path = r"D:\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()
method description
Gets the file name of the Word document.
return value description
The file name of the document.<str>
code example- rpa.app.microsoft.word.Word.file_name-
# Note: Ensure Microsoft Word is installed before use.
word_file_path = r"D:\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')
method description
Moves the cursor.
parameter description
step_count<int> The number of characters (for left/right) or lines (for up/down) to move.
direction<str> The direction to move the cursor.
Options:
left: Leftright: Rightup: Updown: Down
code example- rpa.app.microsoft.word.Word.cursor_move-
# Note: Ensure Microsoft Word is installed before use.
word_file_path = r"D:\TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)
word.cursor_move(1, direction="up")Get table
get_table(index)
method description
Gets a table from the document by its index.
parameter description
index<int> The index of the table.
The index is 1-based, which is different from rpa.app.wps.word.Word.get_table.
return value description
Returns a Table object.<Table>
code example- rpa.app.microsoft.word.Word.get_table-
# Note: Ensure Microsoft Word is installed before use.
word_file_path = r"D:\TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)
table = word.get_table(1)Add picture
add_picture(file=None, source='local', percentage=100, add_in_new_line=False)
method description
Inserts a picture at the current cursor position.
parameter description
file<str> The path to the image file.
source<str> The image source.
Options:
local: A local image file.clipboard: The image from the clipboard.
percentage<int> The size of the image as a percentage of its original size.
add_in_new_line<bool> Specifies whether to insert the image on a new line.
code example- rpa.app.microsoft.word.Word.add_picture-
# Note: Ensure Microsoft Word is installed before use.
word_file_path = r"D:\TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)
pic_path = r"D:\TestFileArchive\TestImage.png"
word.add_picture(file=pic_path, source='local', percentage=100, add_in_new_line=False)To pdf
to_pdf(file)
method description
Converts the document to a PDF file.
parameter description
file<str> The path to save the output PDF file.
code example- rpa.app.microsoft.word.Word.to_pdf-
# Note: Ensure Microsoft Word is installed before use.
word_file_path = r"D:\TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)
pdf_path = r"D:\TestFileArchive\WordDocument.pdf"
word.to_pdf(pdf_path)Get table content by keys
get_table_content_by_keys(keys, combine=False)
method description
Extracts the content of the first table found between the paragraphs that contain the specified start and end keywords. This method does not search for keywords within the table itself.
parameter description
keys<list> A list of keyword pairs that define the boundaries for the table search. The format is [('start1', 'end1'), ('start2', 'end2')].
combine<bool> Specifies whether to merge cells in the output. Defaults to False.
return value description
Returns a two-dimensional array containing the table data.<list>
code example- rpa.app.microsoft.word.Word.get_table_content_by_keys-
# Note: Ensure Microsoft Word is installed before use.
# In this example, the Word document contains the text "Table 1", followed by a table, and then the text "Table 2" on a new line.
word_file_path = r"D:\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)
method description
Inserts a table at the current cursor position.
parameter description
row_num<int> The number of rows for the new table.
col_num<int> The number of columns for the new table.
return value description
Returns a Table object for the newly created table.<Table>
code example- rpa.app.microsoft.word.Word.add_table-
# Note: Ensure Microsoft Word is installed before use.
word_file_path = r"D:\TestFileArchive\TestWord.docx"
word = rpa.app.microsoft.word.open(word_file_path, visible=True)
table = word.add_table(2, 3)