如何解决无法保存在 xlsx 文件中所做的修改
from xml.dom import minidom as md
[... some code ....]
sheet = workdir + '/xl/worksheets/sheet'
sheet1 = sheet + '1.xml'
importSheet1 = open(sheet1,'r')
whole_file= importSheet1.read()
data_Sheet = md.parseString(whole_file)
[... some code ....]
self.array_mem_name = []
y = 1
x = 5 #first useful row
day = int(day)
found = 0
while x <= len_array_shared:
readrow = data_Sheet.getElementsByTagName('row')[x]
c_data = readrow.getElementsByTagName('c')[0]
c_attrib = c_data.getAttribute('t')
if c_attrib == 's':
vName = c_data.getElementsByTagName('v')[0].firstChild.nodeValue
#if int(vName) != broken:
mem_name = self.array_shared[int(vName)]
if mem_name != '-----':
if mem_name == old:
c_data = readrow.getElementsByTagName('c')[day]
c_attrib = c_data.getAttribute('t')
if (c_attrib == 's'):
v_Attrib = c_data.getElementsByTagName('v')[0].firstChild.nodeValue
if v_Attrib != '':
#loc = self.array_shared[int(v_Attrib)]
index = self.array_shared.index('--')
c_data.getElementsByTagName('v')[0].firstChild.nodeValue = index
with open(sheet1,'w') as f:
f.write(whole_file)
如您所见,我使用了 f.write(whole_file)
,但 whole_file 没有使用 index
进行的更改。
检查调试我看到新值已添加到节点,但我无法使用修改后的值保存 sheet1
解决方法
我改用 openpyxl,正如 a comment by Lei Yang 中所建议的那样。我发现这个工具更适合我的工作。使用 openpyxl,读取单元格值比使用 xml.dom.minidom
容易得多。
我唯一担心的是 openpyxl 似乎真的比加载工作簿的 dom 慢。可能是内存超载了。但是,我对使用比这个小性能问题更简单的东西更感兴趣。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。