简体   繁体   中英

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:

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.

Possible reasons for the problem that I suspect are:

1)input file is .xlsm

2)input file is has columns from A to CI

3)input file is password protected (but since the error is in saving this does not seem like it should be an issue)

Taking into account what Charlie said, this is my work-around from openpyxl import load_workbook from 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")

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM