how to extract all the data in a single column in excel sheet using sikuli python - xlrd

This selecting a single data from excel sheet. Now i want to fetch all the data from the excel sheet. Can somebody help me to solve this problem
I have tried this code:
import xlrd
import datetime
EXCELFILE = "D:\\Muthu\\Datamanipulation\\book1.xlsx"
book = xlrd.open_workbook(EXCELFILE)
sheet = book.sheet_by_index(0)
celltype = sheet.cell_type(40,33)
value = sheet.cell_value(40,33)

Related

How to write to same excel file after the operation with sheet name

I have excel file name 'excel.xlsx'. it is having 10 sheets with different name. I am able to save but all other sheets are missing
After the Operation I need to save the sheet on the same excel starting from 5th row and 5th column
The new sheet name is 'Final'
My dataframe name is df
writer = pd.ExcelWriter('excel.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Final', startrow=5,startcol=5,index=False)
writer.save()
Did you tried to add a writer.save() before your df.to_excel(...)?
Will be easier - Try using openpyxl engine.
Prerequisite: openpyxl - Install it with command -
pip install openpyxl
Variables used:
sh_nm= Sheet Name you want to append in the workbook.
filename= Workbook name.
df=Dataframe for your new sheet.
Note: Exceptions not handled .
Code:
import pandas as pd
from openpyxl import load_workbook
# !pip install openpyxl
def sheet_collate(filename,_df,sh_nm):
with pd.ExcelWriter(filename, engine='openpyxl') as writer:
writer.book = load_workbook(filename)
_df.to_excel(writer,sh_nm, startrow=5,startcol=5,index=False)
writer.save()
return "Workbook Edited.."
filename='excel.xlsx'
sh_nm='Final'
sheet_collate(filename,df,sh_nm)

How can I insert data from a online table in excel using Selenium for python 3.7?

I was asked to create an automation tool to create a report excel sheet using the data from an internal web page of the company I work for. I selected python(3.7) as my language(since most of my coworkers use it as well)and Selenium to do the job. Once I have clicked and filled all the fields using Selenium to filter the data I need, I got at least 60 pages of 20 rows each one(the number can vary), so I have to copy each page data in an excel sheet and click the 'next' button, copy the data again and so on, until all pages are processed. I was able to copy the data for the first page of the web site, but when I try to do the 'next' process, it fails in saving the data in the excel sheet. Here is the code I'm working with
***Bunch of Selenium steps***
def excelFactory(values, row, col): #fuction to create the excel workbook
fname = 'App_Report.xlsx'
if os.path.exists(fname): #check if the workbook is already created
wb = openpyxl.load_workbook(fname)
ws = wb.get_sheet_by_name('Sheet')
else: #create the workbook if not found
wb = Workbook()
ws = wb.active
ws.cell(row= row, column= col).value = values #insert the data from the online web in the
#workbook
row = row + 1
wb.save(fname)
def dataDownload(): #function to parse thru the online table and read the data
data = driver.find_elements_by_class_name("rowNumber")
for td in data:
values = td.text
print(td)
excelFactory(values, row, col)
NxtButton = driver.find_elements_by_class_name('NxtButtonClassName')
for content in NxtButton:
if content.text == 'Next':
content.click()
dataDownload()
dataDownload()

Is it possible to insert a worksheet into an existing workbook using Python?

The Problem
Creation of fancy reports using Pandas and Python.
Proposed Solution
Using a template xlsx file containing a template sheet nicely formatted with references to another pre-populated worksheet, delete the pre-populated sheet and insert the new worksheet from pandas. The template sheet will lose the links reverting to #REF so these will need to be renamed.
I tried:
import os
import xlrd, xlwt
import envconfig
swb1 = xlrd.open_workbook(os.path.join(envconfig.REPORT_WRITER_PATH,'TEMPLATE.xls'), on_demand=True, formatting_info=True)
swb2 = xlrd.open_workbook(os.path.join(envconfig.REPORT_WRITER_PATH,'REPORT.xls'), on_demand=True, formatting_info=True)
swb1s1 = swb1.sheet_by_name('Template')
swb2s1 = swb2.sheet_by_name('Report')
twb = xlwt.Workbook()
sheet1 = twb.add_sheet(swb1s1)
sheet2 = twb.add_sheet(swb2s1)
twb.save("python_spreadsheet.xls")
The above errors with:
sheet1 = twb.add_sheet(swb1s1)
File "C:\Users\pa003202\AppData\Local\Continuum\Anaconda3\lib\site-packages\xlwt\Workbook.py", line 366, in add_sheet
sheetname = sheetname.decode(self.encoding)
AttributeError: 'Sheet' object has no attribute 'decode'
sheetname = sheetname.decode(self.encoding)
AttributeError: 'Sheet' object has no attribute 'decode'
Is there a way to inject data from pandas into a workbook or to open a workbook and insert a sheet?
I solved this by creating a template as described and used the solution here:
Proposed Solution Using a template xlsx file containing a template sheet nicely formatted with references to another pre-populated worksheet, insert the new worksheet from pandas. The template sheet does not lose the links providing the inserted sheet has same name.
Solution:
Look at How to write to an existing excel file without overwriting data? and this works for the scenario.

How to create Excel Table by VBA at runtime?

During execution of macro I reads multiple excel file and creates a new excel file, i build a similar sheet:
At runtime by VBA I want to set this area and create a table, add Total Row and get the following result:
Thank you :-)
Like this:
Range("A1:I9").Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$I$9"), , xlYes).Name = "Table1"
ActiveSheet.ListObjects("Table1").ShowTotals = True
ActiveSheet.ListObjects("Table1").ListColumns("B").TotalsCalculation = xlTotalsCalculationSum
ActiveSheet.ListObjects("Table1").ListColumns("C").TotalsCalculation = xlTotalsCalculationSum
'Repeat row above until column I
I got this by recording a VBA Macro. If you are not familiar with Macro recording see link here.

Is there a way to programmatically insert formulas into a csv file using vb.net?

I need to add formulas to a CSV file that already exists. Is this possible to do this using VB.Net?
The idea is I already have a CSV file and I need one column to be populated in each cell with a custom formula. This has to be done programmatically because these files are being created dynamically.
Does anyone know how to do this? Thanks.
1,2,3,=SUM(A1:C1)
Surprised me when I tried it, but Excel will keep the formula.
You can even export formulas into a CSV by first displaying them on screen.
(Ctrl-`)
While I stand by that my original answer is technically correct, I have been getting a lot of downvotes on this. Apparently popular spreadsheet software such as Microsoft Excel, Open Office, Libre Office Calc will calculate formulas that are entered into a CSV file. However, I would still not recommend relying in this capability.
Original answer:
The CSV format does not support formulas. It is a plain text only format.
You can import formula's into excel via a text file using comma separated values. Just remember to make sure it is named with the suffix .txt.
Then do the import.
My example import ( Data table, From Text)
Column1,Column2,ResultColumn
1,2,=A2+B2
It imported and computed just fine
Are you generating the CSV file? If so, consider writing an actual Excel file. (I'm assuming you're importing into Excel, since you used the term "cell", which has no meaning in CSV.)
Here's a link on how to do it: Create Excel (.XLS and .XLSX) file from C#
If you aren't generating the CSV, and if all you want is to add a new, calculated value,(rather than a formula that will change dynamically as cells change values) you can do this easily enough by reading in the CSV file, parsing each line enough to get the values you need for your formula, calculating the result, and appending it (after a comma) to each line before writing the line out to a new file.
You could open the csv in Excel and then add the formulas to Excel and save back out to csv. You would do this by using the Microsoft Excel 11.0 Object Library. Then
dim wb as Excel.Workbook
dim exApp as New Excel.Application
dim exSheet as Excel.Worksheet
dim rowNum as integer = 1
wb = System.Runtime.InteropServices.Marshal.BindToMoniker(pathAndFileNameOfCSV)
exApp = wb.Parent
exApp.DisplayAlerts = False
exApp.AlertBeforeOverwriting = False
exSheet = exApp.ActiveWorkbook.Sheets.Item(1)
'do your functions in a loop here i.e.
exSheet.Range("A" & rowNum).Value = "=SUM($A$1:$D$4)"
rowNum += 1
wb.Close (True) 'closes and saves
Saving the workbook should convert the formulas back to the values when it is closed.
In Excel, to import formulas with commas the formula must be encapsulated by double quotes to prevent the formula being spread across cells. For example:
2,4,6,13,=sum(A1:C1),"=if(A1=C1,D1-A1,D1+A1)"
Another quirk of Excel is that if you have a string consisting entirely of numbers, you must present it as a formula to retain leading zeros. "00012345" imports as 12345, ignoring the quotes. To import as text, the .CSV file must present this as ="00012345".