如何在不更改Python中的原始格式的情况下编辑现有Excel?

2024-03-28 18:09:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Python中现有Excel文件的单个单元格中编辑或插入特定值。我的主要问题是,原始Excel包含一些选择选项单元格、图像和另一张工作表,当我保存新文件时,这些单元格、图像和工作表将消失(只有一个更改的值)。我正在使用openpyxl库,但我不知道它是否最适合这个方案。我把代码留在这里:

import openpyxl
wbkName = "path_to_xlsx_file"
wbk = openpyxl.load_workbook(wbkName)
# Sheet1 is what the sheet is called where is the cell that I want to change
sheet = wbk['Sheet1']
# 54, 6 are the row number and col number of the cell I want to change (insert a 5 in this case)
sheet.cell(54,6).value = 5
wbk.save(wbkName)
wbk.close

Tags: 文件theto图像numberiscellchange