使用python进行excel自动化

问题描述 投票:0回答:1

我正在尝试编写一个函数来使用python从excel文件中读取数据。我的函数应该一次从excel表中读取行。下面是我的代码,它将打印第1行。

import xlrd  
from xlrd import open_workbook, cellname  

book = open_workbook('./Excel/Book1.xls')  
def read_excel(sheetName):  
sheet = book.sheet_by_name(sheetName)  
row = sheet.nrows  
for i in range(1):  
    rows = sheet.row_values(i+1)  
    print(rows)  
excel automation
1个回答
0
投票
file = r'd:\pythonTest.xlsx '
import xlrd 

wb = xlrd.open_workbook(file) 
sheet = wb.sheet_by_index(0) 

# For row 0 and column 0 
sheet.cell_value(0, 0) 

# Extracting number of columns 
print(sheet.ncols)
print(sheet.nrows)

print(sheet.row_values(1)) 

for i in range(sheet.nrows): 
    print(sheet.cell_value(i, 0), sheet.cell_value(i, 1)) 
© www.soinside.com 2019 - 2024. All rights reserved.