尝试保存在openpyxl时出现密钥错误 - 破坏Excel文件

时间:2014-05-19 14:30:04

标签: python excel openpyxl

我正在尝试使用openpyxl自动化报告,并处于早期阶段。我刚刚设置了第一部分,我将一系列单元格从一个工作簿复制粘贴到另一个工作簿。但是,当我去保存工作簿时,我粘贴它会返回一个关键错误。它还会破坏我的模板文件并使其无法打开。

我找到了一个有同样问题的小伙子,但他没有得到解决。有人指示他使用1.8.5版本,但我最多只能找到1.8.2。

ERROR: Failure: KeyError (-8937945243006069197)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "//anaconda/lib/python2.7/site-packages/nose/loader.py", line 413, in loadTestsFromName
        addr.filename, addr.module)
      File "//anaconda/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath
        return self.importFromDir(dir_path, fqname)
      File "//anaconda/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir
        mod = load_module(part_fqname, fh, filename, desc)
      File "/Users/joegavin/Dropbox/Python/projects/project pymur/tests/sandbox_tests.py", line 53, in <module>
        wb0.save('template.xlsx')
      File "//anaconda/lib/python2.7/site-packages/openpyxl/workbook.py", line 265, in save
        save_workbook(self, filename)
      File "//anaconda/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 187, in save_workbook
        writer.save(filename)
      File "//anaconda/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 170, in save
        self.write_data(archive)
      File "//anaconda/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 98, in write_data
        self._write_worksheets(archive, shared_string_table, self.style_writer)
      File "//anaconda/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 128, in _write_worksheets
        style_writer.get_style_by_hash()))
      File "//anaconda/lib/python2.7/site-packages/openpyxl/writer/worksheet.py", line 98, in write_worksheet
        write_worksheet_cols(doc, worksheet, style_table)
      File "//anaconda/lib/python2.7/site-packages/openpyxl/writer/worksheet.py", line 209, in write_worksheet_cols
        col_def['style'] = str(style_table[hash(columndimension.style_index)])
    KeyError: -8937945243006069197

到目前为止,这是我的所有代码。

from openpyxl import load_workbook
from openpyxl import workbook

########################### CLASSES ####################################

class Institution(object):
    def __init__(self, name, usage_hours,percentage_share,
                 target_percentage, number_of_projects):
        self.name = name
        self.usage_hours = usage_hours
        self.percentage_share = percentage_share
        self.target_percentage = target_percentage
        self.number_of_projects = number_of_projects


class Project(object):
    def __init__(self, project_name, project_code, percentage_share,
                 percentage_share_machine):
        self.project_name = project_name
        self.percentage_share = percentage_share
        self.percentage_share_machine = percentage_share_machine



############################ Setup ########################################



# Loading the workbook to read the figures off
wb0 = load_workbook('figures.xlsx')
wb1 = load_workbook('template.xlsx')

# Navigating to the worksheet where we harvest the data
ws0 = wb0.worksheets[0]
# Navigating to the worksheet we will copy data to
ws1 = wb1.worksheets[0]



project_codes0 = ws0["B1":"B100"]

project_codes1 = ws1['B1':'B100']

#cells1 = cells0

#for i in range(0, len(cells0[0])):
    #for cell in cells0:
        #print cell[i].value

wb0.save('template.xlsx')

1 个答案:

答案 0 :(得分:1)

根据OP的评论

  

通过更新到最新版本2.02解决了问题