如何使用openpyxl获取单元格的实际值?

时间:2016-01-07 15:14:56

标签: csv openpyxl

我是Python的初学者,我需要帮助。我正在使用Python 2.7,我尝试检索excel文件的单元格值并将其存储到csv文件中。我的代码如下:

import os, openpyxl, csv
aggname = "deu"

wb_source = openpyxl.load_workbook(filename, data_only = True)
app_file = open(filename,'a')
dest_file = csv.writer(app_file, delimiter=',', lineterminator='\n')

calib_sheet = wb_source.get_sheet_by_name('Calibration')
data = calib_sheet['B78:C88']
data = list(data)
print(data)

for i in range(len(data)):
    dest_file.writerow(data[i])

app_file.close()

在我的csv文件中,我得到了这个,而不是实际值(例如在我的情况下:SFCG,99103)。

<Cell Calibration.B78>,<Cell Calibration.C78>
<Cell Calibration.B79>,<Cell Calibration.C79>
<Cell Calibration.B80>,<Cell Calibration.C80>
<Cell Calibration.B81>,<Cell Calibration.C81>
<Cell Calibration.B82>,<Cell Calibration.C82>
<Cell Calibration.B83>,<Cell Calibration.C83>
<Cell Calibration.B84>,<Cell Calibration.C84>
<Cell Calibration.B85>,<Cell Calibration.C85>
<Cell Calibration.B86>,<Cell Calibration.C86>
<Cell Calibration.B87>,<Cell Calibration.C87>
<Cell Calibration.B88>,<Cell Calibration.C88>

我尝试设置data_only = True,当按照类似问题的答案中的建议打开excel文件时,但它并没有解决我的问题。

--------------- EDIT -------------

考虑到我得到的前两个答案(谢谢!),我尝试了几件事:

for i in range(len(data)):
   dest_file.writerows(data[i].value)

我收到此错误消息:

for i in range(len(data)):
   dest_file.writerows(data[i].values)
Traceback (most recent call last):

  File "<ipython-input-78-27828c989b39>", line 2, in <module>
    dest_file.writerows(data[i].values)

AttributeError: 'tuple' object has no attribute 'values'

然后我尝试了这个:

for i in range(len(data)):
    for j in range(2):
        dest_file.writerow(data[i][j].value)

然后我有以下错误消息:

for i in range(len(data)):
    for j in range(2):
        dest_file.writerow(data[i][j].value)


Traceback (most recent call last):

  File "<ipython-input-80-c571abd7c3ec>", line 3, in <module>
    dest_file.writerow(data[i][j].value)

Error: sequence expected

那么,我试过这个:

import os, openpyxl, csv

wb_source = openpyxl.load_workbook(filename, data_only=True)
app_file = open(filename,'a')
dest_file = csv.writer(app_file, delimiter=',', lineterminator='\n')


calib_sheet = wb_source.get_sheet_by_name('Calibration')

list(calib_sheet.iter_rows('B78:C88'))

for row in calib_sheet.iter_rows('B78:C88'):
    for cell in row:
        dest_file.writerow(cell.value) 

仅收到此错误消息:

Traceback (most recent call last):

  File "<ipython-input-81-5bed62b45985>", line 12, in <module>
    dest_file.writerow(cell.value)

Error: sequence expected 

对于&#34;预期的序列&#34;错误我想python期望列表而不是单个单元格,所以我这样做了:

import os,openpyxl,csv

wb_source = openpyxl.load_workbook(filename, data_only=True)
app_file = open(filename,'a')
dest_file = csv.writer(app_file, delimiter=',', lineterminator='\n')


calib_sheet = wb_source.get_sheet_by_name('Calibration')

list(calib_sheet.iter_rows('B78:C88'))

for row in calib_sheet.iter_rows('B78:C88'):
        dest_file.writerow(row) 

没有错误消息,但我只获取csv文件中单元格的引用,并将其更改为dest_file.writerow(row.value),使我回到元组错误。

我显然仍然需要你的帮助!

3 个答案:

答案 0 :(得分:0)

您已经忘记了获得单元格的价值!见the documentation

答案 1 :(得分:0)

我找到了一种使用numpy的方法,它允许我将我的值存储为列表而不是元组列表。

import os, openpyxl, csv
import numpy as np

wb_source = openpyxl.load_workbook(filename, data_only=True)
app_file = open(filename,'a')
dest_file = csv.writer(app_file, delimiter=',', lineterminator='\n')


calib_sheet = wb_source.get_sheet_by_name('Calibration')

store = list(calib_sheet.iter_rows('B78:C88'))
print store

truc = np.array(store)
print truc

for i in range(11):
    for j in range(1):
        dest_file.writerow([truc[i][j].value, truc[i][j+1].value]) 

app_file.close() 

我实际上在“writerow()”中有一个序列作为我的参数,并且对于list对象,我也可以使用double index和value方法来检索我的单元格的值。

答案 2 :(得分:-1)

在打印时,请尝试使用 data.values 而不是 数据

希望它有所帮助!!

**

***An example : 
import openpyxl 
import re 
import os 

wc=openpyxl.load_workbook('<path of the file>') wcsheet=wc.get_sheet_by_name('test')  
store=[] 

for data in wcsheet.columns[0]: 
    store=data 
print(store.value)***
=======================

=============================================== ==

**

Live Life Buddha Size