import openpyxl
work_book=openpyxl.load_workbook(r'C:\Users\allso\Desktop\new vba projects\openpyxl\countries.xlsx')
print(work_book.sheetnames)
sheet_obj=work_book['Sheet1']
print(sheet_obj.title)
print(sheet_obj['A1'])
cell=sheet_obj['B1']
print(cell.value)
print(cell.row)
print(cell.column)
print(cell.number_format)
print(cell.coordinate)
print(cell.data_type)
print(sheet_obj['A2'].value+\
'\'s capital '+sheet_obj['B2'].value+\
' has a population of '+str(sheet_obj['C2'].value)\
)
print(sheet_obj.cell(row=1,column=2))
print(sheet_obj.cell(row=1,column=2).value)
print(sheet_obj.max_row)
print(sheet_obj.max_column)
max_col=sheet_obj.max_column
for i in range(1,max_col+1):
cell_obj=sheet_obj.cell(row=1,column=i)
print(cell_obj.value)
max_row=sheet_obj.max_row
for i in range(1,max_row+1):
cell_obj=sheet_obj.cell(row=i,column=1)
print(cell_obj.value)
print(sheet_obj['A1':'C2'])
for rows in sheet_obj['A1':'C2']:
for cell in rows:
print(cell.coordinate,cell.value)
print('--------------------------------')
for row in sheet_obj.iter_rows(min_row=1,max_row=2,min_col=1,max_col=3):
print(row)
for value in sheet_obj.iter_rows(min_row=1,max_row=2,min_col=1,max_col=3,values_only=True):
print(value)
for colvalue in sheet_obj.iter_cols(min_row=1,max_row=3,min_col=1,max_col=3,values_only=True):
print(colvalue)
import json
revenues={}
for rowvalue in sheet_obj.iter_rows(min_row=2,max_row=4,min_col=1,max_col=3,values_only=True):
#print(value)
rep=rowvalue[0]
rev_details={"Country":rowvalue[1],"Revenue":rowvalue[2]}
revenues[rep]=rev_details
print(json.dumps(revenues,indent=4,sort_keys=True))
Monday, January 9, 2023
Openpyxl part 3,manipulating rows and columns
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment