Paste from clipboard to excel file - python

Is there a way to automate this process?
1- create new excel file
2- paste data which is ale+redy in clipboard to this new excel file with python
3- save this excel file
I have tried to find this function in openpyxl and xlsxwriter but there is only function for writing exact data to defined cells.
edit: the data in clipboard is the result from program run before this script
thanks

This method gets data copied from the clipboard and converts it into a DataFrame. You can then select whichever column you want from the DataFrame and use the to_csv method to write to the Excel file.
pandas.read_clipboard(sep='\\s+', **kwargs)
DataFrame.to_csv()
Check out the documentation :
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_clipboard.html
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html

Related

How to write a file in azure function app in python?

I am creating a azure function. This function locally have a csv file. I read it via given code
csv = f'{context.function_directory}/new_csv.csv'
df=pd.read_csv(csv)
after reading the data, I want to make some changes to this csv (like adding some columns). Please suggest some code how can I write this updated csv/dataframe in same directory and with the same name.
you just have to write back to the same file (or a new file)...
csv = f'{context.function_directory}/new_csv.csv'
df=pd.read_csv(csv)
# make edits to the dataframe
make a new csv or write to existing csv
df.to_csv()
Here is a link:
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html

Openpyxl don't preserve formatting

I have an excel file that i want to use as template, what i need is just change the values of some cells, and save it as another excel file. The problem is that when i save it the formatting, style and some date values are changed
from openpyxl import load_workbook
wb = load_workbook("test.xlsx")
ws = wb["RDO"]
ws["B8"].value = "MAI MAN"
wb.save("new.xlsx")
The old file:
The new one:
As you can see the borders and date fields were changed.
I was thinking in just unzip the excel and modify the xml files, then zip it back, but this approach has a problem. I will need to make a copy of some worksheets, so i tought i should be ok in just copy and paste the sheet.xml file and change the workbook.xml file to add this new sheet, but when i do this all the cells are cleared which is weird because when i copy the sheet in the excel program the output sheet file it's exactly the same as the original
I would like some simple solution if possible, maybe some other library or a fix for this xml sheet problem

Trouble with to_csv saving groupby dataframe

I want to save groupby dataframe in csv but i am trying to save in csv it is not saving groupby dataframe.
this is data :
dataframe image
i run this code df.groupby(['Date','Name']).sum() after that i got
output image of groupby dataframe
but i am trying to save in csv file it save like this
I run this code df.to_csv("abcd.csv")
csv file image
But I want to save in csv file like
saving excel file output which i want
please tell me the solution
thank you
CSV files are plain text, The agreed format is each row is separated by newline and each character is separated by , in general.
To achieve the formatting you want, you can convert the df into an excel file instead of csv
gdf = df.groupby(['Date','Name']).sum()
gdf.to_excel("<path_to_file>")
You will need to explicitly install xlwt to achieve working with excel files
pip install xlwt

How to update a cell value in excel in Python

I have a spreadsheet which is having following columns:
TestID TestData ExpectedOutput ActualOutput Result
I have separate python scripts for each test-id. I need to read the row corresponding to that particular test-id and after execution, need to update result in same spreadsheet. I am not able to update that result value. can someone please help?
I read the spreadsheet using Pandas.
e.g.
a row in spread sheet:
TestID TestData ExpectedOutput ActualOutput Result
Testid-1 Min_freq=5,Max_freq=60, Drive started Drive started Pass
My script would search for this testid and read the test data. after execution, it would compare the output with expected output and accordingly would update the value of cell Result. I am not getting how to update result value.
Please help me.
The only package that can modify/edit an existing excel is openpyxl
You can read it by xlrd, but cannot modify it by xlwt or xlsxwriter, which can create and flash new xls and xlsx.
However, if you are using another source to edit the existing excel, they are not editing the same ones but two template mirror files, be sure to save it before letting python to read it, and vise versa.

Append existing excel sheet with new dataframe using python pandas without loading the old one

This is an old question, however no one wrote a solution for without loading the excel file. Assuming the excel file is too huge, this will be inefficient to load the existing file and save on it. Instead is there any method to append, like for ordinary files?
df is the dataframe I would like to append
df.to_excel(folder+"file.xlsx", header=False, index=False)
However file.xlsx exists and there is another df with the same headers.
How can I append df to existing file.xlsx, without loading file.xlsx?
I do not know about any append features in Openpyxl. Openpyxl has however a writeonlymode wb = Workbook(write_only=True)
I would try to save (and append) the dataframe as a feather binary file, then just delete and create a new workbook with write_only mode.
This should be much faster than loading and appending the Excel file, but it will depend on what type and how much data there is.
Hope this helps.

Categories

Resources