xlrd

Excel文件处理之xlrd、xlwt、xlutils、openpyxl

荒凉一梦 提交于 2019-12-02 18:15:58
1读excel要用到xlrd模块(支持xls、xlsx) import xlrd filename="C:/Users/11/Desktop/2.xlsx" book = xlrd.open_workbook(filename) #打开Excel文件读取数据 sheet =book.sheets()[0] #指定工作表 print book.nsheets #获取sheet总数 print sheet.nrows #获取行数 print sheet.ncols #获取列数 print(sheet.cell(1,2)) #获取单元格2行3列的值 print(sheet.row_values(0)) #获取第1行的值(返回数组) print(sheet.col_values(0)) #获取第1列的值(返回数组) 指定工作表的三种方式: sheet = book.sheets()[0] sheet = book.sheet_by_index(0) sheet = book.sheet_by_name('Sheet1') #有的资料显示(u'Sheet1'),测试两种均可 2写excel要用到xlwt模块(支持xls) import xlwt book = xlwt.Workbook(encoding = 'ascii') #创建workbook sheet = book.add

Converting a Python Float to a String without losing precision

妖精的绣舞 提交于 2019-12-02 18:04:37
I am maintaining a Python script that uses xlrd to retrieve values from Excel spreadsheets, and then do various things with them. Some of the cells in the spreadsheet are high-precision numbers, and they must remain as such. When retrieving the values of one of these cells, xlrd gives me a float such as 0.38288746115497402. However, I need to get this value into a string later on in the code. Doing either str(value) or unicode(value) will return something like "0.382887461155". The requirements say that this is not acceptable; the precision needs to be preserved. I've tried a couple things so

Python-从excel里面读取数据xlrd

大憨熊 提交于 2019-12-02 12:40:04
安装 pip install xlrd import xlrdimport oscurpath = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))#获取excel路径excelPath = os.path.join(curpath,'common','hello.xlsx')#excel文件的工作表名默认是'Sheet1''Sheet2'等,打算获取工作表Sheet1里面的数据sheetName = 'Sheet1'#打开excel表data = xlrd.open_workbook(excelPath)#获取excel的工作表Sheet1table = data.sheet_by_name(sheetName)#获取Sheet1工作表中的第一行数据,一般是参数名keys = table.row_values(0)#获取工作表中数据的行数rows = table.nrows#获取工作表中数据的列数cols = table.ncols#取出excel表中的数据,放在一个list里面 list=[{'user':'11','psw':'1111','except':True},{'user':'22','psw':'22','except':True},...]lt = []for i in range(rows

Facing problem with XLWT and XLRD - Reading and writing simultaneously

倖福魔咒の 提交于 2019-12-02 08:11:02
I am facing a problem with xlrd and xlwt. Pasting the sample code below. from xlwt import Workbook, Formula, XFStyle import xlrd book = Workbook() sheet1 = book.add_sheet('Sheet 1') myFontStyle = XFStyle() myFontStyle.num_format_str = '0.00' sheet1.write(0,0,10, myFontStyle) sheet1.write(0,1,20, myFontStyle) sheet1.write(1,0,Formula('AVERAGE(A1:B1)'), myFontStyle) book.save('formula.xls') wb = xlrd.open_workbook('formula.xls') sh = wb.sheet_by_index(0) for rownum in range(sh.nrows): print sh.row_values(rownum) The idea is to write some values to Excel file, have some excel specific functions

Facing problem with XLWT and XLRD - Reading and writing simultaneously

巧了我就是萌 提交于 2019-12-02 07:45:07
问题 I am facing a problem with xlrd and xlwt. Pasting the sample code below. from xlwt import Workbook, Formula, XFStyle import xlrd book = Workbook() sheet1 = book.add_sheet('Sheet 1') myFontStyle = XFStyle() myFontStyle.num_format_str = '0.00' sheet1.write(0,0,10, myFontStyle) sheet1.write(0,1,20, myFontStyle) sheet1.write(1,0,Formula('AVERAGE(A1:B1)'), myFontStyle) book.save('formula.xls') wb = xlrd.open_workbook('formula.xls') sh = wb.sheet_by_index(0) for rownum in range(sh.nrows): print sh

python项目实战:简单操作excle表的方法

前提是你 提交于 2019-12-02 06:56:20
前言 Python操作Excle文件:使用xlwt库将数据写入Excel表格,使用xlrd 库从Excel读取数据。这篇文章主要介绍了python简单操作excle的方法,Python操作Excle文件:使用xlwt库将数据写入Excel表格,使用xlrd 库从Excel读取数据。 从excle读取数据存入数据库 1、导入模块: import xlrd 2、打开excle文件: data = xlrd.open_workbook('excel.xls') 3、获取表、行/列值、行/列数、单元值 获取一个工作表: table = data.sheets()[0] # 通过索引顺序获取 table = data.sheet_by_index(0) # 通过索引顺序获取 table = data.sheet_by_name(u'Sheet1') # 通过名称获取 获取整行/列的值,返回一个list,i表示行数和列数: table.row_values(i) table.col_values(i) 获取总行/列数: row_num = table.nrows col_num = table.ncols 获取单元格: cell_value = table.cell(0,0).value 4、插入数据库:获取到一行的值后插入,循环每一行 row = table.nrows print(row)

python xlrd

纵然是瞬间 提交于 2019-12-02 05:31:42
from xlrd import open_workbookclass Read_xlxs: def __init__(self, file): self.file = file self.test_data = [] def read_file(self, sheetname): self.data = open_workbook(self.file) self.table = self.data.sheet_by_name(sheetname) self.nrows = self.table.nrows # self.table_content = self.table.col_values(1, 1, 2) self.table_row = self.table.col_values(2) def get_file(self): for each in self.table_row[1:]: self.test_data.append(each) return self.test_dataif __name__ == '__main__': file = Read_xlxs(r'F:\MyProject\SMS\data\短信因子逻辑2019.10.15.xlsx') file.read_file('数据源') print(file.get_file()) 来源: https

Extract values from Excel spreadsheet

我与影子孤独终老i 提交于 2019-12-01 23:28:56
I want to remove some words from a list of words. I have a list with a recurring word and I want to get rid of it and I have no idea. I don't know whether I need to use a whole loop or regex. from xlrd import open_workbook,error_text_from_code book = open_workbook(inp) sheet0 = book.sheet_by_index(0) x = 0 y = 0 countr = sheet0.nrows countc = sheet0.ncols names = '' variables = [] "different variables-----------------" while x < countr -1: x = x+1 y = y+1 cell = sheet0.cell(y,0) names = names+ str(cell) cell = sheet0.cell(y,1) variables.append(cell) country_text = names countries = ', '.join

接口测试第四步 --》 封装excel

蓝咒 提交于 2019-12-01 23:03:45
import xlrd,jsondef open(): file = r'C:\Users\hui\Desktop\data.xlsx' # 创建data对象 data = xlrd.open_workbook(file) ck = data.sheet_by_index(0) rows = ck.nrows ''' range()函数生成一个迭代对象,索引从第一行开始以0表示,传入行值,再转换成json格式 ''' for i in range(0, rows): value = ck.row_values(i) value = json.dumps(value) print("乘客的下单信息为:\n%s"%(value)) open() 来源: https://www.cnblogs.com/yanhuidj/p/11720178.html

Extract values from Excel spreadsheet

二次信任 提交于 2019-12-01 23:01:51
问题 I want to remove some words from a list of words. I have a list with a recurring word and I want to get rid of it and I have no idea. I don't know whether I need to use a whole loop or regex. from xlrd import open_workbook,error_text_from_code book = open_workbook(inp) sheet0 = book.sheet_by_index(0) x = 0 y = 0 countr = sheet0.nrows countc = sheet0.ncols names = '' variables = [] "different variables-----------------" while x < countr -1: x = x+1 y = y+1 cell = sheet0.cell(y,0) names = names