本文介绍了如何使用openpyxl写入xlsm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写这段代码.我的目标是在此excel文件的特定单元格中设置一个值.该代码运行正常,并且没有错误退出,但是单元格A1保持空白.我该如何解决?

I am writing this piece of code. My objective is to set a value in a specific cell of this excel file. The code is running quite ok and exits with no error, but the cell A1 remains blank. How can I fix this?

import openpyxl
wb = load_workbook('Test.xlsm')
ws = wb.worksheets[1]
ws.cell(row=1, column=1).value = 999999
wb.save('Test.xlsm')

推荐答案

from openpyxl import Workbook
from openpyxl import load_workbook

wb = load_workbook('Test.xlsm',keep_vba=True)
ws = wb['Sheet1']
ws.cell(row=1,column=1).value = 9999999
wb.save('Test.xlsm')

由于您要修改.xlsm(启用Excel宏的工作簿),因此需要启用" keep_vba ​​",我不知道为什么,但这似乎行得通.

You need to have 'keep_vba' enabled since you are trying to modify an .xlsm(Excel Macro Enabled Workbook),I don't know why but this seems to work.

wb = load_workbook('Test.xlsm',keep_vba=True)

这篇关于如何使用openpyxl写入xlsm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 23:03