openpyxl

python操作excel----openpyxl模块

孤者浪人 提交于 2019-11-27 17:09:43
openpyxl模块支持.xls和.xlsx格式的excel创建,但是只支持.xlsx格式的读取操作,不支持.xls的读取(可以使用xlrd模块来读取,写入操作也可使用xlwt模块) openpyxl创建新的excel    1 import openpyxl 2 3 #创建工作簿 4 book=openpyxl.Workbook() 5 6 # 创建表 7 table1=book.create_sheet(title="联系电话",index=0)#title表名;index:自定义表位置 8 table2=book.create_sheet("工作经历",3) 9 10 #单元格值得插入(可以是具体值,也可以是excel函数语句) 11 table1.cell(1,1,"手机号")#参数为:行,列,数值----表中的行和列都是以索引1开始计数,因此在定位单元格时行列都必须不小于1 12 table1['B1']='年龄' 13 #单元格插入值的另外一种写法 14 for i in range(2,10): 15 table1.cell(i,1).value=12345 16 table1.cell(i,2).value=i 17 18 # 保存工作簿--可以指定xls或者xlsx 19 book.save("员工信息.xlsx")#参数:文件名

Read values from named ranges with openpyxl

醉酒当歌 提交于 2019-11-27 16:46:53
问题 How can I read values from named ranges in Excel with openpyxl? I've found the sourcecode at http://openpyxl.readthedocs.org/en/latest/_modules/openpyxl/workbook/names/named_range.html but I can't figure out how to use it properly. 回答1: wb = openpyxl.load_workbook(excel_file_name) rng = wb.get_named_range(name_of_range).destinations[0] sheet = rng[0] address = rng[1].replace('$','') val = sheet[address].value 来源: https://stackoverflow.com/questions/28896209/read-values-from-named-ranges-with

Openpyxl optimizing cells search speed

白昼怎懂夜的黑 提交于 2019-11-27 16:21:12
I need to search the Excel sheet for cells containing some pattern. It takes more time than I can handle. The most optimized code I could write is below. Since the data patterns are usually row after row so I use iter_rows(row_offset=x). Unfortunately the code below finds the given pattern an increasing number of times in each for loop (starting from milliseconds and getting up to almost a minute). What am I doing wrong? import openpyxl import datetime from openpyxl import Workbook wb = Workbook() ws = wb.active ws.title = "test_sheet" print("Generating quite big excel file") for i in range(1

Writing multi-line strings into cells using openpyxl

只谈情不闲聊 提交于 2019-11-27 15:47:11
问题 I'm trying to write data into a cell, which has multiple line breaks (I believe \n), the resulting .xlsx has line breaks removed. Is there a way to keep these line breaks? 回答1: In openpyxl you can set the wrap_text alignment property to wrap multi-line strings: from openpyxl import Workbook workbook = Workbook() worksheet = workbook.worksheets[0] worksheet.title = "Sheet1" worksheet.cell('A1').style.alignment.wrap_text = True worksheet.cell('A1').value = "Line 1\nLine 2\nLine 3" workbook.save

Openpyxl setting number format

拥有回忆 提交于 2019-11-27 15:37:30
问题 Could please someone show an example of applying the number format to the cell. For example, I need scientific format, form would be like '2.45E+05' but I couldn't figure a way how to do that in openpyxl. I tried in several ways but they are all reporting errors when saving the workbook. for example: import openpyxl as oxl wb = oxl.Workbook() ws = wb.create_sheet(title='testSheet') _cell = ws.cell('A1') _cell.style.number_format = '0.00E+00' or this (here I'm trying to use some of the

python: update dataframe to existing excel sheet without overwriting contents on the same sheet and other sheets

冷暖自知 提交于 2019-11-27 15:25:53
问题 Struggling for this for hours so I decided to ask for help from experts here: I want to modify existing excel sheet without overwriting content. I have other sheets in this excel file and I don't want to impact other sheets. I've created sample code, not sure how to add the second sheet that I want to keep though. t=pd.date_range('2004-01-31', freq='M', periods=4) first=pd.DataFrame({'A':[1,1,1,1], 'B':[2,2,2,2]}, index=t) first.index=first.index.strftime('%Y-%m-%d') writer=pd.ExcelWriter(

How to write to an existing excel file without breaking formulas with openpyxl?

前提是你 提交于 2019-11-27 14:48:20
When you write to an excel file from Python in the following manner: import pandas from openpyxl import load_workbook book = load_workbook('Masterfile.xlsx') writer = pandas.ExcelWriter('Masterfile.xlsx') writer.book = book writer.sheets = dict((ws.title, ws) for ws in book.worksheets) data_filtered.to_excel(writer, "Main", cols=['Diff1', 'Diff2']) writer.save() Formulas and links to charts which are in the existing sheets, will be saved as values. How to overwrite this behaviour in order to preserve formulas and links to charts? Openpyxl 1.7 contains several improvements for handling formulae

iterating over a range of rows using ws.iter_rows in the optimised reader of openpyxl

此生再无相见时 提交于 2019-11-27 14:47:12
I need to read an xlsx file of 10x5324 cells This is the gist of what i was trying to do: from openpyxl import load_workbook filename = 'file_path' wb = load_workbook(filename) ws = wb.get_sheet_by_name('LOG') col = {'Time':0 ...} for i in ws.columns[col['Time']][1:]: print i.value.hour The code was taking much too long to run then it should (I was performing operations, not printing) and after a while I got impatient and cancelled it. Any idea how I can work it in the optimized reader? I need to iterate over a range of rows, not over all rows. This is what i tried, but it's wrong: wb = load

Use openpyxl to edit a Excel2007 file (.xlsx) without changing its own styles?

 ̄綄美尐妖づ 提交于 2019-11-27 14:43:41
I have a .xlsx file to edit, I found openpyxl could manipulate Excel 2007 files. I only want to change the value in some cells and leave other settings unchanged. But after I went through the Documentation , I cannot find the examples to edit a existing file. It only demostrated reading a .xlsx file and writing to a new one. I tried below way to edit an existing file, but after I saved it, the styles in the file has been removed( like fonts, colors): from openpyxl.reader.excel import load_workbook wb=load_workbook(r'd:\foo1.xlsx') ws=wb.get_sheet_by_name('Bar') ws.cell('A1').value= 'new_value'

Insert image in openpyxl

坚强是说给别人听的谎言 提交于 2019-11-27 13:10:10
问题 Is it possible to insert an image (jpeg, png, etc) using openpyxl? Basically I want to place a generated image with a chart below it. I don't see anything in the documentation, which seems to be a little lacking compared to the maturity of the code. 回答1: The following inserts an image in cell A1. Adjust the image location to your needs or handle the creation of the PIL image yourself and hand that to Image() import openpyxl wb = openpyxl.Workbook() ws = wb.worksheets[0] img = openpyxl.drawing