Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 views
in Technique[技术] by (71.8m points)

python - How To Use AutoSum Average In openpyxl

I Am Writing A Code In Python Using openpyxl That Can Fill 10K Rows In An Excel File With Random Number.

The First Column Consists Of Date Between 1985 To 2020.

The Second Column Consists Of Highest Temperature Recorded That Day.

The Third Column Consists Lowest Temperature.

The Fourth Column Needs To Be Filled With Average Of Column B And C Values.

My Problem Is How Can I Find The Average Of Each Row Just Like You Can Select Column D From Start To End And Click On AutoSum > Average. When I Use =AVERAGE(B2:C2) It Gives The Average Of One Row. How Can I Do That.

Code:

#Importing.
import random as rand
import openpyxl as xl

#Loading WorkBook And Sheets.
xl_file = xl.load_workbook("The_file.xlsx")
sheet_1 = xl_file["Sheet1"]

for row in range(2, 10001):

#Selecting The First Column And Rows.
sheet_1["A1"] = "Date"
col_1 = f"A{row}"

#Declaring Variables That Store Random Dates, Month And Years.
random_date = rand.randrange(1, 31) 
random_month = rand.randrange(1 , 12)
random_year = rand.randrange(1985, 2020)

#Preventing From Writing 30 Or 31 February.
if random_month == 2 and random_year / 4 == 0:
    random_date = rand.randrange(1, 29)
    sheet_1[col_1] = f"{random_date}-{random_month}-{random_year}"

#Preventing From Writing 29 In February In Non-Leap Year.
elif random_month == 2 and random_year / 4 != 0:
    random_date = rand.randrange(1, 28)
    sheet_1[col_1] = f"{random_date}-{random_month}-{random_year}"

#Preventing From Not Writing 29, 30 And 31 In Non-February Months.
elif random_month != 2:
    random_date = rand.randrange(1, 31)
    sheet_1[col_1] = f"{random_date-1}-{random_month}-{random_year}"

#Preventing From Writing 31 In April, June, September And November.
elif random_month == 4 or 6 or 9 or 11:
    random_date = rand.randrange(1, 30)
    sheet_1[col_1] = f"{random_date}-{random_month}-{random_year}"

else:
    sheet_1[col_1] = f"{random_date}-{random_month}-{random_year}"

#Selecting Second Column And Second Rows.
sheet_1["B1"] = "Highest Temperature"
col_2 = f"B{row}"

#Setting Range Of Highest Temperature Between 21, 35 Celsius. 
high_temp_range = rand.randrange(21, 35)

#Preventing From Writing Too High Temperature In October, November And February.
if random_month == 10 or 11 or 2:
    high_temp_range = rand.randrange(16, 20)
    sheet_1[col_2] = high_temp_range

#Preventing From Writing High Temperature In December And January.
elif random_month == 12 or 1:
    high_temp_range = rand.randrange(1, 15)
    sheet_1[col_2] = high_temp_range

else:
    sheet_1[col_2] = high_temp_range
#Selecting Third Column And Rows.
sheet_1["C1"] = "Lowest Temprature"
col_3 = f"C{row}"

#Setting Range Of Lowest Tempeature Between 15, 20 Celsius.
low_temp_range = rand.randrange(15, 20)

#Preventing From Writing Too High Temperature In October And November.
if random_month == 10 or 11:
    low_temp_range = rand.randrange(5, 15)
    sheet_1[col_3] = low_temp_range

#Preventing From Writing High Temperature In December And January.
elif random_month == 12 or 1:
    low_temp_range = rand.randrange(-10, 0)
    sheet_1[col_3] = low_temp_range

else:
    sheet_1[col_3] = low_temp_range

#Saving The File.
xl_file.save("Temperature.xlsx")
question from:https://stackoverflow.com/questions/65829451/how-to-use-autosum-average-in-openpyxl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...