python 打开excel并在屏幕上呈现_在python中打开并阅读excel .xlsx文件

1586010002-jmsa.png

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 tried to install numpy but it still can't find numpy.

I've also tried using the xlrd library but I get the following traceback:

Traceback (most recent call last):

File "C:\test.py", line 3, in

book = open_workbook('test.xlsx')

File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 370, in open_workbook

biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)

File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 1323, in getbof

raise XLRDError('Expected BOF record; found 0x%04x' % opcode)

XLRDError: Expected BOF record; found 0x4b50

Which I assume is because XLRD can't read .xlsx files?

Anyone got any ideas?

EDIT:

import csv

with open('test.csv', 'rb') as csvfile:

data = csv.reader(csvfile, delimiter=',')

for row in data:

print "------------------"

print row

print "------------------"

for cell in row:

print cell

解决方案

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]

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值