Editing and Saving Excel file from Matlab - excel

I need to generate a code in Matlab to Edit and then save an Excel file. The code has to do the following:
Open the Excel file
Go to the second sheet (sheet2)
Change the value of cell A2
Go to the first sheet (sheet1)
Save the sheet1 as a tab delimited text file (.txt)
Sheet 1 contains cells which depend on the cell A2 in sheet2 (I need to do this several times for different values of sheet2-A2, that's why I need to code it).
Browsing the web, I have been able to code steps 1-4 (see code bellow). However, I have not been able to find the way to do 5. I would very much appreciate your help. Thank you!!
% Open Excel Server:
Excel = actxserver('Excel.Application');
% Makes Excel visible in the screen:
set(Excel, 'Visible', 1);
% Open Excel file:
Workbooks = Excel.Workbooks.Open('E:\TEST.xlsx');
% Make the second sheet active:
Sheets = Excel.ActiveWorkBook.Sheets;
sheet2 = get(Sheets, 'Item', 2);
invoke(sheet2, 'Activate');
% Get a handle in the active sheet:
Activesheet = Excel.Activesheet;
% Edit cell A2:
A = 2;
ActivesheetRange = get(Activesheet,'Range','A2');
set(ActivesheetRange, 'Value', A);
pause(5) %Pauses the code for 5 seconds (so sheet 1 updates the formulas)
% Going to Sheet1 where the focal data is:
Sheets = Excel.ActiveWorkBook.Sheets;
sheet1 = get(Sheets, 'Item', 1);
invoke(sheet1, 'Activate');

For this kind of stuff, you can directly go to the Microsoft docs.
These should help you out:
http://msdn.microsoft.com/en-us/library/office/ff841185.aspx
http://msdn.microsoft.com/en-us/library/office/ff198017.aspx
Basically, you can issue the commands here, as you would in the SaveAs dialog in the Excel GUI.

Related

HYPERLINK not working with particular sheet names

I have a workbook - lets call it Book1
Book1 contains 5 worksheets; Sheet1, Sheet1A, Sheet2, Sheet2A, Summary
Within the Summary worksheet there is a table which contains summary of the data held in the other 4 worksheet.
The first column in the table contains rows naming each of the 4 worksheets; Sheet1, Sheet1A, Sheet2, Sheet2A.
Using the excel HYPERLINK function I have created links in each of these rows so as to navigate directly to each particular worksheet, as follows:
=HYPERLINK("[Book1.xlsx]Sheet1!A1", "Sheet1")
=HYPERLINK("[Book1.xlsx]Sheet1A!A1", "Sheet1A")
=HYPERLINK("[Book1.xlsx]Sheet2!A1", "Sheet2")
=HYPERLINK("[Book1.xlsx]Sheet2A!A1", "Sheet2A")
However, only two out of the four hyperlinks work. The two which do not work are those which end with 'A'; Sheet1A and Sheet2A
All worksheets have been spelt correctly and I have experiment with the format to see whether changing to text or general made any difference, but it did not.
Please could could someone shed some light on why I am facing an error with Sheet1A and Sheet2A - my thoughts are that it is something to do with there being a letter after the number?
Thank you for your time.
If they're in the same workbook, then try adding the # & '
=HYPERLINK("#'"&"Sheet1A'!A1", "Sheet1A")
=HYPERLINK("#'"&"Sheet2A'!A1", "Sheet2A")

With openpyxl, nested list starts writing / appending to cell A2 not A1

I have an excel spreadsheet to calculate data that is added every week for comparison on the first sheet and each additional sheet is the raw data listed as W01 to W52 for each week. I've stripped down the code here to make the issue the only thing not working. In reality, I'm taking multiple CSVs and dumping them into a list, formatting the list and then I am trying to write that list into the first unused worksheet.
It is successfully finding the first empty worksheet, but then when it writes the data from the list into it, it is starting at A2 not A1.
So the worksheet already exists. I have tried deleting all the cells and clearing the contents within excel first, but it always starts at A2 not A1. What am I missing?
Note: If this helps, originally, I had just deleted last year's data from the worksheet, and when I left it like this, it would start adding the list's data on the first row after the deleted info, such as A52 or something. Deleting the contents of the sheet has "fixed" that issue so it is now only starting at A2 but I want to start it at A1.
If I manually add something to the A1 cell, it works, such as:
ws_week.cell(row=1, column=1).value = 'This should be A1'
So I think I can force it to write with a loop for the row number in a loop for the column number, but it seems like the below should be working.
import openpyxl
output_excel = 'KB Videos 2021.xlsx' #Excel Report
#opens excel report
wb = openpyxl.load_workbook(output_excel)
#find the first blank worksheet
for sheet in wb.sheetnames:
if sheet == 'Weekly Stats':
pass #ignore first worksheet i.e. calculations
elif wb[sheet]['A1'].value == None:
ws_week = wb[sheet]
break
test=[[1,2,3],['A','B','C'],[4,5,6],['a','b','c']]
for x in test:
ws_week.append(x)
wb.save(output_excel)
print('Populated ',ws_week.value)`enter code here`)
The 1,2,3 from the "test" list are being put into A2, B2, C2 when I expect them to be in A1, B1, C1.
What did I miss?
Worksheets should never really be considered as "blank". When appending to worksheets, openpyxl uses an internal counter that will start at the next below any existing cells. As openpyxl creates cells on demand, wb[sheet]['A1'].value will implicitly create the cell "A1" so that it can check the value. This is why data is subsequently appended from the second row. You can avoid this by deleting the row after the check, but you might also want to make the check a little more robust.
Here's the code I used to make it work:
I replaced:
for x in test:
ws_week.append(x)
with:
# calculate max rows and columns
maxr = len(test)
maxc = len(test[0]) #I could probably put this into the loop in case a column is different, but not with my data
for this_row in range (1, maxr + 1):
for this_column in range (1, maxc + 1):
cellsource = test[this_row-1][this_column-1]
ws_week.cell(row = this_row, column = this_column).value = cellsource

How can I copy the formatting of a row from an Excel spreadsheet and paste it into another Excel spreadsheet using MATLAB?

I have an input spreadsheet and an output spreadsheet that's generated after calculations are run through MATLAB. I need the formatting of the output spreadsheet's header row to match the input spreadsheet's header row (mainly the column width).
Can anyone please help with the MATLAB code for this? Perhaps if there's a way to utilize the format-copy and paste functions in Excel but coded into MATLAB?
I've been working on this for weeks and can't seem to figure it out. Any help would be greatly appreciated.
This is the code I have as of right now:
sheetIndex = 1;
ExcelWorkbook.Sheets.Item( 'Input.xlsx' ).Activate;
sourceCellRange = 'A3..Z3';
Excel.Range(sourceCellRange).Copy;
sheetIndex = 2;
ExcelWorkbook.Sheets.Item( 'Output.xlsm' ).Activate;
destinationCellRange = 'A3..Z3';
Excel.Range(destinationCellRange).Select;
Excel.ActiveSheet.Paste;
Excel.Range('A1').Select;

How to create sheets in an Excel file with pandas and python?

Let's say that I have the following Excel file:
enter image description here
I would like to use Python to create an Excel file for each sheet, with all the columns as well. So in Sheet1 to have all the lines where "Sheet1" is mentioned in column A, and so on.
I am able to create the sheets, but not to put the data into it, could someone please help? Below is my code until now:
sheets = ['Sheet1', 'Sheet10', 'Sheet2', 'Sheet3']
finalfile = 'test to check.xlsx'
writer = pd.ExcelWriter(finalfile)
for sheet in sheets:
df.to_excel(writer,sheet)
writer.save()
I suppose the below correction in your code should work fine for your expected output.
for sheet in sheets:
df.loc[(df['Sheet'] == sheet)].to_excel(writer, sheet)
Let me know if this works :)

Excel VBA charts changing source

I have an excel sheet with 69 charts linked to another excel file, and I want to change the source of all charts to a Sheet in my current file, but I couldn't do it. Does anyone know how to? Thanks!
Right click on your chart,
Choose 'Select Data'.
In chart data range at the top, enter:
=[PathTo.xlsx]Sheet[N]![Range]
Where:
[PathTo.xlsx] = Path to your remote file
Sheet[N] = Sheet Name
[Range] = Cell range to be charted.
For example:
=[Book1.xlsx]Sheet1!$A$1:$B$13
Hope that helps!

Resources