# How to read excel file and use as coordinates in python code? [duplicate]

Currently this is the excel file:

How do I use python to import the file and get it to read it so that it effectively reads it as:

x, y, z, l, m, n, p, q, r = 5000, 300, 2700, 0.2, -1, 0.1, 0, 0, 0

And is it possible to read more than one line as shown in the image and create another set of coordinates?

The simplest way for this kind of data would be to convert to a .csv (comma-separated-values) file. You can do it easily in Excel with Save As > csv. Then simply access the lines with open(path).

This is my input file :

import bpy

for line_number, line in enumerate(open(r'C:\YourFolder\YourFile.csv')):
columns = line.rstrip().split(';')  # Replace by a comma ',' if the separator is a comma
if line_number == 0:  # Populate the headers
else:
print(f'Line {line_number}:')
for i, column in enumerate(columns):


Result :

In your specific case, you can skip the first column and skip lines easily. Keep in mind in python and most programming languages, the first element of a list has an index of 0. There are a few ways to do it but take this for example.

The file :

New code :

import bpy

from mathutils import Vector

vectors = []

for line_number, line in enumerate(open(r'C:\YourFolder\YourFile.csv')):
columns = line.rstrip().split(';')  # Replace by a comma ',' if the separator is a comma
if line_number == 0:  # Populate the headers
elif line_number % 2 == 0:  #  Keep only odd lines (which have a pair index since it's zero-based)
vectors.append(Vector((float(c) for c in columns[1:4])))
print(f'Line {line_number + 1}:')
for i, column in enumerate(columns[1::]):  # Skip the first column
print(f'{headers[i + 1]} = {column}')

print(vectors)


Result :

• Consider that there are hundreds of posts and pretty good answers about how to import csv data here already... no need to repeat that (IMHO): blender.stackexchange.com/search?tab=votes&q=import%20csv – brockmann May 4 '20 at 11:27
• Thanks @Gorgious, how would I be able to skip rows, eg choosing row 3 and 5 and dismissing the first column for names? Is it also possible to say x= (a specific cell in excel), y= (another specific cell) etc- I want to be able to use these values creating vectors/matrices – newblender May 4 '20 at 11:44
• @newblender I've updated my answer. If you want to access specific cells in the excel sheet, you can do it with csv but you will have less trouble with the pandas module as stated in the other answer. The csv method is more of a linear parse-and-convert approach. – Gorgious May 4 '20 at 12:00
• I seem to get an error 'line_number is not defined' what would I need to define it as? And rather than printing the values, am I able to equate x=.. and so on? – newblender May 4 '20 at 13:08
• Sorry that was a typo ! should be good now. If you want to set variables to the value of some column, you can just use for example instead of the second print statement, if i == 0: x = column. Note that I added a vectors list (which get printed at the end) which gets the x, y, and z value in each line. – Gorgious May 4 '20 at 13:25

Alternatively, you could use the pandas library. (How to install - see here: Using 3rd party Python modules)

With pandas, you can then use the pandas.read_excel() to import your worksheet into a DataFrame, which is basically an array but with named columns to make indexing easier.

Personally, if it's an option, I would export as a CSV and read it in with something like https://docs.python.org/3/library/csv.html