openpyxl

Getting AttributeError 'Workbook' object has no attribute 'add_worksheet' - while writing data frame to excel sheet

社会主义新天地 提交于 2020-01-02 10:05:38
问题 I have following code and trying to write a data frame into an "Existing" worksheet of an Excel file (referred here as test.xlsx). Sheet3 is the targeted sheet, where I want to place the data and I don't want to replace the entire sheet with a new one. df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]}) book = load_workbook('test.xlsx') writer = pd.ExcelWriter('test.xlsx') writer.book = book writer.sheets = dict((ws.title, ws) for ws in book.worksheets) # *I am not sure what is

openpyxl 2.4.2: cell value generated by formula empty after saving

非 Y 不嫁゛ 提交于 2020-01-02 06:24:09
问题 I use openpyxl to open a file, edit some cells and save the changes. Here's an example: import openpyxl book = openpyxl.load_workbook(sheet_path) sheet = book.active for row in range(sheet.max_row): index = row + 1 sheet.cell(row=index, column=1).value = "something" book.save(sheet_path) The problem is, when I save the file, other cells are modified. In fact, the cells in my sheet that contains formulas are "corrupted", the file size is greatly reduced and when I use other scripts to read the

How do I add a column to an existing excel file using python?

僤鯓⒐⒋嵵緔 提交于 2020-01-02 04:41:12
问题 here is my code: import openpyxl, pprint wb = openpyxl.load_workbook('/Users/sarahporgess/Desktop/SSA1.xlsx') sheet = wb.get_sheet_by_name('Sheet1') data = {} for row in range(1,sheet.max_row+1): date = sheet['A' +str(row)].value gamma = sheet['B' +str(row)].value theta = sheet['C' +str(row)].value ratio = float(gamma)/float(theta) resultFile = open('SSA2.csv' , 'w') resultFile.write( pprint.pformat(date)) resultFile.write( pprint.pformat(gamma)) resultFile.write( pprint.pformat(theta))

python : Get Active Sheet in xlrd? and help for reading and validating excel file in Python

泄露秘密 提交于 2020-01-01 19:37:09
问题 2 Questions to ask: Ques 1: I just started studying about xlrd for reading excel file in python. I was wondering if there is a method in xlsrd --> similar to get_active_sheet() in openpyxl or any other way to get the Active sheet ? get_active_sheet() works this in openpyxl import openpyxl wb = openpyxl.load_workbook('example.xlsx') active_sheet = wb.get_active_sheet() output : Worksheet "Sheet1" I had found methods in xlrd for retrieving the names of sheets, but none of them could tell me the

How to clear a range of values in an Excel workbook using OpenPyXl

删除回忆录丶 提交于 2020-01-01 08:35:12
问题 I have a workbook that I would like to clear a range of values with using OpenPyXI. So far I have the following: # Import OpenPyXl module. from openpyxl import load_workbook # Load workbook. wb = load_workbook(filename = 'testing.xlsx') # Make a variable with a worksheet you want to view/modify. sheet = wb['AR Cutoff'] # Change value of A3. sheet['A3'] = 'Assigned value' In short, I am trying to do in OpenPyXL the same thing the following VBA does: Worksheets("Sheet1").Range("A1:G37").Clear

how to read the xlsx color infomation by using openpyxl

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 06:00:11
问题 I am using openpyxl to read excel file. For my application, i need to read the background color of the cell in the xlsx file But i cant find how to load those color info. I tried to use cell.style.fill.color.index, but it only return FFFFFFFF as background which is not correct to the file i read. Does openpyxl support reading color format? 回答1: UPDATE (2014): I updated openpyxl to v. 2.2. They seem to have resolved the problem that I documented in my original answer (see below). I am now able

How to set up the Graphical Properties of Chart Area using openpyxl

拥有回忆 提交于 2019-12-31 04:41:11
问题 I would like to change the background color of Chart_area as well as remove all borders. Regarding the background color, I have tried the solution suggested: How to set Background Color of Plot Area of Chart using openpyxl but this solution doesn't work for any charts i.e. BarChart, AreaChart, LineChart, ScatterChart, RadarChart etc... I tried two openpyxl versions: 2.4.7, 2.4.9. without success. # setup the chart chart = LineChart() # test to remove border line ******************************

Converting txt to xlsx while setting the cell property for number cells as number

自古美人都是妖i 提交于 2019-12-31 04:29:06
问题 Related question: Error in converting txt to xlsx using python I have the following code which I revised thanks you Anand S Kumar. import csv import openpyxl import sys def convert(input_path, output_path): """ Read a csv file (with no quoting), and save its contents in an excel file. """ wb = openpyxl.Workbook() ws = wb.worksheets[0] with open(input_path) as f: reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE) for row_index, row in enumerate(reader, 1): for col_index, value in

如何快速将合并居中的单行文本变为多行以匹配不同内容

久未见 提交于 2019-12-30 01:03:10
如何快速将合并居中的单行文本变为多行以匹配不同内容 **问题:**在Excel中将合并居中的单行文本变为多行以匹配不同内容,如下图,如果数量较少,很容易处理,如果数量很多,处理起来相对麻烦,本文尝试用python来解决。 **分析:**最关键的在于将左侧三列赋值与右侧产品相同的行数。 1、原文件 2、最终文件 3、用python实现过程 import openpyxl #将名单和产品名称分别放入两个excel,并分别读取 wb1 = openpyxl . load_workbook ( '业务经理名单.xlsx' ) sheet1 = wb1 [ 'Sheet1' ] wb2 = openpyxl . load_workbook ( '产品.xlsx' ) sheet2 = wb2 [ 'Sheet1' ] name_list = [ ] #创建空列表存放数据 for r1 in range ( 1 , sheet1 . max_row + 1 ) : #遍历文本 if r1 == 1 : name_list . append ( [ sheet1 . cell ( row = r1 , column = 1 ) . value , sheet1 . cell ( row = r1 , column = 2 ) . value , sheet1 . cell ( row = r1

openpyxl库之文档保护学习(摘录自官方文档)

谁说胖子不能爱 提交于 2019-12-29 11:47:43
保护 警告 保护工作簿或工作表的密码仅提供了相当基本的安全性。数据未加密,因此可以使用任意数量的免费工具进行修改。实际上,该规范指出:“工作表或工作簿元素保护不应与文件安全​​性混淆。这旨在使您的工作簿免受意外修改的影响,并且不能保护其免受恶意修改。” Openpyxl支持保护工作簿和工作表不被修改。除非明确配置了其他算法,否则Open XML“传统密码哈希算法”用于生成哈希密码值。 工作簿保护 为防止其他用户查看隐藏的工作表,添加,移动,删除或隐藏工作表以及重命名工作表,可以使用密码保护工作簿的结构。可以使用openpyxl.workbook.protection.WorkbookProtection.workbookPassword()属性设置密码 wb.security.workbookPassword = ‘…’ wb.security.lockStructure = True 同样,可以通过设置另一个密码来防止从共享工作簿中删除更改跟踪和更改历史记录。可以使用openpyxl.workbook.protection.WorkbookProtection.revisionsPassword()属性设置此密码 wb.security.revisionsPassword = ‘…’ openpyxl.workbook.protection