Merge particular sheets from multiple workbooks - python

I have a folder with 8 workbooks with multiple sheets. I want to rearrange columns from the sheet named RAW from all workbooks and combine all the RAW sheets into one sheet as Final_Raw.
I need a macro code to achieve this also can this be automated using python?

It is possible to do in VBA. You need to collect the data from the sheets. This means you declare all the sheets like:
Sub getdata()
Dim strLocation As String
Dim objWorkbookOne As Workbook
Dim wsData As Worksheet
Dim intFR, intLR As Integer
strLocation = "C:\Users\fredd\Documents\"
Set objWorkbookOne = Workbooks.Open(strLocation & "14082022194559_download_MEDEWERKER.xlsx")
Set wsData = ThisWorkbook.Sheets(1)
wsData.Activate
intFR = 1
intLR = objWorkbookOne.Worksheets("MEDEWERKER").Cells(Rows.Count, 1).End(xlUp).Row
For intFR = 1 To intLR
wsData.Cells(intFR, 1) = objWorkbookOne.Worksheets("MEDEWERKER").Cells(intFR, 1)
Next intFR
End Sub
In the code above we get data from a file named 14082022194559_download_MEDEWERKER.xlsx on location *C:\Users\fredd\Documents*. I made a variable of the location so it is easy to change if nessesary. The file is opened in objWorkbookOne (ofcourse you can do this for eight workbooks as well).
When the workbook is opened, we activate the sheet in which we want to 'paste' the data. Next the first row (intFR) and last Row (intLR) are defined in workbookone. With that FOR loop you can 'copy' the data to the masterfile.
I don't know exactly how your masterfile and other files are build up, so the I have to make assumptions. In this code (above) I copy one column to another column, but this is also possible with ranges.

Related

Python: How to save excel workbook without ruining dynamic spill/array formulas

Short description of the problem:
I am currently accessing an Excel workbook from Python with openpyxl.
I have some dynamic spill formulas in sheet1, like filter(), byrow() and unique().
With the python script, I am doing some operations in sheet2, but I am not touching sheet1 (where the dynamic spill formulas are located).
When using workbook.save() method in Python, I experience that the dynamic formulas in sheet1 are ruined and static, not having the dynamic functionality they had before interacting with python.
What can I do? Use a parameter in .save()? Use another method?
Detailed description of problem (with pictures):
I have a workbook called Original, with the following three sheets:
nums
dynamic
dump
In "nums" I have a cell for ID (AA), and a column with some numerical values (picture1).
In "dynamic" I have some dynamic formulas like byrow() and filter() that updates automatically with the values in ID and Values-column of "nums" (picture2).
The sheet "dump" is for now empty.
I have a second workbook called Some_data, which have one sheet with a 3-column dataframe (picture3).
I am dumping the 3-column dataframe of Some_data into the empty "dump"-sheet of Original with a Python script, and then using the workbook.save() method to save the new workbook.
The code is here:
import pandas as pd
from openpyxl import load_workbook
Some_data = filepath of the workbook
Original = filepath of the workbook
df = pd.read_excel(Some_data, engine = "openpyxl")
wb = load_workbook(filename = Original)
ws = wb["dump"]
rownr = 2
for index, row in df.iterrows():
ws["B"+str(rownr)] = row["col1"]
ws["C"+str(rownr)] = row["col2"]
ws["D"+str(rownr)] = row["col3"]
rownr+=1
wb.save(filepath of new workbook)
Now, the newly saved workbook's sheet "dump" has now been populated.
The problem is that the dynamic formulas in the sheet "dynamic" has been ruined, although the python script does not interact with any of the sheets "nums" or "dynamic".
First of all - the dynamic array formulas (like filter) now have brackets around them (picture4), and the dynamic array formulas are not dynamic anymore (there are no blue line around the array when selected, and they do not update automatically; picture5).
I need help with what to do. I want to save the excel-file, but with the dynamic array formulas not being ruined.
Thank you for your help, in advance.
Frode

How to run a python code for large inputs

In my code,
Samp_size
MSI
MRI
M_ASRS
a_h
d_h
a_v
d_v
max_hor_vel
max_ver_vel
These are randomly generated parameters above.Each can all take different numbers of values, lets say each has 2 different value.
I print them as dataframes to an excel file each in different sheets.(sheet1,sheet2 etc.)
So I have 2^10 different parameter set. How can I print the all the solutions for all this parameter set in order to an excel file?
It seems like you want to combine sheets into one single excel file, if so, the following post will solve your problem: Combine Multiple Excel sheets within Workbook into one sheet Python
The code from # And then append all the Workbooks into single Excel Workbook sheet will help.

Excel sheet to Rmarkdown

I have a excel workbook which have n number of worksheets in it. Each sheet contains different number of tables in it of different length. so, is there a way that I can convert them into Rmarkdown tables in just one go. The method I currently know is to copy and past the table on some converter and it converts but its static. Is there a way that in R or python I just read the excel file and the tables of excel are converted into Rmarkdown tables. like I don't want to copy and paste each table to convert.
You should consider xlsx package and xlsx::read.xlsx. According to Geza in this answer you should before take care of the workbooks sheets names. So :
wb <- loadWorkbook("path-to-your_xlsx/file.xlsx")
sheets <- getSheets(wb)
namesl <- names(sheets)
Then you can made a data.frame for each sheets, for e.g with read.xlsx or read.xlsx2, like in the following code :
> for(1 in 1:length(namesl)) { assign(paste(namesl[i]),
> # create an object in your env. with assign()
>
> xlsx::read.xlsx("path-of-your-workbook.xlsx", # read a workbook-sheet
> sheetName = paste(namesl[i]), as.data.frame = T, header = T
> # As you like for importing opts, need a check for correct importation.
> )
> }
In order to create a data.frame by sheets in the workbook (each data.frame had the same name as a workbook sheet) and fill it with the content of the sheet.
Excellent day

Import data from excel and use it to create individual files on c drive

Please look at the picture. I have individual acct no in excel Column A that needs to be created as individual files on the c drive. How can I do that without doing it manually?
Instead of python, we could use VBA within excel.
Assuming you wanted to just write out a blank excel file with each one of these names, we could loop over the text in a range, ex:
Function GetNames(aRange as Range):
Dim aCell as Range
# Define ArrayList here
For Each aCell in aRange:
ArrayList.add(aCell.Text)
Next
Set GetNames = ArrayList
End Function
and then loop over each item in that arrayList in a separate function to write them out, ex:
Sub WriteFiles(ArrayList):
Dim wb As Workbook
Dim name as String
For Each name in ArrayList:
Set wb = Application.Workbooks.Add()
wb.Activate
wb.SaveAs name
wb.Close (False)
Next
End Sub

How do I fill [] with sheet information from pandas and xlrd?

I have this:
dic_sheets = {}
for y in xl_files[]
dic_sheets.update({y:[]})
I want to populate the tables in the dictionary (dic_sheets) for each key(y) with the individual sheets inside of the excel document.
I do not know how many sheets are inside of the excel document; I don't have an index number to stop a range (x,y,z) loop.
Another way to put it: I want to dump x-number of excel files into the active directory and have each files sheets populate in a dictionary when I run the .py in CMD.
Can anyone help me achieve this goal?
xl_files contains "ExcelFile" data "pandas.io.excel.ExcelFile object at 0x0FF6B0D0
Edit: y represents individual excel files
Edit2: I need only the sheet names (or their unique index numbers) to populate, (i.e. 'sheet1', 'pivot2'). I'm not yet concerned with cells in the sheets.
Edit3: I already have the table ‘xl_files’ generated to contain every excel file in the cwd
I figured it out!
I had to use a for loop and the return function as an object, then combine it with another object of the array.append function and return function with a new array.
I'll try to word my questions better in the future, as I did not get a bite this round.

Categories