How to use Application.AutoCorrect in the xlwings API way

The AutoCorrect feature in Excel is a powerful tool for automatically correcting common typing errors, capitalizing days of the year, and expanding text abbreviations. Through xlwings, you can programmatically access and control these settings via the Application object’s AutoCorrect property. This allows for automation of tasks such as disabling auto-capitalization of days, adding custom replacement entries, or checking if a specific text string is in the AutoCorrect list.

Functionality
The AutoCorrect object provides control over Excel’s AutoCorrect options. Key capabilities include:

  • Managing automatic capitalization of days of the week.
  • Controlling the first letter capitalization in sentences.
  • Adding, removing, or checking text replacements (e.g., replacing “(c)” with “©”).
  • Enabling or disabling various auto-formatting features.

Syntax and Parameters
In xlwings, you access it through the app object (an instance of xw.App). The basic syntax is:

app.api.AutoCorrect.MemberName

Where MemberName is a property or method of the AutoCorrect object. Common members include:

MemberTypeDescriptionParameters/Values
CapitalizeNamesOfDaysPropertyGets or sets whether days are auto-capitalized.Boolean (True/False)
TwoInitialCapitalsPropertyControls auto-correction of two initial capitals.Boolean
AddReplacement(What, Replacement)MethodAdds a custom text replacement.What (String): Text to replace. Replacement (String): New text.
DeleteReplacement(What)MethodRemoves a custom replacement.What (String): Text entry to delete.
ReplacementList(Index)PropertyReturns a specific replacement pair.Index (Integer): Position in the list (1-based).

Code Examples
Here are practical examples using xlwings:

  1. Disable auto-capitalization of days:
import xlwings as xw
app = xw.App(visible=False)
app.api.AutoCorrect.CapitalizeNamesOfDays = False
app.quit()
  1. Add a custom text replacement:
import xlwings as xw
app = xw.App(visible=False)
# Replace "xlw" with "xlwings"
app.api.AutoCorrect.AddReplacement("xlw", "xlwings")
app.quit()
  1. Check and delete a replacement:
import xlwings as xw
app = xw.App(visible=False)
# Delete if "xlw" exists in the list
try:
    app.api.AutoCorrect.DeleteReplacement("xlw")
    print("Replacement deleted.")
except Exception as e:
    print("Entry not found:", e)
app.quit()
  1. Iterate through replacement list (first 5 entries):
import xlwings as xw
app = xw.App(visible=False)
ac = app.api.AutoCorrect
for i in range(1, 6):
try:
    item = ac.ReplacementList(i)
    print(f"Index {i}: {item}")
except:
    break
app.quit()

May 1, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *