xlrd

Python xlrd、xlwt、xlutils修改Excel文件-OK

吃可爱长大的小学妹 提交于 2019-11-29 13:40:40
一、xlrd读取excel 这里介绍一个不错的包xlrs,可以工作在任何平台。这也就意味着你可以在Linux下读取Excel文件。 首先,打开workbook; import xlrd wb = xlrd.open_workbook('myworkbook.xls') 检查表单名字: wb.sheet_names() 得到第一张表单,两种方式:索引和名字 sh = wb.sheet_by_index(0) sh = wb.sheet_by_name(u'Sheet1') 递归打印出每行的信息: for rownum in range(sh.nrows): print sh.row_values(rownum) 如果只想返回第一列数据: first_column = sh.col_values(0) 通过索引读取数据: cell_A1 = sh.cell(0,0).value cell_C4 = sh.cell(rowx=3,colx=2).value 注意:这里的索引都是从0开始的。 二、xlwt写excel 这里介绍一个不错的包xlwt,可以工作在任何平台。这也就意味着你可以在Linux下保存Excel文件。 基本部分 在写入Excel表格之前,你必须初始化workbook对象,然后添加一个workbook对象。比如: import xlwt wbk = xlwt

python3使用xlrd、xlwt、xlutils、openpyxl、xlsxwriter操作excel

和自甴很熟 提交于 2019-11-29 13:39:08
特色简介 xlrd主要用来读excel,针对. xls 格式; xlwt主要用来写excel,针对. xls 格式,超出excel 的单格内容长度上限32767,就会报错; xlutils结合xlrd可以达到修改excel文件目的,需要注意的是你必须同时安装这三个库; openpyxl读写 .xlsx 格式的excel,无长度限制; xlsxwriter可以写excel文件并加上图表,缺点是不能打开/修改已有文件,意味着使用 xlsxwriter 需要从零开始。 xlrd import xlrd #打开excel data = xlrd.open_workbook('demo.xls') #注意这里的workbook首字母是小写 #查看文件中包含sheet的名称 data.sheet_names() #得到第一个工作表,或者通过索引顺序 或 工作表名称 table = data.sheets()[0] table = data.sheet_by_index(0) table = data.sheet_by_name(u'Sheet1') #获取行数和列数 nrows = table.nrows ncols = table.ncols #获取整行和整列的值(数组) table.row_values(i) table.col_values(i) #循环行,得到索引的列表 for

Read merged cells in Excel with Python

人走茶凉 提交于 2019-11-29 08:07:58
问题 I am trying to read merged cells of Excel with Python using xlrd. My Excel: (note that the first column is merged across the three rows) A B C +---+---+----+ 1 | 2 | 0 | 30 | + +---+----+ 2 | | 1 | 20 | + +---+----+ 3 | | 5 | 52 | +---+---+----+ I would like to read the third line of the first column as equal to 2 in this example, but it returns '' . Do you have any idea how to get to the value of the merged cell? My code: all_data = [[]] excel = xlrd.open_workbook(excel_dir+ excel_file)

Extract columns from Excel using Python

泄露秘密 提交于 2019-11-29 08:06:55
I have an Excel file with the ff: row/col structure ID English Spanish French 1 Hello Hilo Halu 2 Hi Hye Ghi 3 Bus Buzz Bas I would like to read the Excel file, extract the row and col values, and create 3 new files base on the columns English, Spanish, and French. So I would have something like: English File: "1" = "Hello" "2" = "Hi" "3" = "Bus" I've been using xlrd. I can open, read, and print the contents of the file. However, this is what I get using this command (with the Excel file already open): for index in xrange(0,2): theWord = '\n' + str(sh.col_values(index, start_rowx=index, end

Read Excel XML .xls file with pandas

我们两清 提交于 2019-11-29 07:30:57
I'm aware of a number of previously asked questions, but none of the solutions given work on the reproducible example that I provide below. I am trying to read in .xls files from http://www.eia.gov/coal/data.cfm#production -- specifically the Historical detailed coal production data (1983-2013) coalpublic2012.xls file that's freely available via the dropdown. Pandas cannot read it. In contrast, the file for the most recent year available, 2013, coalpublic2013.xls file, works without a problem: import pandas as pd df1 = pd.read_excel("coalpublic2013.xls") but the next decade of .xls files (2004

Python xlrd.Book: how to close the files?

≡放荡痞女 提交于 2019-11-29 04:06:19
I read 150 excel files in a loop, opening them with xlrd.open_workbook() , which returns a Book object. At the end, when I tried to umount the volume, I was unable, and when I checked with lsof , I found that 6 of the files were still open: $ lsof | grep volumename python2 32349 deeenes mem REG 0,40 138240 181517 /.../150119.xls python2 32349 deeenes mem REG 0,40 135168 181482 /.../150609.xls python2 32349 deeenes mem REG 0,40 140800 181495 /.../140828.xls python2 32349 deeenes 5r REG 0,40 140800 181495 /.../140828.xls python2 32349 deeenes 6r REG 0,40 135168 181482 /.../150609.xls python2

Reading Excel file is magnitudes slower using openpyxl compared to xlrd

十年热恋 提交于 2019-11-28 20:42:26
I have an Excel spreadsheet that I need to import into SQL Server on a daily basis. The spreadsheet will contain around 250,000 rows across around 50 columns. I have tested both using openpyxl and xlrd using nearly identical code. Here's the code I'm using (minus debugging statements): import xlrd import openpyxl def UseXlrd(file_name): workbook = xlrd.open_workbook(file_name, on_demand=True) worksheet = workbook.sheet_by_index(0) first_row = [] for col in range(worksheet.ncols): first_row.append(worksheet.cell_value(0,col)) data = [] for row in range(1, worksheet.nrows): record = {} for col

XLRD/Python: Reading Excel file into dict with for-loops

喜夏-厌秋 提交于 2019-11-28 17:55:35
I'm looking to read in an Excel workbook with 15 fields and about 2000 rows, and convert each row to a dictionary in Python. I then want to append each dictionary to a list. I'd like each field in the top row of the workbook to be a key within each dictionary, and have the corresponding cell value be the value within the dictionary. I've already looked at examples here and here , but I'd like to do something a bit different. The second example will work, but I feel like it would be more efficient looping over the top row to populate the dictionary keys and then iterate through each row to get

How to obtain sheet names from XLS files without loading the whole file?

戏子无情 提交于 2019-11-28 17:13:57
I'm currently using pandas to read an Excel file and present its sheet names to the user, so he can select which sheet he would like to use. The problem is that the files are really big (70 columns x 65k rows), taking up to 14s to load on a notebook (the same data in a CSV file is taking 3s). My code in panda goes like this: xls = pandas.ExcelFile(path) sheets = xls.sheet_names I tried xlrd before, but obtained similar results. This was my code with xlrd: xls = xlrd.open_workbook(path) sheets = xls.sheet_names So, can anybody suggest a faster way to retrieve the sheet names from an Excel file

error: could not create '/Library/Python/2.7/site-packages/xlrd': Permission denied

╄→尐↘猪︶ㄣ 提交于 2019-11-28 17:11:54
I'm trying to install xlrd on mac 10.8.4 to be able to read excel files through python. I have followed the instructions on http://www.simplistix.co.uk/presentations/python-excel.pdf I did this: unzipped the folder to desktop in terminal, cd to the unzipped folder $ python setup.py install This is what I get: running install running build running build_py creating build creating build/lib creating build/lib/xlrd copying xlrd/__init__.py -> build/lib/xlrd copying xlrd/biffh.py -> build/lib/xlrd copying xlrd/book.py -> build/lib/xlrd copying xlrd/compdoc.py -> build/lib/xlrd copying xlrd