openpyxl

Python openpyxl data_only=True returning None

假装没事ソ 提交于 2019-11-29 14:05:11
I have a simple excel file: A1 = 200 A2 = 300 A3 = =SUM(A1:A2) this file works in excel and shows proper value for SUM, but while using openpyxl module for python I cannot get value in data_only=True mode Python code from shell: wb = openpyxl.load_workbook('writeFormula.xlsx', data_only = True) sheet = wb.active sheet['A3'] <Cell Sheet.A3> # python response print(sheet['A3'].value) None # python response while: wb2 = openpyxl.load_workbook('writeFormula.xlsx') sheet2 = wb2.active sheet2['A3'].value '=SUM(A1:A2)' # python response any suggestions what am I doing worng? It depends upon the

Closing files in openpyxl

ⅰ亾dé卋堺 提交于 2019-11-29 13:45:06
Neither of these process, as would be expected reading the documentation: worksheet.close() workbook.close() Is there a way to close files once done in openpyxl? Or is it handled automatically when the program quits? I dont want to leave spreadsheets left hanging in memory. well you can take a look at the source code, Im currently using 1.5.5 as such def load_workbook(filename, use_iterators=False): if isinstance(filename, file): # fileobject must have been opened with 'rb' flag # it is required by zipfile if 'b' not in filename.mode: raise OpenModeError("File-object must be opened in binary

Python 操作 excel

◇◆丶佛笑我妖孽 提交于 2019-11-29 13:44:13
一、概述 操作 excel 是程序员经常要遇到的场景。因为产品、运营的数据都是以这种格式存储。所以,当程序员拿到这些数据肯定要解析,甚至需要把结果输出成 excel 文件。 下面就介绍如果用 Python 方面的读、写 excel 文件。 二、openpyxl A Python library to read/write Excel 2010 xlsx/xlsm files 借助 Python 的三方库 openpyxl ,让操作 excel 变得简单。 安装: pip install openpyxl 文档: 官方文档 示例代码: # coding=utf-8 from openpyxl import Workbook wb = Workbook() # 选择 sheet ws = wb.active # 设置值到某一个单元格(cells) ws['A1'] = 42 # Python 的数据类型可以自动转换 import datetime ws['A2'] = datetime.datetime.now() # 存储文件 wb.save("sample.xlsx") # 默认保存到当前目录下。文件名称为 sample.xlsx 读数据 from openpyxl import load_workbook wb = load_workbook('sample.xlsx') #

python3读取excel

懵懂的女人 提交于 2019-11-29 13:43:30
说明 2007版以前的Excel(xls结尾的),需要使用xlrd读,xlwt写。 2007版以后的Excel(xlsx结尾的),需要使用openpyxl来读写。 pypi的地址: https://pypi.python.org/pypi/xlwt https://pypi.python.org/pypi/xlrd https://pypi.python.org/pypi/openpyxl openpyxl文档地址: https://openpyxl.readthedocs.io/en/latest/changes.html 举个栗子 # -*- coding: utf-8 -*- # 读写2003 excel import xlrd import xlwt # 读写2007 excel import openpyxl def write03Excel(path): wb = xlwt.Workbook() sheet = wb.add_sheet("2003测试表") value = [["名称", "价格", "出版社", "语言"], ["如何高效读懂一本书", "22.3", "机械工业出版社", "中文"], ["暗时间", "32.4", "人民邮电出版社", "中文"], ["拆掉思维里的墙", "26.7", "机械工业出版社", "中文"]] for i in

python操作Excel的几种方式

穿精又带淫゛_ 提交于 2019-11-29 13:40:24
Python对Excel的读写主要有xlrd、xlwt、xlutils、openpyxl、xlsxwriter几种。 1.xlrd主要是用来读取excel文件 import xlrd workbook = xlrd.open_workbook(u'有趣装逼每日数据及趋势.xls') sheet_names= workbook.sheet_names() for sheet_name in sheet_names:    sheet2 = workbook.sheet_by_name(sheet_name)    print sheet_name rows = sheet2.row_values(3) # 获取第四行内容    cols = sheet2.col_values(1) # 获取第二列内容    print rows    print cols 2.xlwt主要是用来写excel文件 import xlwt wbk = xlwt.Workbook() sheet = wbk.add_sheet('sheet 1') sheet.write(0,1,'test text')#第0行第一列写入内容 wbk.save('test.xls') 3.xlutils结合xlrd可以达到修改excel文件目的 import xlrd from xlutils.copy import

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

python操作Excel的几种方式

给你一囗甜甜゛ 提交于 2019-11-29 13:38:53
Python对Excel的读写主要有xlrd、xlwt、xlutils、openpyxl、xlsxwriter几种。 1.xlrd主要是用来读取excel文件 import xlrd data = xlrd.open_workbook('abcd.xls') # 打开xls文件 table = data.sheets()[0] # 打开第一张表 nrows = table.nrows # 获取表的行数 for i in range(nrows): # 循环逐行打印 if i == 0:# 跳过第一行 continue print (table.row_values(i)[:13]) # 取前十三列   示例2: #coding=utf-8 ####################################################### #filename:test_xlrd.py #author:defias #date:xxxx-xx-xx #function:读excel文件中的数据 ####################################################### import xlrd #打开一个workbook workbook = xlrd.open_workbook('E:\\Code\\Python\\testdata

Applying borders to a cell in OpenPyxl

穿精又带淫゛_ 提交于 2019-11-29 12:42:02
问题 I am trying to use Openpyxl to apply a border to a cell, but I have failed on the most basic "apply any kind of border to any cell anywhere" task. I tried copying from the Openpyxl documentation (http://pythonhosted.org/openpyxl/styles.html#introduction) default style and modifying, but that gives me "TypeError: init () got an unexpected keyword argument 'superscript'" I tried copying straight out of another example here (Apply borders to all cells in a range with openpyxl), but that gives me

PermissionError [errno 13] when running openpyxl python script in Komodo

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 12:02:24
I am having trouble using openpyxl scripts in Komodo edit 9 and python 3.4 on Windows 7. I copied some openpyxl code to learn, but it won't execute from Komodo. I receive a permission error 13. I checked my path and python34 is present. The same script will run when I use IDLE or Command Prompt. My Komodo command is currently: %(python3) -u %F Any ideas on what may be causing this issue? The code and error are included below from openpyxl import Workbook from openpyxl.compat import range from openpyxl.cell import get_column_letter wb = Workbook() dest_filename = 'empty_book.xlsx' ws1 = wb

Openpyxl check for empty cell

风格不统一 提交于 2019-11-29 11:52:28
问题 openpyxl seems to be a great method for using Python to read Excel files, but I've run into a constant problem. I need to detect whether a cell is empty or not, but can't seem to compare any of the cell properties. I tried casting as a string and using "" but that didn't work. The type of cell when it is empty is None , or NoneType but I can't figure out how to compare an object to that. Suggestions? I understand openpyxl is under development, but maybe this is more a general Python problem.