Opening and reading an excel .xlsx file in python

后端 未结 3 491
星月不相逢
星月不相逢 2021-01-03 00:15

I\'m trying to open an excel .xlsx file with python but am unable to find a way to do it, I\'ve tried using pandas but it\'s wanting to use a library called NumPy I\'ve trie

相关标签:
3条回答
  • 2021-01-03 00:37

    Maybe you could export your .xlsx to a .csv file?

    Then you could try:

    import csv
    with open('file.csv','rb') as file:
        contents = csv.reader(file)
        [x for x in contents]
    

    This may be useful: http://docs.python.org/2/library/csv.html#csv.reader

    Hope that helps!

    EDIT:

    If you want to locate a spectific cell, such as F13, you could make a nested list like a matrix and them refer to each element:

    import csv
    with open('file.csv','rb') as file:
        contents = csv.reader(file)
        matrix = list()
        for row in contents:
            matrix.append(row)
    

    And then access F13 with matrix[5][12].

    P.S.: I did not test this. If "row" is a list with each cell as an element, you keep appending all lines to the matrix, so the first index is row number and the second is the column number.

    0 讨论(0)
  • 2021-01-03 00:41

    it seems that you are on a Linux Distro. I had the same problem too and this does not happen with "xlwt" library but only with "xlrd". what I did is not the right way to solve this problem but it makes things work for the time being to hopefully have an answer to that question soon ;I have installed "xlrd" on Windows and took the folder and pasted it on Linux in the directory where my python code is and it worked.

    0 讨论(0)
  • 2021-01-03 00:47

    Since I know other people will also be reading this -

    You can install the following module (it's not there automatically) https://pypi.python.org/pypi/openpyxl

    You can read the following to get a nice breakdown on how to use it

    https://automatetheboringstuff.com/chapter12/

    0 讨论(0)
提交回复
热议问题