1.introduction to python

about python

http://kaiwu.city/index.php/python

books on  python

http://kaiwu.city/index.php/python-book

 

2.install python

python 3.13 you can download from official website

https://www.python.org/downloads/ 

or

 http://kaiwu.city/openfiles/python-3.13.0-amd64.exe 

 

http://kaiwu.city/index.php/python-vscode

 

3.python and Excel

    http://kaiwu.city/openfiles/hotel50python_EN.xlsx 

  http://kaiwu.city/openfiles/python_excel50hotel_EN.ipynb 

 

import openpyxl as xl;

# be sure to modify the folder where the files are stored. 
# The names of the folders should not contain non-English characters and spaces.
filename ="D:/tdata/hotel50/hotel50python_EN.xlsx"
wb1 = xl.load_workbook(filename)
ws1 = wb1.worksheets[0]
mr = ws1.max_row
mc = ws1.max_column

# load the workbook
from openpyxl import Workbook

# create a new workbook
wb2 = Workbook()
ws2 =  wb2.active

# The "i" represents the row number. 
# create a workbook for the data from row 2 to row 50 respectively.
for i in range (2, mr + 1):
    #The name of the new worksheet: Hotel Name
    ws2.title = ws1.cell(row = i, column = 3).value
    
     # j is the column number from 1 to 8:id, hotelid,Hotel Name,Rating,City,Address,Opening Year,Number of Rooms
    
    for j in range (1, mc):
        #Copy the first row of the source data, which is the header row, and use it as the first row of the new worksheet.
        ws2.cell(row = 1, column = j).value = ws1.cell(row = 1, column = j).value
        
        # Copy the data in the i-th row of the source data and use it as the 2nd row of the new worksheet.
        ws2.cell(row = 2, column = j).value = ws1.cell(row = i, column = j).value

    # Save the created Excel workbook.
    wb2.save("D:/tdata/hotel50/" + ws2.title + ".xlsx")
    # Output the result to indicate the work progress, specifically which worksheet has been completed already.

    print('save No. %d Excel file。' % (i-1))