How to read a specific row in excel file using python - python

I want to read single row from an Excel_file1, Sheet1, Row number 7 using python, any help?

First install xlrd
pip install xlrd
then open python file and
import xlrd
# Give the location of the file
loc = ("path of file")
# To open Workbook
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
print(sheet.row_values(7))
location is relative path not absolute path.
To read more about xlrd and its usage visit https://xlrd.readthedocs.io/en/latest/
Happy coding.

You can also use pd.read_excel of pandas library:
You would need to install pandas and xlrd first:
import pandas as pd
import xlrd
df = pd.read_excel('abc.xlsx', sheet_name='Sheet1')
Now, you can filter your dataframe to get any specific row using iloc
df.iloc[6] ## This will give you 7th row

Related

Not able to read excel file which I created through open function?

I created one excel file and wrote something in it. I am trying to read that file through pandas - dataframe, but I am getting error
XLRDError: Unsupported format, or corrupt file: Expected BOF record
Code -
import pandas as pd
a = open("D:\\Joseph\\abcsaa.xlsx","a")
a.write("Hello all")
p = pd.read_excel("D:\\Joseph\\abcsaa.xlsx")
p
Thanks for the answers. I need to store tick data in a excel and then read it through dataframe.
What is the use of open function in python for excel file if I have to use other modules for this ?
Excel file cannot be created with inbuilt python open function. You have to use openpyxl package to read and write excel files.
Some besic operations using openpyxl
import openpyxl
# Open Workbook
wb = openpyxl.load_workbook(filename='example.xlsx', data_only=True)
# Get All Sheets
a_sheet_names = wb.get_sheet_names()
print(a_sheet_names)
# Get Sheet Object by names
o_sheet = wb.get_sheet_by_name("Sheet1")
print(o_sheet)
# Get Cell Values
o_cell = o_sheet['A1']
print(o_cell.value)
o_cell = o_sheet.cell(row=2, column=1)
print(o_cell.value)
o_cell = o_sheet['H1']
print(o_cell.value)
# Sheet Maximum filled Rows and columns
print(o_sheet.max_row)
print(o_sheet.max_column)
Install this if you don't already have it.
pip install XlsxWriter
Code:
import xlsxwriter
workbook = xlsxwriter.Workbook("D:\\Joseph\\abcsaa.xlsx")
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello world')
workbook.close()
XLsxWriter can do a lot and has great documentation here.
If the file already exists, open it the first time with
a = pd.read_excel('path/aabcsaa.xlsx')
Else, create a pandas dataframe with
a = pd.DataFrame(data)
and then save it using
pd.to_excel('path/aabcsaa.xlsx')
You opened your file in append mode ("a"). If you want to read it with read_excel by passing the filename, you need to close the file before:
a.close()
And the content of the file needs to be in valid excel format.

Formatting entire excel worksheet - Python

Is there to format the entire xlsx font in python without iterating through each cell. And if this is possible , which module perform this?
You should try xlwings. It is very easy to use.
There is this cheat sheet that can help you with the library.
I think a line of code like this is close to what you need:
Range('A1:Z100').api.Font.Size = 20
Hope it helps!
You can read xls files with the help of python module xlrd to install it use the command.
pip install xlrd
Below is the example of how to use xlrd to open spreadsheet in python.
import xlrd
file_location = "your excel spreadsheet file location"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
# for printing it row by row
for col in range(sheet.ncols):
print(sheet.cell_value(0, col)) # change 0 to print different rows
For further reading check the link please & for the video guidance click here
I hope it works for you : D

How to get the value from merged cells in xlsx file using python?

I am trying to get the value from cell with row = 11 and column B and C. See screenshot for more clarification.
I tried following code using xlrd package but it does not print anything.
import xlrd
path = "C:/myfilepath/data.xlsx"
workbook = xlrd.open_workbook(path)
sheet = workbook.sheet_by_index(0)
sheet.cell_value(10,1)
sheet.cell_value(10,2)
I am not able to output the value from particular merged cells using xlrd package in python.
Above code should print the cell value i.e PCHGFT001KS
I don't know how xlrd works, but I do know how the lovely openpyxl works. You should use openpyxl! it's a robust tool for working with xlsx files. (NOT xls).
import openpyxl
wb = openpyxl.load_workbook(excel)
ws = wb[wb.get_sheet_names()[0]]
print(ws['B11'].value)
Extra:
If you want to unmerge those blocks you can do the following.
for items in ws.merged_cell_ranges:
ws.unmerge_cells(str(items))
wb.save(excel)

How to open .xlsx using python 2.7.3?

I would like to open .xlsx file using python for further manual process.
I tried
wb = load_workbook(filename = 'empty_book.xlsx')
by importing openpyxl module. But it does not open the file rather it just loads the file. Is there any other way to open excel file in python?
Thanks in advance
You could use pandas (also you need to install module xlrd)
import pandas as pd
excel_data = pd.read_excel('empty_book.xlsx')
Import openpyxl
>>> import openpyxl
Load the workbook that you are trying to read
>>> wb = openpyxl.load_workbook("Empty.xlsx")
Give the name of the sheet
>>> ws = wb['sheet_name']
For looping through values in the excel
for row in ws.rows:
for cell in row:
print cell.value
To edit the values
for row in ws.rows:
for cell in row:
cell.value = new_value

Write data into existing excel file and making summary table

I have to write some data into existing xls file.(i should say that im working on unix and couldnt use windows)
I prefer work with python and have tried some libraries like xlwt, openpyxl, xlutils.
Its not working, cause there is some filter in my xls file. After rewriting this file filter is dissapearing. But i still need this filter.
Could some one tell me about options that i have.
help, please!
Example:
from xlutils.copy import copy
from xlrd import open_workbook
from xlwt import easyxf
start_row=0
rb=open_workbook('file.xls')
r_sheet=rb.sheet_by_index(1)
wb=copy(rb)
w_sheet=wb.get_sheet(1)
for row_index in range(start_row, r_sheet.nrows):
row=r_sheet.row_values(row_index)
call_index=0
for c_el in row:
value=r_sheet.cell(row_index, call_index).value
w_sheet.write(row_index, call_index, value)
call_index+=1
wb.save('file.out.xls');
I also tried:
import xlrd
from openpyxl import Workbook
import unicodedata
rb=xlrd.open_workbook('file.xls')
sheet=rb.sheet_by_index(0)
wb=Workbook()
ws1=wb.create_sheet("Results", 0)
for rownum in range(sheet.nrows):
row=sheet.row_values(rownum)
arr=[]
for c_el in row:
arr.append(c_el)
ws1.append(arr)
ws2=wb.create_sheet("Common", 1)
sheet=rb.sheet_by_index(1)
for rownum in range(sheet.nrows):
row=sheet.row_values(rownum)
arr=[]
for c_el in row:
arr.append(c_el)
ws2.append(arr)
ws2.auto_filter.ref=["A1:A15", "B1:B15"]
#ws['A1']=42
#ws.append([1,2,3])
wb.save('sample.xls')
The problem is still exist. Ok, ill try to find machine running on windows, but i have to admit something else:
There is some rows like this:
enter image description here
Ive understood what i was doing wrong, but i still need help.
First of all, i have one sheet that contains some values
Second sheet contains summary table!!!
If i try to copy this worksheet it did wrong.
So, the question is : how could i make summary table from first sheet?
Suppose your existing excel file has two columns (date and number).
This is how you will append additional rows using openpyxl.
import openpyxl
import datetime
wb = openpyxl.load_workbook('existing_data_file.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
a = sheet.get_highest_row()
sheet.cell(row=a,column=0).value=datetime.date.today()
sheet.cell(row=a,column=1).value=30378
wb.save('existing_data_file.xlsx')
If you are on Windows, I would suggest you take a look at using the win32com.client approach. This allows you to interact with your spreadsheet using Excel itself. This will ensure that any existing filters, images, tables, macros etc should be preserved.
The following example opens an XLS file adds one entry and saves the whole workbook as a different XLS formatted file:
import win32com.client as win32
import os
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(r'input.xls')
ws = wb.Worksheets(1)
# Write a value at A1
ws.Range("A1").Value = "Hello World"
excel.DisplayAlerts = False # Allow file overwrite
wb.SaveAs(r'sample.xls', FileFormat=56)
excel.Application.Quit()
Note, make sure you add full paths to your input and output files.

Categories