I tried a program in python to read data from excel when I retrieve data from Excel its not displaying in correct format.
Here is my sample excel data:
Here is my code
import os
os.getcwd()
import openpyxl
wb=openpyxl.load_workbook('sample.xlsx')
type(wb)
wb.get_sheet_names()
sheet=wb.get_sheet_by_name('Sheet1')
sheet.title
columns=3
rows=3
for r in range(1,rows+1):
for c in range(1,columns+1):
d=sheet.cell(row=r,column=c)
print('%-8s'%d.value , end='')
print('')
I want output like this
ID Name age
1 Sam 12
2 Arun 12
but I get like this:
ID
Name
age
1
Sam
12
2
Arun
12
The issue is with the second print('')
, which induces an unnecessary line break after displaying your data.
for r in range(1,rows+1):
for c in range(1,columns+1):
d=sheet.cell(row=r,column=c)
print('%-8s'%d.value , end='')
print('') <------There's the error
It will be executed after each data print instead of each line.
for r in range(1,rows+1):
for c in range(1,columns+1):
d=sheet.cell(row=r,column=c)
print('%-8s'%d.value , end='')
print('')
Just pull it back one indent level to call it after each line
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.