翻譯|使用教程|編輯:李顯亮|2021-06-07 10:55:25.550|閱讀 1142 次
概述:MS Excel 提供了查找和替換選項,只需單擊一下即可更新所需的文本。在本文中,將學習如何在 Python 中以編程方式查找和替換 Excel 文件中的文本。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Excel 電子表格通常在多個工作表中包含數千條記錄。在這種情況下,手動查找和替換特定文本可能是一項繁忙的任務。因此,MS Excel 提供了查找和替換選項,只需單擊一下即可更新所需的文本。在本文中,將學習如何在 Python 中以編程方式查找和替換 Excel 文件中的文本。
為了加密和解密 Excel 文件,我們將使用Aspose.Cells for Python via Java除了對 Excel 文件的保護外,該 API 還支持動態生成、修改和轉換 Excel 文件。你可以點擊下方按鈕獲取使用。
點擊下載Aspose.Cells for Python via Java
以下是在 Excel 文件中查找和替換文本的步驟。
以下代碼示例展示了如何使用 Python 在 Excel 中查找和替換文本。
# Load Excel file workbook = Workbook("Workbook.xlsx") # Create replace options replace = ReplaceOptions() # Set case sensitivity and text matching options replace.setCaseSensitive(False) replace.setMatchEntireCellContents(False) # Replace text workbook.replace("find and replace this text","with this text", replace) # Save as Excel XLSX file workbook.save("updated.xlsx");
還可以在 Excel 文件中查找和替換與特定模式匹配的文本。以下步驟顯示如何使用正則表達式查找和替換 Excel 文件中的文本。
以下 Python 代碼示例展示了如何使用正則表達式在 Excel 中搜索和替換文本。
# Load Excel file workbook = Workbook("Workbook.xlsx") # Create replace options replace = ReplaceOptions() # Set case sensitivity and text matching options replace.setCaseSensitive(False) replace.setMatchEntireCellContents(False) # Set to true to indicate that the searched key is regex replace.setRegexKey(True) # Replace text workbook.replace("\\bKIM\\b", "^^^TIM^^^", replace) # Save as Excel XLSX file workbook.save("updated.xlsx");
如果你想試用Aspose的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn