使用openpyxl从一个工作簿复制到另一个工作簿会导致保存时出错

时间:2016-01-07 15:58:46

标签: python excel openpyxl

我正在尝试将一行中的值逐行附加到新工作簿。当我在一个小的测试文件上运行它时,我的代码可以工作,但是当我在目标文件上运行它时,它会在保存时返回错误。

这是我的代码:

from openpyxl import load_workbook
from openpyxl import Workbook

wb = load_workbook(filename='RM Activity-Pricing Report - 2014-05-31.xlsm',keep_vba=False, data_only=True)
ws_Ottawa = wb.get_sheet_by_name('Ottawa')

wb2 = Workbook()
ws2 = wb2.create_sheet()

for row in ws_Ottawa.iter_rows():
        ws2.append(row)

wb2.save('new_big_file.xlsx')

我在Spyder(python 3.5)中得到的输出错误是:

Traceback (most recent call last):

File "<ipython-input-22-171ffbcd4891>", line 1, in <module>
runfile('Z:/Revenue Management Report/ExtractPromoData.py', wdir='Z:/Revenue Management Report')

File "C:\Anaconda3-64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)

File "C:\Anaconda3-64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

File "Z:/Revenue Management Report/ExtractPromoData.py", line 35, in <module>
wb2.save('new_big_file4.xlsx')

File "C:\Anaconda3-64\lib\site-packages\openpyxl\workbook\workbook.py", line 298, in save
save_workbook(self, filename)

File "C:\Anaconda3-64\lib\site-packages\openpyxl\writer\excel.py", line 198, in save_workbook
writer.save(filename, as_template=as_template)

File "C:\Anaconda3-64\lib\site-packages\openpyxl\writer\excel.py", line 181, in save
self.write_data(archive, as_template=as_template)

File "C:\Anaconda3-64\lib\site-packages\openpyxl\writer\excel.py", line 87, in write_data
self._write_worksheets(archive)

File "C:\Anaconda3-64\lib\site-packages\openpyxl\writer\excel.py", line 114, in _write_worksheets
write_worksheet(sheet, self.workbook.shared_strings,

File "C:\Anaconda3-64\lib\site-packages\openpyxl\writer\worksheet.py", line 233, in write_worksheet
write_rows(xf, worksheet)

File "C:\Anaconda3-64\lib\site-packages\openpyxl\writer\lxml_worksheet.py", line 59, in write_rows
if cell.value is None and not cell.has_style:

File "C:\Anaconda3-64\lib\site-packages\openpyxl\cell\cell.py", line 306, in value
if value is not None and self.is_date:

File "C:\Anaconda3-64\lib\site-packages\openpyxl\cell\cell.py", line 351, in is_date
if self.data_type == "n" and self.number_format != "General":

File "C:\Anaconda3-64\lib\site-packages\openpyxl\styles\styleable.py", line 49, in __get__
return coll[idx - 164]
IndexError: list index out of range

当我在较小的测试.xlsx文件上使用我的代码时,我没有收到错误。

我怀疑这个问题的可能原因是:

1)输入文件是.xlsm

2)输入文件包含从A到CI的列

3)输入文件受密码保护(但由于错误是保存,这似乎不应该是一个问题)

考虑到查理所说的,这是我的解决方法   来自openpyxl import load_workbook     来自openpyxl import Workbook

 wb = load_workbook(filename='RM Activity-Pricing Report - 2014-5-31.xlsm',keep_vba=False, data_only=True)#,guess_types=True)


ws_Ottawa = wb.get_sheet_by_name('Ottawa')

wb2 = Workbook()
ws2 = wb2.create_sheet()
counter = 0
new_rows = []
for rrow in ws_Ottawa.iter_rows():
    new_rows.append([])
    for cell in rrow:
        new_rows[counter].append(cell.value)
    counter +=1
for wrow in new_rows:

    ws2.append(wrow)

wb2.save('new_big_file4.xlsx')


print("ALL DONE")

1 个答案:

答案 0 :(得分:0)

你完全不能做你想做的事。遗憾的是,数据存储在文件格式中的方式意味着很多相关信息不会与单元格一起存储,而是使用对工作簿对象的引用。这些明显不同于工作簿到工作簿,这就是您在保存时看到错误的原因:您要使用的数字格式并不存在于新文件中。