Python3 to Pyxl

Hello,

I have been following tutorial:

[A Guide to Excel Spreadsheets in Python With openpyxl – Real Python ]
https://realpython.com/openpyxl-excel-spreadsheets-python/#practical-use-cases)

But when I open the Python3 IDLE and enter:

from openpyxl import load_workbook
workbook = load_workbook(filename="sample.xlsx")
workbook.sheetnames


sheet = workbook.active
sheet

sheet.title

I get this:

Python 3.9.1 (v3.9.1:1e5d33e9b9, Dec  7 2020, 12:44:01) 
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license()" for more information.
>>> from openpyxl import load_workbook
workbook = load_workbook(filename="sample.xlsx")
workbook.sheetnames


sheet = workbook.active
sheet


sheet.title

SyntaxError: multiple statements found while compiling a single statement
>>> 

Can anyone help please

Jason

I have tried typing in script manually, still not working:

>>> from openpyxl import load_workbook
>>> workbook = load_workbook(filename=‘sample.xlsx’)
SyntaxError: invalid character '‘' (U+2018)

or:

>>> from openpyxl import load_workbook
>>> workbook = load_workbook(filename = sample.xlsx)
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    workbook = load_workbook(filename = sample.xlsx)
NameError: name 'sample' is not defined

The first error I just don’t understand, but the second refers to name error. I have tried different paths to the file with no success.

Still need some help please
Jason

Success,

I found the path to file by right click on the file then press and hold option button. This changes copy to copy" “as pathname, click on copy” "as pathname and you have it to paste wherever you like. I am using a MacBook Pro with Big Sur.

Working things out slowly!
Jason

1 Like

When putting quotes around the filename, you need to use either the single quote ’ at the start and end, or the double quote ". The same quote opens and closes the string. You were using the opening (backward?) quote at the start of the string, and python doesn’t like that. So use ‘file’ not `file’, or “file”.

Hope this helps.

1 Like