简体   繁体   English

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

[英]Using openpyxl to copy from one workbook to another results in error when saving

I am trying to append the values from one sheet row by row to a new workbook. 我试图将一个工作表中的值逐行追加到新工作簿中。 My code works when I run it on a small test file, but when I run it on my target file it returns an error when saving. 当我在一个小的测试文件上运行它时,我的代码可以工作,但是当我在目标文件上运行它时,它在保存时返回错误。

Here is my code: 这是我的代码:

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')

The output error I get in Spyder (python 3.5) is: 我在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

I do not get an error when I use my code on a smaller test .xlsx file. 在较小的测试.xlsx文件上使用代码时,没有出现错误。

Possible reasons for the problem that I suspect are: 我怀疑该问题的可能原因是:

1)input file is .xlsm 1)输入文件是.xlsm

2)input file is has columns from A to CI 2)输入文件的列从A到CI

3)input file is password protected (but since the error is in saving this does not seem like it should be an issue) 3)输入文件受密码保护(但是由于保存时出错,这似乎不应该成为问题)

Taking into account what Charlie said, this is my work-around from openpyxl import load_workbook from openpyxl import Workbook 考虑到查理说的话,这是我从openpyxl import Workbook到openpyxl import load_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")

You quite simply cannot do what you are trying to do. 您完全无法做您想做的事情。 Unfortunately, the way data is stored within the file formats means that much relevant information is not stored with the cell but using reference to the workbook object. 不幸的是,数据在文件格式中的存储方式意味着许多相关信息没有随单元存储,而是使用对工作簿对象的引用。 These obviously differ from workbook to workbook which is why you see errors when saving: the number format you want to use doesn't exist in the new file. 这些显然在工作簿之间是不同的,这就是为什么保存时会看到错误:新文件中不存在您要使用的数字格式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 openpyxl 将工作表从一个工作簿复制到另一个工作簿? - How to copy worksheet from one workbook to another one using openpyxl? 尝试使用 OPENPYXL 将一系列单元格从一个工作簿复制到另一个工作簿 - Trying to copy a range of cells from one workbook to another using OPENPYXL 想要使用 openpyxl 将工作表从一个工作簿添加到另一个工作簿 - want to add worksheet from one workbook in to another workbook using openpyxl 使用openpyxl将单元格值列从一个工作簿复制到另一个工作簿 - Copy column of cell values from one workbook to another with openpyxl 使用 Openpyxl 将特定列从一个工作簿复制到另一个工作簿 - Copying Specific Column from One Workbook to Another using Openpyxl Python/openpyxl - 有没有办法将工作表从一个工作簿复制到另一个具有所有属性的工作簿(精确副本) - Python/openpyxl - Is there a way to copy a worksheet from one workbook to another with all properties (exact copy) 使用Openpyxl将数据从一个工作簿移动到另一个工作簿 - Moving Data From One Workbook To Another With Openpyxl 使用 Openpyxl 加载工作簿时 Python 出错? - Error in Python When Loading Workbook Using Openpyxl? 使用openpyxl将公式从一个单元格复制到另一个单元格 - Copy formula from one cell to another using openpyxl 使用 openpyxl 将工作表(数据+样式)从工作簿复制到 Python 中的另一个 - Copy a worksheet (data+style) from a workbook to another in Python using openpyxl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM