Criso
Criso

Reputation: 397

Extract information from Excel with python 2.7/ pyMel?

I know this is asking a lot, but I have researched quite a few ways on looking up a column or looking up a row using python within an excel document.

However, these solutions require you to import different packages, when I do not have access to them. My primary motive for this is to utilize a pre-made excel doc with shaders listed within the document and extract them based upon a brief search.

So, has anyone successfully used python to read an excel doc and, if so, have you been able to match named rows with named columns?


EDIT: Answer picked because it's the closest solution, though it doesn't necessarily answer my question. It's will, however, still provide a very reasonable solution given the context that the user, and his/her excel document, will utilize the same columns and rows to get a usable database.

TL;DR:

No easy way to compare columns to rows to get a data set without using an external package. It's easier to just extract data as an array and specify which "column" is which.

Upvotes: 2

Views: 861

Answers (4)

jiayi fang
jiayi fang

Reputation: 1

1. Installation

Maya uses Python version 2.7.11 on all supported platforms. The standalone Python shell for Maya is named mayapy.exe on Windows and is located at D:\Program Files\Autodesk\Maya2017\bin\mayapy.exe. Put it to the system environment path.

If pip.exe is not found, go to https://pip.pypa.io/en/stable/installing/ and download get-pip.py.

From a console:

C:\Users\JM-FF>python get-pip.py

After success installed pip, the pip.exe is at D:\Program Files\Autodesk\Maya2017\Python\Scripts. Put this to the system environment path.

Install XlsxWriter:

C:\Users\JM-FF>pip install XlsxWriter

2. step

Now open your Maya in ScriptEditor, write that:


import xlsxwriter

workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write(11,3,"hello")
workbook.close()

Now you can read and write data to Excel from Maya using Python.

And the hello.xlsx is at D:\Program Files\Autodesk\Maya2017\bin.

Upvotes: -1

theodox
theodox

Reputation: 12208

You can also read CSV documents using the built-in csv module to read CSV files, so don't need to install any external modules.

import csv
with open('names.csv') as csvfile:
     reader = csv.DictReader(csvfile)
     for row in reader:
         print(row['first_name'], row['last_name'])

If you just grab the whole contents of the reader and stuff them into a list, the lists will contain all the rows and each row will be a dictionary using the headers in the first row. Getting to a particular cell would be something like:

records = []
with open('names.csv') as csvfile:
     reader = csv.reader(csvfile)
     for row in reader:
         records.append(row)

# cel "A4" would be
records[0][3]

Upvotes: 3

DrWeeny
DrWeeny

Reputation: 2512

the module xlrd is already in the python package and really easy to use :

import xlrd
xlsFile = xlrd.open_workbook(path)
sheetName = xlsFile.sheets()[0].name

for s in xlsFile.sheets():
    for row in range(s.nrows):
        print s.cell(row, 1)

Upvotes: 0

Matthew
Matthew

Reputation: 658

I save my excel files as a .csv file, and import the data with numpy.genfromtxt and use the delimiter=',' argument to parse the test file.

For example:

import numpy
data = numpy.genfromtxt("my_file.csv", delimiter=",")
# do stuff with data

Upvotes: 1

Related Questions