[openpyxl] load_workbook file import question

Asked 2 years ago, Updated 2 years ago, 102 views

 Ubuntu 18.04
Windows 10
Hansel 2014
anaconda    4.6.14
Python      3.7.2
openpyxl    2.6.2
PyQt5       5.12.2
PyInstaller 3.4
import os
from openpyxl import load_workbook


filename = '/Users/feynman/Desktop/test.xlsx'
# # filename = 'C:\\Users\\feynman\\Desktop\\test.xlsx'
# # filename = r'C:\Users\feynman\Desktop\test.xlsx'
# # filename = 'C:/Users/feynman/Desktop/test.xlsx'

book = load_workbook(filename=filename)
# access files in the same location as book = load_workbook (filename = 'test.xlsx')
# # book = load_workbook(filename=os.path.join(path, 'filename.xlsx'))

sheet = book.active
sheet['E1'] = 200
book.save(filename='testtest.xlsx')

raise TypeError('expected ' + str(self.expected_type)) TypeError: expected

Calling the load_workbook() function results in an error.

from openpyxl import Workbook

wb = Workbook()
ws = wb.active
ws['A1'] = 1
wb.save(filename='test.xlsx')

It works well if you create an Excel file using the above method and access it with load_workbook. However, if you modify a file (writing on a cell, changing fonts, etc.), the same error occurs. Why does this error occur? I don't know if I follow the debugs...

It went well in Ubuntu, so I installed LibreOffice in Windows and created an Excel file. And unlike Hansel, it reads the file well. Then I can't load the Hansel file, but why is it like this? And where should I change the code? I'm still watching the debugs, but I don't know the difference between the files that work and the files that don't work.

python openpyxl

2022-09-22 15:24

1 Answers

There is a possibility that the Excel version you are using is higher.

The appropriate openpyxl module site says "A Python library to read/write Excel 2010 xlsx/xlsm files".

Only the Excel 2010 format is likely to work properly.

Of course, when I save it in openpyxl, I will save it in 2010 format, so there will be no problem with load_workbook.

When you save it in Excel, save it in a low version and test it.


2022-09-22 15:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.