How to open a excel file?

513 views
Skip to first unread message

xueguan li

unread,
Sep 16, 2015, 5:46:40 AM9/16/15
to openpyxl-users
If my excel file is saved on path of " D:/python/test.xlsx",how can i open it.
my code is like below:
   import openpyxl;
   from openpyxl import load_workbook
   wb = load_workbook('D:\python\test.xlsx')

but,the running  result is error,and the indication is:

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    wb = load_workbook('D:\python\test.xlsx')
  File "C:\Python27\lib\site-packages\openpyxl-2.3.0b2-py2.7.egg\openpyxl\reader\excel.py", line 150, in load_workbook
    archive = _validate_archive(filename)
  File "C:\Python27\lib\site-packages\openpyxl-2.3.0b2-py2.7.egg\openpyxl\reader\excel.py", line 114, in _validate_archive
    archive = ZipFile(filename, 'r', ZIP_DEFLATED)
  File "C:\Python27\lib\zipfile.py", line 699, in __init__
    self.fp = open(file, modeDict[mode])
IOError: [Errno 22] invalid mode ('rb') or filename: 'D:\\python\test.xlsx'

What is the problem ,please give me help,Thanks!

Charlie Clark

unread,
Sep 16, 2015, 6:36:15 AM9/16/15
to openpyx...@googlegroups.com
Am .09.2015, 11:46 Uhr, schrieb xueguan li <lxg...@gmail.com>:

> If my excel file is saved on path of " D:/python/test.xlsx",how can i
> open it.

On Windows you have to be very careful with file names because the \
character is treated specially in Python. The best approach is always to
use the os.path.join function to assemble a path with the correct
separators for the operating system. A quick workaround is to use a Python
raw string. The following two statements are equivalent and should work:

wb = load_workbook('D:\\python\\test.xlsx')
wb = load_workbook(r'D:\python\test.xlsx')

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226
Reply all
Reply to author
Forward
0 new messages