xlsxwriter: protection unhiding - python

UPDATE: Fixed. Sheet protected, selected column is possible to unhide.
Is it possible to lock all visible cells (protection from editing), but still allow the users to unhide certain columns?
I would like to export a pandas dataframe via pd.excel_writer().
worksheet.protect() doesn't allow for any arguments?
I apply column-wise formatting and tried 'lock': False on the hidden columns, but that didn't work.
# init
import pandas as pd
import random as rd
import string
random.seed(10)
S = 10
string_col = []
number_col1 = [] # protect
number_col2 = [] # hide
# create testdata
for i in range(0, 20):
ran_str = ''.join(rd.choices(string.ascii_uppercase + string.digits, k = S))
ran_num1 = ''.join(str(rd.randrange(S)))
ran_num2 = ''.join(str(rd.randrange(S)))
string_col.append(ran_str)
number_col1.append(ran_num1)
number_col2.append(ran_num2)
testframe = pd.DataFrame(
{'String_col': string_col,
'Hide': number_col1,
'Protect': number_col2
})
# helperfunction for selecting columns
def getColnameByPosition(pos):
from string import ascii_uppercase
colnames = list(ascii_uppercase)
for col in ascii_uppercase:
for col2 in ascii_uppercase:
colnames.append(col+col2)
return colnames[pos]
# export
writer = pd.ExcelWriter("./FormatTest.xlsx", engine='xlsxwriter')
testframe.to_excel(writer, sheet_name="Sheet0", startrow=0, header=True, index=False)
workbook = writer.book
worksheet = writer.sheets["Sheet0"]
# protect and hide format
format_protect = workbook.add_format({'bg_color': '#ADD8E6','font_name': 'Calibri', 'font_size': 9, 'num_format': '#,##0', "locked": True})
format_hide = workbook.add_format({'bg_color': '#FFFFFF', 'font_name': 'Calibri', 'font_size': 9, 'num_format': '#,##0', "locked": False})
prot_col = getColnameByPosition(testframe.columns.get_loc("Protect"))
hide_col = getColnameByPosition(testframe.columns.get_loc("Hide"))
worksheet.set_column(prot_col+':'+prot_col, 10, format_protect)
worksheet.set_column(hide_col+':'+hide_col, 10, format_hide, {'hidden': True})
# FIX: allow for unhiding.
worksheet.protect('', {'format_columns': True})
writer.save()

You just need to set protect() to turn on worksheet protection but turn on the exceptions that you want. In this case turning on "Format Columns" should get you what you want:
# ...
worksheet.set_column(prot_col+':'+prot_col, 10, format_protect)
worksheet.set_column(hide_col+':'+hide_col, 10, format_hide, {'hidden': True})
worksheet.protect('', {'format_columns': True})
writer.save()
Also, set_column() can take (col, col) notation so you could drop the getColnameByPosition() code and just do this:
# ...
prot_col = testframe.columns.get_loc("Protect")
hide_col = testframe.columns.get_loc("Hide")
worksheet.set_column(prot_col, prot_col, 10, format_protect)
worksheet.set_column(hide_col, hide_col, 10, format_hide, {'hidden': True})
worksheet.protect('', {'format_columns': True})
writer.save()

Related

Pandas change background column color

im tryna change the color background for the column names, like in this script "case its changing the row1 column1 color, but the problem its that its changing also the column name for all the sheets in "False"
workbook = xlwriter.book
cell_format = workbook.add_format({
'bold': True,
'fg_color': '#00FFFF',
'border': 1})
ws.write(0,0,False,cell_format)
ANEX: ENTIRE SCRIPT
import pandas as pd
from timestampdirectory import createdir
import openpyxl
import xlsxwriter
import os
import time
def svnanalysis():
dest = createdir()
dfSvnUsers = pd.read_excel(os.path.join(dest, "SvnUsers.xlsx"))
dfSvnUsers.fillna("N/A", inplace=True)
dfSvnGroupMembership = pd.read_excel(os.path.join(dest, "SvnGroupMembership.xlsx"))
dfSvnRepoGroupAccess = pd.read_excel(os.path.join(dest, "SvnRepoGroupAccess.xlsx"))
dfsvnReposSize = pd.read_excel(os.path.join(dest, "svnReposSize.xlsx"))
dfsvnRepoLastChangeDate = pd.read_excel(os.path.join(dest, "svnRepoLastChangeDate.xlsx"))
dfUserDetails = pd.read_excel(r"D:\GIT-files\Automate-Stats\SVN_sample_files\CM_UsersDetails.xlsx")
dfUserDetails.fillna("N/A", inplace=True)
timestr = time.strftime("%Y-%m-%d-")
xlwriter = pd.ExcelWriter(os.path.join(dest, f'{timestr}Usage-SvnAnalysis.xlsx'))
dfUserDetails.to_excel(xlwriter, sheet_name='UserDetails', index=False)
dfSvnUsers.to_excel(xlwriter, sheet_name='SvnUsers', index=False)
dfSvnGroupMembership.to_excel(xlwriter, sheet_name='SvnGroupMembership', index=False)
dfSvnRepoGroupAccess.to_excel(xlwriter, sheet_name='SvnRepoGroupAccess', index=False)
dfsvnReposSize.to_excel(xlwriter, sheet_name='svnReposSize', index=False)
dfsvnRepoLastChangeDate.to_excel(xlwriter, sheet_name='svnRepoLastChangeDate', index=False)
for column in dfSvnUsers:
column_width = max(dfSvnUsers[column].astype(str).map(len).max(), len(column))
col_idx = dfSvnUsers.columns.get_loc(column)
xlwriter.sheets['SvnUsers'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['UserDetails'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['SvnGroupMembership'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['SvnRepoGroupAccess'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['svnReposSize'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['svnRepoLastChangeDate'].set_column(col_idx, col_idx, column_width)
for sheet_name in xlwriter.sheets:
ws = xlwriter.sheets[sheet_name]
ws.freeze_panes(1, 0)
#workbook = xlwriter.book
#cell_format = workbook.add_format({
# 'bold': True,
# 'fg_color': '#00FFFF',
# 'border': 1})
#ws.write(0,0,False,cell_format)
#for col_num, value in enumerate(df.columns.values):
#worksheet.write(0, col_num + 1, value, header_format)
#cell_format.set_pattern(1) # This is optional when using a solid fill.
#cell_format.set_bg_color('blue')
#ws.write(0, 0,'test',cell_format)
xlwriter.close()
This is the full script that i use to add more xlsx files as sheets on at only 1 excel

Pandas change font color based on condition in Excel and save to the same excel python

New to Pandas as of now.
My Problem statement is I am trying to open an existing excel sheet,
Traverse through the values present in that,
add an if condition and change the font colour of text if the condition is true.
This is the sample excel where I am trying to change the color:
Below is my code which I have tried:
def highlight_cells(val):
color = 'red' if val =='DATA' else '#C6E2E9'
return 'color: %s' % color
ddf = pd.read_excel(PathToTheExcelFile)
ddf.style.applymap(highlight_cells)
ddf.to_excel(PathToTheExcelFile,index=False)
What I am currently getting is this:
What I want is this:
.style.applymap produces a Styler object, which has a to_excel method to conveniently export it:
def highlight_cells(val):
color = 'red' if val == 'DATA' else '#C6E2E9'
return 'color: %s' % color
ddf.style.applymap(highlight_cells).to_excel("data.xlsx", index=False)
# If you want to stylize only the Comments column
ddf.style.applymap(highlight_cells, subset="Comments").to_excel("data.xlsx", index=False)
Result:
The style.applymap is for showing output of dataframes in HTML, not updating excel sheets. You can change the code thus to update the font in excel. I am reading the excel input.xlsx, updating the contents using openpyxl and writing it to output.xlsx. You can change other things like size, bold, fontname, etc. as well. Note: Color used is HEX color, but without the # symbol in front
import openpyxl
wb = openpyxl.load_workbook(filename="input.xlsx")
ws = wb.active
for row in range(2,ws.max_row+1): #Skipping first row as I assume it is header
if ws.cell(row=row, column=3).value == 'DATA':
ws.cell(row=row, column=3).font = openpyxl.styles.Font(color='FF0000') #, size=16, bold=True, name='Calibri')
else:
ws.cell(row=row, column=3).font = openpyxl.styles.Font(color='C6E2E9')
wb.save("output.xlsx")
USING pandas.ExcelWriter instead of openpyxl
You can use the below code pandas.ExcelWriter to change the font to RED for DATA and GREEN for others. Note: you can edit the colors to anything you want using # followed by the 6 char hexcode in case you want to change the font color
import pandas as pd
import numpy as np
df = pd.read_excel('output.xlsx')
df.fillna('NA', inplace = True)
writer = pd.ExcelWriter('output1.xlsx')
df.to_excel(writer, sheet_name= 'sheet1', index=False)
worksheet = writer.sheets['sheet1']
workbook = writer.book
cell_format_red = workbook.add_format({'font_color': 'red'})
cell_format_green = workbook.add_format({'font_color': 'green'})
start_row = 1
start_col = 2
end_row = len(df)
end_col = start_col
worksheet.conditional_format(start_row, start_col, end_row, end_col, {'type': 'cell', 'criteria': '==', 'value': '"DATA"', 'format': cell_format_red})
worksheet.conditional_format(start_row, start_col, end_row, end_col, {'type': 'cell', 'criteria': '!=', 'value': '"DATA"', 'format': cell_format_green})
writer.save()

How to insert a real pivot table in a excel sheet with python?

I want to create a "real" pivot table in excel sheet with python without using the function of pandas (pandas.pivot_table).
Is there a method?
I need to create the "true" pivot table in excel with a dropdown combo box. Unfortunately the pandas pivot is static (is a table) and not dynamic.
Can anyone help me?
Can you try it this way?
import win32com.client
Excel = win32com.client.gencache.EnsureDispatch('Excel.Application') # Excel = win32com.client.Dispatch('Excel.Application')
win32c = win32com.client.constants
wb = Excel.Workbooks.Add()
Sheet1 = wb.Worksheets("Sheet1")
TestData = [['Country','Name','Gender','Sign','Amount'],
['CH','Max' ,'M','Plus',123.4567],
['CH','Max' ,'M','Minus',-23.4567],
['CH','Max' ,'M','Plus',12.2314],
['CH','Max' ,'M','Minus',-2.2314],
['CH','Sam' ,'M','Plus',453.7685],
['CH','Sam' ,'M','Minus',-53.7685],
['CH','Sara','F','Plus',777.666],
['CH','Sara','F','Minus',-77.666],
['DE','Hans','M','Plus',345.088],
['DE','Hans','M','Minus',-45.088],
['DE','Paul','M','Plus',222.455],
['DE','Paul','M','Minus',-22.455]]
for i, TestDataRow in enumerate(TestData):
for j, TestDataItem in enumerate(TestDataRow):
Sheet1.Cells(i+2,j+4).Value = TestDataItem
cl1 = Sheet1.Cells(2,4)
cl2 = Sheet1.Cells(2+len(TestData)-1,4+len(TestData[0])-1)
PivotSourceRange = Sheet1.Range(cl1,cl2)
PivotSourceRange.Select()
Sheet2 = wb.Worksheets(2)
cl3=Sheet2.Cells(4,1)
PivotTargetRange= Sheet2.Range(cl3,cl3)
PivotTableName = 'ReportPivotTable'
PivotCache = wb.PivotCaches().Create(SourceType=win32c.xlDatabase, SourceData=PivotSourceRange, Version=win32c.xlPivotTableVersion14)
PivotTable = PivotCache.CreatePivotTable(TableDestination=PivotTargetRange, TableName=PivotTableName, DefaultVersion=win32c.xlPivotTableVersion14)
PivotTable.PivotFields('Name').Orientation = win32c.xlRowField
PivotTable.PivotFields('Name').Position = 1
PivotTable.PivotFields('Gender').Orientation = win32c.xlPageField
PivotTable.PivotFields('Gender').Position = 1
PivotTable.PivotFields('Gender').CurrentPage = 'M'
PivotTable.PivotFields('Country').Orientation = win32c.xlColumnField
PivotTable.PivotFields('Country').Position = 1
PivotTable.PivotFields('Country').Subtotals = [False, False, False, False, False, False, False, False, False, False, False, False]
PivotTable.PivotFields('Sign').Orientation = win32c.xlColumnField
PivotTable.PivotFields('Sign').Position = 2
DataField = PivotTable.AddDataField(PivotTable.PivotFields('Amount'))
DataField.NumberFormat = '#\'##0.00'
Excel.Visible = 1
wb.SaveAs('ranges_and_offsets.xlsx')
Excel.Application.Quit()
https://www.yinglinglow.com/blog/2018/04/29/xlwings-pivot-table
Here is another way using xlwings.
import xlwings as xw
from xlwings import constants
# create instance of Excel app
app = xw.App(visible=True)
# create workbook object
wb = app.books.open('my_file') # where my_file is in .xlsx format
# create sheet object
sht = wb.sheets['my_sheet_name'] # where my_sheet_name is name of your Excel sheet name that contains your data
# determine last row and column of your data
num_col = sht.range('A1').end('right').column
num_row = sht.range('A1').end('down').row
# define your pivot table data range
PivotSourceRange = sht.range((1,1), (num_row, num_col))
# add a pivot sheet to your Excel file
xw.sheets.add(name='pivot', before='my_sheet_name')
# name your pivot table
PivotTableName = 'MyPivotTable'
pivot_tab_name = '' + 'pivot' + '!R1C1'
# create pivot table cache
PivotCache = wb.api.PivotCaches().Create(SourceType=constants.PivotTableSourceType.xlDatabase,
SourceData=PivotSourceRange.api,
Version=constants.PivotTableVersionList.xlPivotTableVersion14)
PivotTable = PivotCache.CreatePivotTable(TableDestination=pivot_tab_name,
TableName=PivotTableName,
DefaultVersion=constants.PivotTableVersionList.xlPivotTableVersion14)
# structure the pivot table
PivotTable.PivotFields('your_row_label').Orientation = constants.PivotFieldOrientation.xlRowField
PivotTable.PivotFields('your_page_label').Orientation = constants.PivotFieldOrientation.xlPageField
DataField = PivotTable.AddDataField(PivotTable.PivotFields('the_column_you_want_to_tabulate'))
DataField.Function = constants.TotalsCalculation.xlTotalsCalculationAverage
# other available calculation methods include:
# .xlTotalsCalculationCountNums
# .xlTotalsCalculationMax
# .xlTotalsCalculationMin
# .xlTotalsCalculationNone
# .xlTotalsCalculationCount
# .xlTotalsCalculationCustom
# .xlTotalsCalculationStdDev
# .xlTotalsCalculationSum
# .xlTotalsCalculationVar
# you specify the formatting of your numbers with this statement
DataField.NumberFormat = '0.0' # which uses standard Excel formats
# finish building the pivot table
PivotTable.PivotFields('your_page_label').EnableMultiplePageItems = True
PivotTable.PivotFields('your_page_label').PivotItems().Visible = True
End result is something like this:

Python: Pandas style background color not visible in excel file

I am creating an excel file with multiple sheets using xlsxwriter as engine.
In each sheet the row color is based on value of column named colour
But the color is not visible in my excel file.
import pandas as pd
def row_colour(row):
return ['background-color:'+row.colour.lower()for i in row]
writer = pd.ExcelWriter('try.xlsx', engine='xlsxwriter')
cols = ['subject','colour']
df1 = pd.DataFrame([['Math','DarkRed'],['Science','Yellow']],columns=cols)
df2 = pd.DataFrame([['English','Orange'],['History','Green']],columns=cols)
df3 = pd.DataFrame([['Geography','DarkRed'],['Civic','Yellow']],columns=cols)
df1.style.apply(row_colour,axis=1)
df2.style.apply(row_colour,axis=1)
df3.style.apply(row_colour,axis=1)
df1.to_excel(writer, sheet_name='Sheet 1')
df2.to_excel(writer, sheet_name='Sheet 2')
df3.to_excel(writer, sheet_name='Sheet 3')
writer.save()
In output no color is visible:
The accepted answer is right for the above question.
I have improved the task by deleting the color column since it's only use was to color the rows.
Code for it:
import pandas as pd
def row_colour(table,color):
print("table: \n "+str(table))
print("table shape : "+str(table.shape))
color_data = []
for index,row in table.iterrows():
color.iloc[index]
if str(color.iloc[index]['colour']) == "DarkRed":
c= 'background-color:red'
else:
c= 'background-color:'+str(color.iloc[index]['colour'])
color_data.append([c for i in range(len(row))])
return pd.DataFrame(color_data,index=table.index, columns=table.columns)
writer = pd.ExcelWriter('try.xlsx', engine='xlsxwriter')
cols = ['subject','colour']
df1 = pd.DataFrame([['Math','DarkRed'],['Science','Yellow']],columns=cols)
df2 = pd.DataFrame([['English','Orange'],['History','Green']],columns=cols)
df3 = pd.DataFrame([['Geography','DarkRed'],['Civic','Yellow']],columns=cols)
color = pd.DataFrame(columns=['colour'])
color['colour']=df1['colour']
df1 = df1.drop(['colour'],axis=1)
df1=df1.style.apply(row_colour,axis=None,color=color)
color = pd.DataFrame(columns=['colour'])
color['colour']=df2['colour']
df2=df2.drop(['colour'],axis=1)
df2=df2.style.apply(row_colour,axis=None,color=color)
color = pd.DataFrame(columns=['colour'])
color['colour']=df3['colour']
df3=df3.drop(['colour'],axis=1)
df3=df3.style.apply(row_colour,axis=None,color=color)
df1.to_excel(writer, sheet_name='Sheet 1')
df2.to_excel(writer, sheet_name='Sheet 2')
df3.to_excel(writer, sheet_name='Sheet 3')
writer.save()
The function is ok, you just have to reassign df1, df2, df3. This should work:
import pandas as pd
def row_colour(row):
return ['background-color:'+row.colour.lower()for i in row]
writer = pd.ExcelWriter('try.xlsx', engine='xlsxwriter')
cols = ['subject','colour']
df1 = pd.DataFrame([['Math','DarkRed'],['Science','Yellow']],columns=cols)
df2 = pd.DataFrame([['English','Orange'],['History','Green']],columns=cols)
df3 = pd.DataFrame([['Geography','DarkRed'],['Civic','Yellow']],columns=cols)
df1 = df1.style.apply(row_colour,axis=1)
df2 = df2.style.apply(row_colour,axis=1)
df3 = df3.style.apply(row_colour,axis=1)
df1.to_excel(writer, sheet_name='Sheet 1')
df2.to_excel(writer, sheet_name='Sheet 2')
df3.to_excel(writer, sheet_name='Sheet 3')
writer.save()
to_excel here is a method of pandas.io.formats.style.Styler rather than the original dataframe.
As an answer to your comment, I came up with a more complex solution.
The colours are now read from the DataFrame before being dropped. Then passed as an argument to a row-colouring function.
The key points are my use of zip and pd.IndexSlice for subsetting df.style.apply. I hope this suits your colouring needs.
import pandas as pd
def colour_row(row, colour):
return ['background-color:'+ colour.lower() for i in row]
def colour_df(df, colour_col):
colours = list(df['colour'])
df = df.drop('colour', axis = 1)
coloured_df = df.style
for i, colour in zip(range(len(df)), colours):
coloured_df = coloured_df.apply(colour_row, axis=1, subset=pd.IndexSlice[i,:], colour=colour)
return coloured_df
writer = pd.ExcelWriter('try.xlsx', engine='xlsxwriter')
cols = ['subject','colour']
df1 = pd.DataFrame([['Math','DarkRed'],['Science','Yellow']],columns=cols)
df2 = pd.DataFrame([['English','Orange'],['History','Green']],columns=cols)
df3 = pd.DataFrame([['Geography','DarkRed'],['Civic','Yellow']],columns=cols)
sheet_num = 1
for df in [df1, df2, df3]:
sheet_name = 'Sheet ' + str(sheet_num)
df = colour_df(df, 'colour')
df.to_excel(writer, sheet_name = sheet_name)
sheet_num += 1
writer.save()

IOError: [Errno 13] Permission denied: 'Test.xlsx'

the goal of my program is to read the sheet SNR_COPY1 from the Test.xlsx file, using the data to do some computations and then write those to a new sheet within Test.xlsx... Now yesterday my code worked perfectly, but then when I reran it, I got the above mentioned error.. Both the python script and the xlsx file are placed within Documents (Not sure how much it matters, but I'm working on Windows). My code:
import numpy as np
from numpy import pi, r_
import matplotlib.pyplot as plt
from scipy import optimize
from scipy.optimize import curve_fit
import pandas as pd
from pandas import DataFrame
import copy
#version in which all data will be read from the excelfile. The resulting fitparameters will be written into the excisting excelfile.
def snr_fitting_excel(number_of_altitude_points, row_begin_position_of_data, number_of_wavelengths):
# (0,0) in python = (2,1) in excel
n_alt = number_of_altitude_points
n_lambda = number_of_wavelengths
begin = row_begin_position_of_data
end = begin + n_alt
xlsx = pd.ExcelFile('Test.xlsx')
df = pd.read_excel(xlsx, 'SNR-COPY1')
xlsx_copy = copy.deepcopy(xlsx)
#print the beginning point of your data. This ensures that you are working at the correct position in the excel file
print(df.iat[11,2])
d = (n_alt, n_lambda)
#each row of height will represent the data for a given alltitude (height[0] = 5km data,...)
height = np.zeros(d, dtype=int)
# print(height)
for j in range(begin, end):
for i in range(2,10):
height[j-begin][i-2] = (np.around(df.iat[j,i], decimals =0))
height = np.array(height)
#array with the different wavelengths at which the data was taken
wavelengths = np.array([400, 450, 500, 550, 600, 650, 700, 800])
parameter_values = []
#fit the points with the desired function from above
for i in range(0, len(height)):
popt, pcov = curve_fit(fitfunc_polynome_OP_BL, wavelengths, height[i])
fig = plt.figure()
plt.plot(wavelengths, height[i], 'o')
plt.plot(wavelengths, fitfunc_polynome_OP_BL(wavelengths, *popt), 'r-', label ='fit: a=%5.3f, b=%5.3f, c=%5.3f, d=%5.3f, e=%5.3f, g=%5.3f, h=%5.3f, i=%5.3f' % tuple(popt))
plt.xlabel("Wavelength (nm)")
plt.ylabel("SNR")
plt.title("OP-BL-SNR fitting without data cut-off alltitude: " + str((i+1)*5) + "km")
fig.savefig("snr_op_fit_bl" + str((i+1)*5) +".pdf")
parameter_values.append(popt)
print(str((i+1)*5))
print(popt)
parameter_values_to_export = {'a': [parameter_values[0][0], parameter_values[1][0], parameter_values[2][0], parameter_values[3][0], parameter_values[4][0], parameter_values[5][0], parameter_values[6][0], parameter_values[7][0], parameter_values[8][0], parameter_values[9][0], parameter_values[10][0], parameter_values[11][0], parameter_values[12][0], parameter_values[13][0], parameter_values[14][0], parameter_values[15][0], parameter_values[16][0], parameter_values[17][0]],
'b': [parameter_values[0][1], parameter_values[1][1], parameter_values[2][1], parameter_values[3][1], parameter_values[4][1], parameter_values[5][1], parameter_values[6][1], parameter_values[7][1], parameter_values[8][1], parameter_values[9][1], parameter_values[10][1], parameter_values[11][1], parameter_values[12][1], parameter_values[13][1], parameter_values[14][1], parameter_values[15][1], parameter_values[16][1], parameter_values[17][1]],
'c': [parameter_values[0][2], parameter_values[1][2], parameter_values[2][2], parameter_values[3][2], parameter_values[4][2], parameter_values[5][2], parameter_values[6][2], parameter_values[7][2], parameter_values[8][2], parameter_values[9][2], parameter_values[10][2], parameter_values[11][2], parameter_values[12][2], parameter_values[13][2], parameter_values[14][2], parameter_values[15][2], parameter_values[16][2], parameter_values[17][2]],
'd': [parameter_values[0][3], parameter_values[1][3], parameter_values[2][3], parameter_values[3][3], parameter_values[4][3], parameter_values[5][3], parameter_values[6][3], parameter_values[7][3], parameter_values[8][3], parameter_values[9][3], parameter_values[10][3], parameter_values[11][3], parameter_values[12][3], parameter_values[13][3], parameter_values[14][3], parameter_values[15][3], parameter_values[16][3], parameter_values[17][3]],
'e': [parameter_values[0][4], parameter_values[1][4], parameter_values[2][4], parameter_values[3][4], parameter_values[4][4], parameter_values[5][4], parameter_values[6][4], parameter_values[7][4], parameter_values[8][4], parameter_values[9][4], parameter_values[10][4], parameter_values[11][4], parameter_values[12][4], parameter_values[13][4], parameter_values[14][4], parameter_values[15][4], parameter_values[16][4], parameter_values[17][4]],
'g': [parameter_values[0][5], parameter_values[1][5], parameter_values[2][5], parameter_values[3][5], parameter_values[4][5], parameter_values[5][5], parameter_values[6][5], parameter_values[7][5], parameter_values[8][5], parameter_values[9][5], parameter_values[10][5], parameter_values[11][5], parameter_values[12][5], parameter_values[13][5], parameter_values[14][5], parameter_values[15][5], parameter_values[16][5], parameter_values[17][5]],
'h': [parameter_values[0][6], parameter_values[1][6], parameter_values[2][6], parameter_values[3][6], parameter_values[4][6], parameter_values[5][6], parameter_values[6][6], parameter_values[7][6], parameter_values[8][6], parameter_values[9][6], parameter_values[10][6], parameter_values[11][6], parameter_values[12][6], parameter_values[13][6], parameter_values[14][6], parameter_values[15][6], parameter_values[16][6], parameter_values[17][6]],
'i': [parameter_values[0][7], parameter_values[1][7], parameter_values[2][7], parameter_values[3][7], parameter_values[4][7], parameter_values[5][7], parameter_values[6][7], parameter_values[7][7], parameter_values[8][7], parameter_values[9][7], parameter_values[10][7], parameter_values[11][7], parameter_values[12][7], parameter_values[13][7], parameter_values[14][7], parameter_values[15][7], parameter_values[16][7], parameter_values[17][7]],
}
dataframe = DataFrame(parameter_values_to_export, columns= ['a', 'b', 'c', 'd', 'e', 'g', 'h', 'i'])
append_df_to_excel('Test.xlsx', dataframe, 'SNR OP BL Parameters')
print(dataframe)
def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=None,
truncate_sheet=False,
**to_excel_kwargs):
from openpyxl import load_workbook
# ignore [engine] parameter if it was passed
if 'engine' in to_excel_kwargs:
to_excel_kwargs.pop('engine')
writer = pd.ExcelWriter(filename, engine='openpyxl')
try:
# try to open an existing workbook
writer.book = load_workbook(filename)
# get the last row in the existing Excel sheet
# if it was not specified explicitly
if startrow is None and sheet_name in writer.book.sheetnames:
startrow = writer.book[sheet_name].max_row
# truncate sheet
if truncate_sheet and sheet_name in writer.book.sheetnames:
# index of [sheet_name] sheet
idx = writer.book.sheetnames.index(sheet_name)
# remove [sheet_name]
writer.book.remove(writer.book.worksheets[idx])
# create an empty sheet [sheet_name] using old index
writer.book.create_sheet(sheet_name, idx)
# copy existing sheets
writer.sheets = {ws.title:ws for ws in writer.book.worksheets}
except IOError:
# file does not exist yet, we will create it
pass
if startrow is None:
startrow = 0
# write out the new sheet
df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs)
# save the workbook
writer.save()
def fitfunc_polynome_OP_BL(x ,a, b, c, d, e, g, h, i):
return a*(np.power(x,7)) + b*(np.power(x,6))+ c*(np.power(x,5)) + d*(np.power(x,4)) + e*(np.power(x,3)) + g*(np.power(x,2)) + h*x +i
if __name__ == '__main__':
print("\nFitting SNR_UV---------------------------------------------\n")
snr_fitting_excel(18,11,8)
Any tips/remarks/solutions to this error?
This problem comes when we want to update our xlsx file (i.e data.xlsx) using updated dataframe and simultenously that file (i.e data.xlsx) is already opened in background that time.
To solve this problem first close xlsx file (i.e data.xlsx) and perform this task again.

Categories

Resources