import random
import math
import xlrd
import xlsFunction
import xlwt
# 输入总量及数量,按照比例进行分配
def allocation(amount, person):
adjustQuo = []
perAmount =sum(person)
for per in person:
data = adjust(amount * per/perAmount)
adjustQuo.append(data)
adjustAgainQuo = adjustAgain(adjustQuo, amount)
return adjustAgainQuo
# 进行数据矫正,不能完全按照比例生成
def adjust(data):
if data > 0 and data <= 0.05:
if random.randint(0, 10) == 0:
data = data - 0.01
elif random.randint(0, 10) == 1:
data = data + 0.01
elif data > 0.05 and data <= 0.1:
if random.randint(0, 1) == 0:
data = data - random.randint(0, 1) * 0.01
else:
data = data + random.randint(0, 1) * 0.01
elif data >0.1 and data <= 1:
if random.randint(0, 1) == 0:
data = data - random.randint(0, 2) * 0.01
else:
data = data + random.randint(0, 2) * 0.01
elif data > 1:
if random.randint(0, 1) == 0:
data = data * (100 + random.randint(0, 4)) * 0.01
else:
data = data * (100 - random.randint(0, 4)) * 0.01
else:
return data
return round(data, 2)
def adjustAgain(array,count):
flag = round(math.fabs((sum(array) - count) * 100), 2)
num = 0
if count > sum(array):
while True:
for i in range(0, len(array)):
array[i] = round(array[i] + 0.01, 2)
print("还在运行")
num = num + 1
if num == flag:
break
if num == flag:
break
elif count < sum(array):
while True:
for i in range(0, len(array)):
# print(i)
if array[i] > 0.01:
array[i] = round(array[i] - 0.01, 2)
print("zai运行")
num = num + 1
if num == flag:
break
if num == flag:
break
else:
return array
return array
# 数组求和
def sum(array):
count = 0
for a in array:
count = count + a
return round(count, 2)
plz = [2678, 3524, 2048, 3116, 3886, 2523, 3618]
fp = allocation(22092, plz)
print(fp)
# # plz = [2559, 1948, 2583, 2354, 1789, 2045, 2901]
# #
# # print(allocation(93, plz))
# # print(allocation(84, plz))
# # print(allocation(79, plz))
#
#
# #===================================================================================================================================================================
#
#
#
# # print("黑堰塘、晏家沟、大明寺、香房、石匣寺、秦蔡沟、庙宇庵")
# # plz2 = [2354, 2559, 1948, 2045, 2583, 1789, 2901]
# #
# # print("========2023年柑橘类==========")
# # print(allocation(84, plz2))
# # print("========2024年柑橘类==========")
# # print(allocation(92, plz2))
# # print("========2023年柑==========")
# # print(allocation(80, plz2))
# # print("========2024年柑==========")
# # print(allocation(88, plz2))
# # print("========2023年柚==========")
# # print(allocation(4, plz2))
# # print("========2024年柚==========")
# # print(allocation(4, plz2))
# # print("=======================================================================================")
# # print("========2023年梨园==========")
# # print(allocation(1703, plz2))
# # print("========2024年梨园==========")
# # print(allocation(1703, plz2))
# # print("========2023年柑橘园==========")
# # print(allocation(313, plz2))
# # print("========2024年柑橘园==========")
# # print(allocation(313, plz2))
# # print("========2023年李子园==========")
# # print(allocation(3, plz2))
# # print("========2024年李子园==========")
# # print(allocation(3, plz2))
# # print("=======================================================================================")
# # print("========2023年中药材面积==========")
# # print(allocation(55, plz2))
# # print("========2024年中药材面积==========")
# # print(allocation(70, plz2))
# # print("========2023年中药材产量==========")
# # print(allocation(25, plz2))
# # print("========2024年中药材产量==========")
# # print(allocation(32, plz2))
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# # # #花生2024
# # # hs1 = [287.63, 322.46, 247.48, 254.77, 309.30, 231.63, 376.73]
# # # #花生2023
# # # hs2 = [301.10, 327.37, 244.20, 253.81, 333.68, 215.24, 356.60]
# # # #烟叶2024
# # # yy1 = [5.24, 5.65, 4.34, 4.55, 5.86, 4.10, 6.26]
# # # #烟叶2023
# # # yy2 = [5.70, 6.25, 4.65, 4.97, 6.12, 4.32, 6.99]
# # # #中药药材2024
# # # zy1 = [8.55, 9.58, 7.39, 7.53, 9.37, 6.54, 11.04]
# # # #中药药材2023
# # # zy2 = [8.83, 9.21, 7.29, 7.90, 9.88, 6.77, 11.12]
# # # #蔬菜与食用菌2024
# # # sc1 = [717.02, 764.13, 570.98, 587.20, 733.23, 524.63, 865.81]
# # # #蔬菜与食用菌2024
# # # sc2 = [619.06, 685.90, 514.04, 577.70, 685.27, 501.82, 776.21]
# #
# #
# #
# #
#设置行数的全局变量
#菜叶类
cyl = [1,2,3,4]
#白菜类
bcl = [5,6]
#甘蓝类
gll = [7,8]
#根茎类
gjl = [9,10,11,12,13]
#瓜菜类
gcl = [14,15,16,17]
#豆类
dl = [18,19,20]
#茄果类
qgl = [21,22,23,24]
#葱蒜类
csl = [25,26,27]
#水生菜类
sscl = [28,29]
#其他类
qtl = [30]
all = [cyl, bcl, gll, gjl, gcl, dl, qgl, csl, sscl, qtl]
#设置组织机构的全局变量
plz = [2559, 1948, 2583, 2354, 1789, 2045, 2901]
lzx = [592, 857, 1482, 1919, 724, 1115, 1966, 1766, 2511, 878, 716, 1729, 6058, 4329, 5516, 1377, 1583, 1222, 1308, 2753, 1836]
# zymj = allocation(55, plz)
# zycl = allocation(25, plz)
# print("========================2023===========================")
# print("中药面积")
# print(zymj)
# print("中药产量")
# print(zycl)
# print("========================2023===========================")
#
#
# zymj = allocation(70, plz)
# zycl = allocation(32, plz)
# print("========================2024===========================")
# print("中药面积")
# print(zymj)
# print("中药产量")
# print(zycl)
# print("========================2024===========================")
organization = plz
# 改原始数据位置
table = xlsFunction.gettable(address="D:/工作/2025/统计云/2025年一季度/过程数据/镇2025.xls", tableName="Sheet1")
# table = xlsFunction.gettable(address="D:/工作/2024/统计工作/农业统计/四季度/镇2024.xls", tableName="Sheet1")
new_workbook = xlwt.Workbook(encoding='utf-8')
worksheet = new_workbook.add_sheet('Sheet1')
for lb in all:
if len(lb) > 1:
count1 = []
count2 = []
for a in lb:
if a == lb[0]:
continue
else:
data1 = xlsFunction.getData(table=table, row=a, col=3)
data2 = xlsFunction.getData(table=table, row=a, col=4)
a1 = allocation(data1, organization)
a2 = allocation(data2, organization)
if len(count1) == 0:
for i in range(0,len(a1)):
count1.append(a1[i])
for i in range(0,len(a2)):
count2.append(a2[i])
# print("========初次录入========")
# print(count1)
# print(count2)
# print("========初次录入========")
else:
for i in range(0,len(count1)):
count1[i] = round(count1[i] + a1[i], 2)
for i in range(0,len(count2)):
count2[i] = round(count2[i] + a2[i], 2)
num = 0
for mj in a1:
worksheet.write(a - 1, num * 2, mj)
num = num + 1
num = 0
for cl in a2:
worksheet.write(a - 1, num * 2 + 1, cl)
num = num + 1
# print("============最终录入=============")
# print(count1)
# print(count2)
# print("============最终录入=============")
amount1 = 0
for flag1 in count1:
amount1 = amount1 + flag1
if xlsFunction.getData(table=table, row=lb[0], col=3) == amount1:
print("面积直接分配相加")
num = 0
for mj in count1:
worksheet.write(lb[0] - 1, num * 2, mj)
num = num + 1
else:
print("面积分配相加再分配")
again = allocation(xlsFunction.getData(table=table, row=lb[0], col=3) - round(amount1, 2), organization)
num = 0
for i in range(0,len(count1)):
worksheet.write(lb[0] - 1, num * 2, count1[i] + again[i])
num = num + 1
amount2 = 0
for flag2 in count2:
amount2 = amount2 + flag2
if xlsFunction.getData(table=table, row=lb[0], col=4) == amount2:
print("产量直接分配相加")
num = 0
for cl in count2:
worksheet.write(lb[0] - 1, num * 2 + 1, cl)
num = num + 1
else:
print("产量分配相加再分配")
again2 = allocation(xlsFunction.getData(table=table, row=lb[0], col=4) - round(amount2, 2), organization)
num = 0
for i in range(0, len(count2)):
worksheet.write(lb[0] - 1, num * 2 + 1, count2[i] + again2[i])
num = num + 1
else:
data1 = xlsFunction.getData(table=table, row=lb[0], col=3)
data2 = xlsFunction.getData(table=table, row=lb[0], col=4)
a1 = allocation(data1, organization)
a2 = allocation(data2, organization)
num = 0
for mj in a1:
worksheet.write(lb[0] - 1, num * 2, mj)
num = num + 1
num = 0
for cl in a2:
worksheet.write(lb[0] - 1, num * 2 + 1 , cl)
num = num + 1
# 改文件生成位置
new_workbook.save('D:/工作/2025/统计云/2025年一季度/过程数据/改--镇2025.xls')