Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
openpyxl
#1
Hello everyone!

I am writing a code which will sort names by the first letter and move them from the first column where they are initially written to 2nd column if it's letter "A", to the 3rd - if "B", to the 4th - if "C", and so on.

This is my code:
from openpyxl import *

wb = load_workbook('D:\Studying\Excel\excelnames.xlsx')
sheet = wb.sheetnames

curSheet = wb[sheet[0]]

x=2
while x<=30:
    vvv=curSheet.cell(row=x, column=1)
    if vvv.value[0] == 'А':
        curSheet.cell(row=x, column=2, value=vvv.value)
    if vvv.value[0] == 'Б':
        curSheet.cell(row=x, column=3, value=vvv.value)
    if vvv.value[0] == 'В':
        curSheet.cell(row=x, column=4, value=vvv.value)
    x += 1

wb.save('D:\Studying\Excel\excelnames.xlsx')
As a result I've got this:
[Image: joYPEL8pIr2UI1UXT1Hr11aTKEK2SC]
But I can't figure out how not just to shift cells right to needed column but also move them up to the row 2.
If anyone could help me with this, I would be very grateful.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020