save changes to an existing .xlsx gives KeyError: 549131827
Created originally on Bitbucket by rwlantz (Rob Lantz)
Hi. I'm currently using openpyxl 1.8.2, and Python 2.7.6 (Anaconda 1.9.2). I can run the following script the first time and it works fine. The second time it chokes when trying to save the file, and corrupts the file. Strangely, if I open the file between the first and second attempts at running the script, and manually delete the information that is written to the cells it resolves the problem. I attached the .docx that I'm reading from, and the .xlsx is in its state following the first running of the script. Any help is appreciated. Best, Rob.
#!python
import os
import xml.etree.ElementTree
import docx as dx
import openpyxl as xl
import easygui as eg
mydoc = dx.Document("test.docx")
mytab = mydoc.tables[0]
mypar = mydoc.paragraphs[0]
mybk = xl.load_workbook("testbk.xlsx")
mysht = mybk["Sheet1"]
tablist = []
parlist = []
for r in mypar.runs:
parlist.append(r.text)
mysht['B2'] = parlist[0]
for row in mytab.rows:
for cel in row.cells:
tablist.append(cel.paragraphs[0].text)
i = 2
for tv in tablist:
mysht.cell(row = i, column = 1).value = tv
i +=1
eg.msgbox("Congrats, it worked, and you've imported " + str(len(tablist) + len(parlist)) + " pieces of info", title = "Success!")
mybk.save('testbk.xlsx')
Attachments: test.docxtestbk.xlsx