openpyxl

openpyxl.load_workbook(file, data_only=True doens't work?

懵懂的女人 提交于 2019-12-04 01:53:09
问题 Why does x = "None" instead of "500"? I have tried everything that I know and searched 1 hour for answer... Thank you for any help! import openpyxl wb = openpyxl.Workbook() sheet = wb.active sheet["A1"] = 200 sheet["A2"] = 300 sheet["A3"] = "=SUM(A1+A2)" wb.save("writeFormula.xlsx") wbFormulas = openpyxl.load_workbook("writeFormula.xlsx") sheet = wbFormulas.active print(sheet["A3"].value) wbDataOnly = openpyxl.load_workbook("writeFormula.xlsx", data_only=True) sheet = wbDataOnly.active x =

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

╄→尐↘猪︶ㄣ 提交于 2019-12-04 00:37:27
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 Thank you! It seems that the library doesn't have a method to clear or set a range directly. So your

Openpyxl: How to copy a row after checking if a cell contains specific value

ぐ巨炮叔叔 提交于 2019-12-03 22:13:55
问题 I have a worksheet that is updated every week with thousands of rows and would need to transfer rows from this worksheet after filtering. I am using the current code to find the cells which has the value I need and then transfer the entire row to another sheet but after saving the file, I get the "IndexError: list index out of range" exception. The code I use is as follows: import openpyxl wb1 = openpyxl.load_workbook('file1.xlsx') wb2 = openpyxl.load_workbook('file2.xlsx') ws1 = wb1.active

How we can draw two series of data in “openpyxl” package of python (line-chart)

青春壹個敷衍的年華 提交于 2019-12-03 21:02:47
Suppose that we have this code: from openpyxl import Workbook wb = Workbook() ws = wb.active for row in range(1,10): value = ws.cell(row=row,column=1).value = row+5 for row in range(1,10): value2 = ws.cell(row=row,column=2).value = row wb.save("SampleChart.xlsx") from openpyxl.charts import Reference, Series,LineChart values = Reference(ws, (1, 1), (9, 1)) series = Series(values, title="First series of values") chart = LineChart() chart.append(series) chart.drawing.name = 'This is my chart' ws.add_chart(chart) wb.save("SampleChart.xlsx") How can i plot second column values to same line-chart?

Search a word in a text string in Excel sheet using openpyxl

谁说胖子不能爱 提交于 2019-12-03 20:09:17
I'm trying to search for a word in a cell that has a text string that looks like this (Energy;Green Buildings;High Performance Buildings). Here is the code I wrote, I get a syntax error for row in ws.iter_rows('D2:D11'): for cell in row: if 'Energy' in ws.cell.value : Print 'yes' Obviously, I don't want to print yes, this was to test the search function. Additionally, I want to get the cell location, and then tell openpyxl to assign a color to a cell in the same row under column E. here is a snap shot of my Excel sheet. I know how to assign a color using this command c.fill = PatternFill(start

使用openpyxl读取excel

梦想与她 提交于 2019-12-03 20:07:50
读取excel import openpyxl workbook = openpyxl.load_workbook("test.xlsx") #通过文件名得到文件对象 sheet_name = workbook.get_sheet_by_name("Sheet1") #通过名称得到工作簿对象 # rows_sheet = sheet_name.rows #按行生成工作表中所有单元格对象,生成器类型 rows = [item.value for item in list(sheet_name.rows)[1]] print(rows) #第二行的内容 cols = [item.value for item in list(sheet_name.columns)[1]] print(cols) #第二列的内容 rows_sheet = sheet_name.iter_rows() for item in rows_sheet: for call in item: print(call.coordinate, call.value) #遍历所有内容 cell_1_2 = sheet_name.cell(row=1,column = 2).value print(cell_1_2) #查看第一行第二列的单元格内容 print(sheet_name.max_row,sheet_name

Python: openpyxl change font to bold

一世执手 提交于 2019-12-03 20:07:49
问题 I'm using Python version 3.6 and the latest version of the openxlpy module (v2.4.8) on Windows. I want to change certain font to bold in a cell, but I don't want all the text contained in the cell to be bold. In short, I'm saving data to a new Excel workbook that I've created using openxlpy. I'm saving multiple lines of data in one cell. I only want the first line of each cell to be bold. I've searched everywhere in the openpyxl documentation and online but I can't find anything. It appears

Python (openpyxl) : Put data from one excel file to another (template file) & save it with another name while retaining the template

浪子不回头ぞ 提交于 2019-12-03 16:42:02
I have a template excel file named as template.xlsx which has a number of sheets. I would like to copy data from a seperate .csv file into the first sheet of template.xlsx (named as data ) and save the new file as result.xlsx while retaining the original template file. I want to paste the data starting from the second row in the data sheet of template.xlsx This is the code I have developed so far import pandas as pd from openpyxl.utils.dataframe import dataframe_to_rows import openpyxl from shutil import copyfile template_file = 'template.xlsx' # Has a header in row 1 already which needs to be

Calculating Excel sheets without opening them (openpyxl or xlwt)

白昼怎懂夜的黑 提交于 2019-12-03 16:02:49
I made a script that opens a xls. file, writes a few new value's in it, then save the file. Later the script opens it again, and wants to find the answers in some cells which contain formulas. If I call that cell with openpyxl, I get the formula (ie: =A1*B1). And if I activate data_only, i get nothing. Is there a way to let python calculate the xls file? (or should I try PyXll?) There is actually a project that takes Excel formulas and evaluates them using Python: Pycel . Pycel uses Excel itself (via COM) to extract the formulas, so in your case you would skip that part. The project probably

openpyxl 模块 读写Excel

∥☆過路亽.° 提交于 2019-12-03 15:19:56
import openpyxl#写到execl中def write_execl(): book=openpyxl.Workbook() sheet=book.active #获取默认sheet # sheet2=book.get_sheet_by_name("sheet2") #获取指定sheet2 sheet.append(["id","name","password"]) sheet["a1"]="id" # a行 1列 "id" sheet["b1"]="name" # b行 1列 "id" sheet.cell(3,1,1) #指定行、列 写入数据 book.save("book.xlsx") #希望使用.xlsx格式#读execl (比较麻烦,希望使用xlwt) 使用load_workbook方法def read(): book=openpyxl.load_workbook("book.xlsx") sheet=book.active print(sheet["a1"].value)#获取a行1列数据 print(sheet[1:10]) #获取第几行到第几行 print(sheet.cell(1,1).value)#获取第一行一列数据#删除sheet表def del_row_col(): book = openpyxl.load_workbook("book.xlsx"