I need to convert function fun(=SUMIF(B1:B16,B2,C1:C16)) into value so that I can use the output in another operation.
Moreover, I need to know which is the best module for working in excel and formatting cells.
import openpyxl
wb = openpyxl.load_workbook('file.xlsx')
ws = wb.active
# add a simple formula
ws['A3'] = ("=SUMIF(B1:B16,B2,C1:C16)")
wb.save("file.xlsx")
You could have a look at pyparsing, but I don't know, if it suits your needs best and can solve the given task.
Related
I have an excel workbook with several sheets. I need to read a portion from one of the sheets, get a filtered dataframe and write a single value from that filtered dataframe to a specific cell in the same sheet. What is the best way to accomplish this, ideally without opening the excel workbook? I need to run this on linux, so can't use xlwings. I don't want to write the entire sheet, but just a selected cell/offset inside it. I tried the following to write to the existing sheet, but doesn't seem to work for me (no update occurs at the desired cell):
with pd.ExcelWriter('test.xlsx', engine='openpyxl') as writer:
writer.book = load_workbook('test.xlsx')
df_filtered.to_excel(writer, 'Sheet_Name', columns=['CS'], startrow=638, startcol=96)
Any tips would be helpful. Thanks.
If you're just writing a single cell the below should suffice.
import pandas as pd
import openpyxl
df = pd.DataFrame(data=[1,2,3], columns=['col'])
filtered_dataframe = df[df.col == 1].values[0][0]
filename = 'test.xlsx'
wb = openpyxl.load_workbook(filename)
wb['Sheet1'].cell(column=1, row=2, value=filtered_dataframe)
wb.save(filename)
I believe your issue was that you never called the save method of the writer.
So I just learned that if you type VBA. this will activate an auto list that includes VBA functions. Does this mean that VBA is an object? If I want to "fully qualify" using a VBA function, what would those qualifiers be?
For example, would these all be valid statements:
x = Sqr(25)
x = VBA.Sqr(25)
x = Application.VBA.Sqr(25)
Thanks for the clarification.
If you don't qualify, the default is that VBA assumes it is a VBA function. If you want to use a worksheet function in VBA, then use something like:
x = Application.WorksheetFunction.VLookup(Range("A1").Value, Range("B1:C100"), 1, False)
It is REALLY important to qualify if you use a function likeTRIM() because the VBA version and the worksheet version have different results.
I tried to create a file with decimal numbers on cells (F1:F4), but when i am going to insert a formula to the cell, it's show it like a string.
Code:
s1("F5").Formula = "=SUMME(F1:F4)"
s1("F5").Value = s1("F5").Formula
i tried:
s1("F5").Formula = "=SUMME(F1:F4)"
s1("F5").Formatting.HiddenFormula = False
s1("F5").Value = s1("F5").Formula
i tried:
s1("F5").Formula = "=SUMME(F1:F4)"
The result its the same, on cell F5, =SUMME(F1:F4)
always on string format.
Note : s1 = Sheet1
Try this:
s1.Cells("F5").Formula = "=SUM(F1:F4)"
You didn't set the range of the sheet. Also changed the SUMME to SUM, unless you have a User Defined Function (UDF) which is called SUMME or you are using Excel in a language where Summe = Sum.
EDIT:
After looking into the library you are using, I would try the following:
s1["F5"].Value = "=SUMME(F1:F4)";
I found the solution. With that library, it's not possible.
I used another library just to make the formula. Nix library it's good to add data to the cells.
I work with people who use Excel and continuously add or subtract rows unbeknownst to me. I have to scrape a document for data, and the row where the header is found changes based on moods.
My challenge is to handle these oscillating currents by detecting where the header is.
I first organized my scrape using xlrd and a number of conditional statements using the values in the workbook.
My initial attempt works and is long (so I will not publish it) but involves bringing in the entire sheet, and not slices:
from xlrd import open_workbook
book = open_workbook(fName)
sheet = book.sheet_by_name(sht)
return book,sheet
However, it is big and I would prefer to get a more targeted selection. The header values never change, nor does when the data shows up after this row.
Do you know of a way to implicitly get the header based on a found value in the sheet using either pandas.ExcelFile or pandas.read_excel?
Here is my attempt with pandas.ExcelFile:
import pandas as pd
xlsx = pd.ExcelFile(fName)
dataFrame = pd.read_excel(xlsx, sht,
parse_cols=21, merge_cells=noMerge,
header=header)
return dataFrame
I cannot get the code to work unless I give the call the correct header value, which is exactly what I'm hoping to avoid.
This previous question seems to present a similar problem without addressing the concern of finding the headers implicitly.
Do the same loop through ExcelFile objects:
xlsx = pd.ExcelFile(fName)
sheet = xlsx.sheet_by_name(sht)
# apply the same algorithm you wrote against xlrd here
# ... results in having header_row = something, 0 based
dataFrame = pd.read_excel(xlsx, sht,
parse_cols=21, merge_cells=noMerge,
skip_rows=header_row)
I have an excel spreadsheet with lots of data that I want to import into matlab.
filename = 'for_matlab.xlsx';
sheet = (13*2)+ 1;
xlRange = 'A1:G6';
all_data = {'one_a', 'one_b', 'two_a', 'two_b', 'three_a', 'three_b', 'four_a', 'four_b', 'five_a', 'five_b', 'six_a', 'six_b', 'seven_a', 'seven_b', 'eight_a', 'eight_b', 'nine_a', 'nine_b', 'ten_a', 'ten_b', 'eleven_a', 'eleven_b', 'twelve_a', 'twelve_b', 'thirteen_a', 'thirteen_b', 'fourteen_a'};
%read data from excel spreadsheet
for i=1:sheet,
all_data{i} = xlsread(filename, sheet, xlRange);
end
Each element of the 'all_data' vector has a corresponding matrix in separate excel sheet. The code above imports the last matrix only into all of the variables. Could somebody tell me how to get it so I can import these matrices into individual matlab variables (without calling the xlsread function 28 times)?
You define a loop using i but then put sheet in the actual xlsread call, which will just make it read repeatedly from the same sheet (the value of the variable sheet is not changing). Also not sure whether you intend to somehow save the contents of all_data, as written there's no point in defining it that way as it will just be overwritten.
There are two ways of specifying the sheet using xlsread.
1) Using a number. If you intended this then:
all_data{i} = xlsread(filename, i, xlRange);
2) Using the name of the sheet. If you intended this and the contents of all_data are the names of sheets, then:
data{i} = xlsread(filename, all_data{i}, xlRange); %avoiding overwriting