Openpyxl损坏了我的excel文件,现在丢失了吗?

时间:2015-03-31 10:31:12

标签: openpyxl

我一直在使用openpyxl将一些数据写入已有的Excel文件中,我发现它非常友好且易于使用。 然而,事情昨天开始出错了。

我的python代码加载的excel文件在A2,B2,C2,e.t.c中有一些标题。并且代码在每列下面打印一些数据。

下午我运行了代码(我已经运行了很多并且进行了大量的试验和错误)并且它完全符合希望,除了Excel文件的各个页面中的几列已经神秘地消失了。这是因为某些列(大多数是有数据的地方)由于某种原因被最小化为0像素宽,并且一旦我将列扩展得更宽,一切都应该如此。其中一个已经使列最小化的工作表没有被代码触及。不确定这是否相关,但发现它很奇怪所以我认为无论如何我都会添加它。

然后在晚上,我再次运行代码,试用了我添加的几个新功能。它看起来工作正常,但当它到达最后的.save()命令时,它抛出了所有这些错误:


 Traceback (most recent call last):
File "whoscored_scraper.py", line 239, in <module>
wb2.save("WhoscoredDatabase.xlsx")
File "C:\Users\SamH\Documents\Betting\Python\openpyxl\workbook\workbook.py", l
ine 296, in save
save_workbook(self, filename)
File "C:\Users\SamH\Documents\Betting\Python\openpyxl\writer\excel.py", line 1
91, in save_workbook
writer.save(filename, as_template=as_template)
File "C:\Users\SamH\Documents\Betting\Python\openpyxl\writer\excel.py", line 1
74, in save
self.write_data(archive, as_template=as_template)
File "C:\Users\SamH\Documents\Betting\Python\openpyxl\writer\excel.py", line 8
5, in write_data
self._write_worksheets(archive)
File "C:\Users\SamH\Documents\Betting\Python\openpyxl\writer\excel.py", line 1
11, in _write_worksheets
write_worksheet(sheet, self.workbook.shared_strings,
File "C:\Users\SamH\Documents\Betting\Python\openpyxl\writer\worksheet.py", li
ne 299, in write_worksheet
xf.write(comments)
File "C:\Users\SamH\Documents\Betting\Python\lib\contextlib.py", line 24, in _
_exit__
self.gen.next()
File "C:\Users\SamH\Documents\Betting\Python\openpyxl\xml\xmlfile.py", line 51
, in element
self._write_element(el)
File "C:\Users\SamH\Documents\Betting\Python\openpyxl\xml\xmlfile.py", line 78
, in _write_element
xml = tostring(element)
File "C:\Users\SamH\Documents\Betting\Python\lib\xml\etree\ElementTree.py", li
ne 1126, in tostring
ElementTree(element).write(file, encoding, method=method)
File "C:\Users\SamH\Documents\Betting\Python\lib\xml\etree\ElementTree.py", li
ne 820, in write
serialize(write, self._root, encoding, qnames, namespaces)
File "C:\Users\SamH\Documents\Betting\Python\lib\xml\etree\ElementTree.py", li
ne 939, in _serialize_xml
_serialize_xml(write, e, encoding, qnames, None)
File "C:\Users\SamH\Documents\Betting\Python\lib\xml\etree\ElementTree.py", li
ne 939, in _serialize_xml
_serialize_xml(write, e, encoding, qnames, None)
File "C:\Users\SamH\Documents\Betting\Python\lib\xml\etree\ElementTree.py", li
ne 932, in _serialize_xml
v = _escape_attrib(v, encoding)
File "C:\Users\SamH\Documents\Betting\Python\lib\xml\etree\ElementTree.py", li
ne 1092, in _escape_attrib
_raise_serialization_error(text)
File "C:\Users\SamH\Documents\Betting\Python\lib\xml\etree\ElementTree.py", li
ne 1052, in _raise_serialization_error
"cannot serialize %r (type %s)" % (text, type(text).__name__)
TypeError: cannot serialize 7L (type long)

我的代码很长,所以下面是一些涉及openpyxl的行:


from openpyxl import workbook
from openpyxl import load_workbook
wb2 = load_workbook('filename.xlsx')
ws = wb2["Shots_and_Goals"]
ws4 = wb2["Assorted"]
ws2 = wb2["On_Target"]

def HomeTeam(string):
    HomeTeam_cell = “c” + str(3)
    ws[str(HomeTeam_cell)] = Home_Team   #Home_Team is some variable

def Home_Shot_Minutes(string):
   for num in range (0, 10):
       cell = column_titles[num+10] + str(3)] #column_titles is just a list
       ws[str(cell)] = int(shot_count_for_that_minute)    #shot_count_for_that_minute is variable defined above

HomeTeam(data)
Home_Shot_Minutes(data)
Wb2.save(“filename.xlsx”)

有很多代码行与上面列出的代码基本相同。到目前为止,一切都工作正常。

当我尝试打开新保存的文件时,它说Excel发现了不可读的内容 当我说我想要恢复不可读的内容时,我的新excel文件除了工作表名称之外完全是空白的。

我知道将文件保存为不同的文件名是一个愚蠢的决定,因为这意味着我不必重新制作整个Excel文件!

有谁知道这里出了什么问题?

1 个答案:

答案 0 :(得分:0)

不幸的是,由于原始文件被覆盖,数据会丢失。

请提交原始文件的错误报告,以便我们进行调查。很明显错误来自哪里,但我们需要原始数据来重现错误。

与此同时,我怀疑如果安装lxml,问题不太可能发生。