Error with Openpyxl: Pyimport is not in current path

I am looking for a package which can read and write an excel file cell by cell. I know openpyxl do the job. Is there any other better package in Julia which does the same job?

Now, I am trying to install it, so I used the following piece of code:

using Pkg
import Conda
Conda.add("openpyxl")
ENV["PYTHON"]="C:\\Python37-x64\\python.exe"; Pkg.build("PyCall")
@pyimport openpyxl

I am getting the following error:

LoadError: LoadError: UndefVarError: @pyimport not defined
in expression starting at C:\Users\Administrator\Desktop\Test Code [Julia]\TESTING_1.jl:14
in expression starting at C:\Users\Administrator\Desktop\Test Code [Julia]\TESTING_1.jl:14
top-level scope

How to get rid of this error?

I am using Julia v1.0.0.

Looks like you didn’t call using PyCall? Also, note that:

@pyimport foo is deprecated in favor of foo = pyimport("foo").

Another point: If you want to use Conda.jl to install openpyxl, don’t do ENV["PYTHON"]="C:\\Python37-x64\\python.exe"; Pkg.build("PyCall"). Run ENV["PYTHON"]=""; Pkg.build("PyCall").

You could look at GitHub - felipenoris/XLSX.jl: Excel file reader and writer for the Julia language.

Thanks for your response. I have changed the code as follows:

import Conda
import Pkg
Conda.add("openpyxl")
ENV["PYTHON"]=""; Pkg.build("PyCall")
using PyCall
pyimport("openpyxl")

Still, I am getting the following error:

LoadError: ArgumentError: Package PyCall not found in current path:
-Run import Pkg; Pkg.add("PyCall") to install the PyCall package.
in expression starting at C:\Users\Administrator\Desktop\Rajat_Sanyal\Test Code [Julia]\TESTING_1.jl:14
require(::Module, ::Symbol) at loading.jl:823

I have found two Julia folders in my system (added both of them in the environment variables) which are as follows:

  1. C:\Users\Administrator\AppData\Local\Julia-1.1.0
  2. C:\Users\Administrator\.julia

Any idea how to resolve the issue?

have you tried using Tutorial ¡ XLSX.jl ?

I have used the XLSX.jl package for data writing, but have not tried for reading. Actually, I have seen the tutorial, but only few examples are provided. I was not sure how it could be used to read an excel file cell by cell. I will try once again, and let you know about it. Thanks!

Reading by cell examples are here :smile: hope it works for you

1 Like

Does it work if you do what the error message suggests?

I am not able to understand how to add the Pycall to the path.

Hi, @YongHee-Kim thanks for your input. Let me put my problem properly.

Let us consider we have an excel sheet which contains cities in a row and population is a column. Now, we don’t know how many cities are there. I am trying to write a program which will take the number of cities as an input from a user/or a cell, then save the data in an array.

Let me try to write a mock code:

G = 10; # Input 
v = zeros(1,10)
f = XLSX.openxlsx("DATA.xlsx", enable_cache=false)
sheet = f["Sheet1"]
for g in 1:G
     v = sheet[g+1,2]
     println("v=$v")
end

Doubt:

  1. How to do it correctly?
  2. Is there any better way to do it?

You can access all of your excel data with just numbers
you don’t need excel address types

using XLSX

xf = XLSX.readxlsx("data.xlsx")

# All datas in 'Sheet1'
mydata = xf["Sheet1"][:]

# access to range
firstrow = mydata[1, :]
firstcolumn = mydata[:, 1]

# so if you want datas until 10th row
g = 10
columnA = mydata[1:g, 1]
columnB = mydata[1:g, 2]
1 Like

Run import Pkg; Pkg.add("PyCall") to install the PyCall package.

Thanks a lot. It did the magic. :slight_smile:

Just a small doubt. If we do as follows:

In this case, it reads the whole file first which may be a cumbersome job. Is it possible to extract the data directly? Go to the sheet and extract the columns/rows using the numbers (not using the excel address).

I guess that will drastically reduce the reading time.