partReport.py 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085
  1. import pandas as pd
  2. import numpy as np
  3. import time
  4. import os
  5. from docx.shared import Inches
  6. from docx.shared import Pt, RGBColor
  7. from docx import Document
  8. from docx.enum.table import WD_TABLE_ALIGNMENT, WD_CELL_VERTICAL_ALIGNMENT
  9. from docx.oxml.ns import qn
  10. from docx.enum.text import WD_ALIGN_PARAGRAPH
  11. from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
  12. import report
  13. from datetime import datetime
  14. import chinese_converter # 需要安装这个库来转换数字为中文大写
  15. import cn2an
  16. # 频度信息表生成
  17. def makeInfoTable(data, doc):
  18. rows = (int(len(data.columns) / 6) + 1)
  19. columnsList = np.arange(0, rows * 6, 6)
  20. dataList = []
  21. for i in columnsList:
  22. res = data.iloc[:, i:i + 6]
  23. res = res.reset_index()
  24. dataList.append(res)
  25. table_f_2 = doc.add_table(rows=rows * 6, cols=7, style='Light Shading Accent 1')
  26. for i, row in enumerate(table_f_2.rows):
  27. for j, cell in enumerate(row.cells):
  28. # 获取单元格中的段落对象
  29. paragraph = cell.paragraphs[0]
  30. if i == columnsList[0]:
  31. # 第一行 显示前6个指标的列名
  32. if len(dataList[0].columns) > j:
  33. r = paragraph.add_run(dataList[0].columns[j])
  34. r.font.bold = True
  35. r.font.size = Pt(10.5)
  36. r.font.name = 'Times New Roman'
  37. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  38. else:
  39. paragraph.add_run('')
  40. elif len(columnsList) > 1 and i > columnsList[0] and i < columnsList[1]:
  41. if len(dataList[0].columns) > j:
  42. r = paragraph.add_run(str(dataList[0].iloc[i - 1, j]))
  43. r.font.size = Pt(10.5)
  44. r.font.name = 'Times New Roman'
  45. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  46. else:
  47. paragraph.add_run('')
  48. elif i == columnsList[1]:
  49. # 第6行 显示前6个指 标的列名
  50. if len(dataList[1].columns) > j:
  51. r = paragraph.add_run(dataList[1].columns[j])
  52. r.font.bold = True
  53. r.font.size = Pt(10.5)
  54. r.font.name = 'Times New Roman'
  55. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  56. else:
  57. paragraph.add_run('')
  58. elif len(columnsList) > 2 and i > columnsList[1] and i < columnsList[2]:
  59. if len(dataList[1].columns) > j:
  60. r = paragraph.add_run(str(dataList[1].iloc[i - 7, j]))
  61. r.font.size = Pt(10.5)
  62. r.font.name = 'Times New Roman'
  63. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  64. else:
  65. paragraph.add_run('')
  66. elif i == columnsList[2]:
  67. # 第6*2行 显示前6个指 标的列名
  68. if len(dataList[2].columns) > j:
  69. r = paragraph.add_run(dataList[2].columns[j])
  70. r.font.bold = True
  71. r.font.size = Pt(10.5)
  72. r.font.name = 'Times New Roman'
  73. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  74. else:
  75. paragraph.add_run('')
  76. elif len(columnsList) > 3 and i > columnsList[2] and i < columnsList[3]:
  77. if len(dataList[2].columns) > j:
  78. r = paragraph.add_run(str(dataList[2].iloc[i - 13, j]))
  79. r.font.size = Pt(10.5)
  80. r.font.name = 'Times New Roman'
  81. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  82. else:
  83. paragraph.add_run('')
  84. elif i == columnsList[3]:
  85. # 第6*3行 显示前6个指 标的列名
  86. if len(dataList[3].columns) > j:
  87. r = paragraph.add_run(dataList[3].columns[j])
  88. r.font.bold = True
  89. r.font.size = Pt(10.5)
  90. r.font.name = 'Times New Roman'
  91. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  92. else:
  93. paragraph.add_run('')
  94. elif len(columnsList) > 4 and i > columnsList[3] and i < columnsList[4]:
  95. if len(dataList[3].columns) > j:
  96. r = paragraph.add_run(str(dataList[3].iloc[i - 19, j]))
  97. r.font.size = Pt(10.5)
  98. r.font.name = 'Times New Roman'
  99. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  100. else:
  101. paragraph.add_run('')
  102. elif i == columnsList[4]:
  103. # 第6*4行 显示前6个指 标的列名
  104. if len(dataList[4].columns) > j:
  105. r = paragraph.add_run(dataList[4].columns[j])
  106. r.font.bold = True
  107. r.font.size = Pt(10.5)
  108. r.font.name = 'Times New Roman'
  109. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  110. else:
  111. paragraph.add_run('')
  112. elif len(columnsList) > 5 and i > columnsList[4] and i < columnsList[5]:
  113. if len(dataList[4].columns) > j:
  114. r = paragraph.add_run(str(dataList[4].iloc[i - 25, j]))
  115. r.font.size = Pt(10.5)
  116. r.font.name = 'Times New Roman'
  117. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  118. else:
  119. paragraph.add_run('')
  120. elif i == columnsList[5]:
  121. # 第6*5行 显示前6个指 标的列名
  122. if len(dataList[5].columns) > j:
  123. r = paragraph.add_run(dataList[5].columns[j])
  124. r.font.bold = True
  125. r.font.size = Pt(10.5)
  126. r.font.name = 'Times New Roman'
  127. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  128. else:
  129. paragraph.add_run('')
  130. elif len(columnsList) > 6 and i > columnsList[5] and i < columnsList[6]:
  131. if len(dataList[5].columns) > j:
  132. r = paragraph.add_run(str(dataList[5].iloc[i - 31, j]))
  133. r.font.size = Pt(10.5)
  134. r.font.name = 'Times New Roman'
  135. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  136. else:
  137. paragraph.add_run('')
  138. elif i == columnsList[6]:
  139. # 第6*6行 显示前6个指 标的列名
  140. if len(dataList[6].columns) > j:
  141. r = paragraph.add_run(dataList[6].columns[j])
  142. r.font.bold = True
  143. r.font.size = Pt(10.5)
  144. r.font.name = 'Times New Roman'
  145. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  146. else:
  147. paragraph.add_run('')
  148. elif len(columnsList) > 7 and i > columnsList[6] and i < columnsList[7]:
  149. if len(dataList[6].columns) > j:
  150. r = paragraph.add_run(str(dataList[6].iloc[i - 37, j]))
  151. r.font.size = Pt(10.5)
  152. r.font.name = 'Times New Roman'
  153. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  154. else:
  155. paragraph.add_run('')
  156. elif i == columnsList[7]:
  157. # 第6*7行 显示前6个指 标的列名
  158. if len(dataList[7].columns) > j:
  159. r = paragraph.add_run(dataList[7].columns[j])
  160. r.font.bold = True
  161. r.font.size = Pt(10.5)
  162. r.font.name = 'Times New Roman'
  163. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  164. else:
  165. paragraph.add_run('')
  166. elif len(columnsList) >= 8 and i > columnsList[7] and i < columnsList[8]:
  167. if len(dataList[7].columns) > j:
  168. r = paragraph.add_run(str(dataList[7].iloc[i - 43, j]))
  169. r.font.size = Pt(10.5)
  170. r.font.name = 'Times New Roman'
  171. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  172. else:
  173. paragraph.add_run('')
  174. elif i == columnsList[8]:
  175. if len(dataList[8].columns) > j:
  176. # 第6*8行 显示前6个指 标的列名
  177. r = paragraph.add_run(dataList[8].columns[j])
  178. r.font.bold = True
  179. r.font.size = Pt(10.5)
  180. r.font.name = 'Times New Roman'
  181. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  182. else:
  183. paragraph.add_run('')
  184. elif len(columnsList) >= 9 and i > columnsList[8] and i < columnsList[9]:
  185. if len(dataList[8].columns) > j:
  186. r = paragraph.add_run(str(dataList[8].iloc[i - 49, j]))
  187. r.font.size = Pt(10.5)
  188. r.font.name = 'Times New Roman'
  189. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  190. else:
  191. paragraph.add_run('')
  192. elif i == columnsList[9]:
  193. # 第6*9行 显示前6个指 标的列名
  194. if len(dataList[9].columns) > j:
  195. r = paragraph.add_run(dataList[9].columns[j])
  196. r.font.bold = True
  197. r.font.size = Pt(10.5)
  198. r.font.name = 'Times New Roman'
  199. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  200. else:
  201. paragraph.add_run('')
  202. elif len(columnsList) >= 10 and i > columnsList[9] and i <= 60:
  203. if len(dataList[9].columns) > j:
  204. r = paragraph.add_run(str(dataList[9].iloc[i - 55, j]))
  205. r.font.size = Pt(10.5)
  206. r.font.name = 'Times New Roman'
  207. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  208. else:
  209. paragraph.add_run('')
  210. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  211. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  212. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  213. # 生成子报告: 物理指标 常规养分指标 一般化学性指标 重金属指标
  214. indexClassificationList = {
  215. '物理指标': ['pH', '土壤质地', '土壤容重1(g/cm³)', '土壤容重2(g/cm³)', '土壤容重3(g/cm³)', '土壤容重4(g/cm³)', '土壤容重平均值(g/cm³)',
  216. '2~0.2mm颗粒含量', '0.2~0.02mm颗粒含量', '0.02~0.002mm颗粒含量', '0.002mm以下颗粒含量', '水稳>5mm(%)', '水稳3mm~5mm(%)',
  217. '水稳2mm~3mm(%)', '水稳1mm~2mm(%)', '水稳0.5mm~1mm(%)', '水稳0.25mm~0.5mm(%)', '水稳性大团聚体总和(%)', '洗失量(吸管法需填)', '风干试样含水量(分析基)'],
  218. '常规养分指标': ['pH','有机质', '全氮', '全磷', '全钾', '有效磷', '速效钾', '有效硫', '有效硼', '有效铁', '有效锰', '有效铜', '有效锌', '有效钼', '有效硅', '缓效钾'],
  219. '一般化学性指标': ['pH','阳离子交换量', '交换性盐基总量', '交换性钙', '交换性镁', '交换性钠', '交换性钾', '全盐量', '电导率',
  220. '水溶性Na⁺含量', '水溶性K⁺含量', '水溶性Ca²⁺含量', '水溶性Mg²⁺含量', '水溶性Cl⁻含量', '水溶性CO₃²⁻含量','水溶性HCO₃⁻含量',
  221. '水溶性SO₄²⁻含量', '离子总量', '碳酸钙'],
  222. '重金属指标': ['pH', '总汞', '总砷', '总铅', '总镉', '总铬', '总镍']
  223. }
  224. # 生成物理指标审核报告
  225. def getphysicsReport(originData,data,type, changeFileUrl, saveFileUrl, check_1_data,
  226. check_3_data,
  227. check_5_data ,
  228. check_8_data, # 样品编号替换为编号
  229. check_10_data,
  230. check_12_data,
  231. check_14_data ):
  232. """
  233. :param type: 指标类型
  234. :param changeFileUrl: 选择的数据文件路径
  235. :param saveFileUrl: 保存的文件路径
  236. :param check_1_data: 土壤容重数据
  237. :param check_3_data: 水稳审核数据
  238. :param check_5_data: 盐离子数据
  239. :param check_8_data: 水溶性离子数据
  240. :param check_10_data: 有机质及氮磷钾数据
  241. :param check_12_data: 有效养分数据
  242. :param check_14_data: 重金属数据
  243. :return:
  244. """
  245. # 生成报告
  246. name = os.path.basename(changeFileUrl)
  247. n = name.split('.')
  248. areaName = n[0].replace('数据', '')
  249. # 生成一个新的文件夹用于存放审核报告相关的数据
  250. nowTime = time.strftime("%Y-%m-%d %H时%M分%S秒", time.localtime())
  251. dir_name = f'{areaName}-{type}数据审核报告'
  252. mkdir_path = saveFileUrl + '/' + dir_name + nowTime
  253. if not os.path.exists(mkdir_path):
  254. os.mkdir(mkdir_path)
  255. # 获取相应指标数据
  256. physicsData = data[indexClassificationList[type]]
  257. physicsDataNum = originData[indexClassificationList[type]]
  258. report.getFrequencyImage(physicsData, mkdir_path)
  259. physicsData['序号'] = data['序号']
  260. physicsData['原样品编号'] = data['原样品编号']
  261. physicsData['样品编号'] = data['样品编号']
  262. physicsData['地理位置'] = data['地理位置']
  263. physicsData['母质'] = data['母质']
  264. physicsData['土壤类型'] = data['土壤类型']
  265. physicsData['土地利用类型'] = data['土地利用类型']
  266. physicsData['土壤质地'] = data['土壤质地']
  267. physicsDataNum['序号'] = originData['序号']
  268. physicsDataNum['原样品编号'] = originData['原样品编号']
  269. physicsDataNum['样品编号'] = originData['样品编号']
  270. physicsDataNum['地理位置'] = originData['地理位置']
  271. physicsDataNum['母质'] = originData['母质']
  272. physicsDataNum['土壤类型'] = originData['土壤类型']
  273. physicsDataNum['土地利用类型'] = originData['土地利用类型']
  274. physicsDataNum['土壤质地'] = originData['土壤质地']
  275. physicsDataNum['原样品编号'] = originData['原样品编号'].astype(str)
  276. # 生成相应审核报告
  277. # 根据选择的路径读取数据
  278. physicsData['原样品编号'] = physicsData['原样品编号'].astype(str)
  279. # todo 有数据后这里去掉注释
  280. # checkData = pd.read_excel(changeFileUrl, sheet_name='检测方法')
  281. # 上面这个地址,可以传递给函数中,用于保存表格和图片
  282. # 调用函数 开始生成报告相关内容
  283. # 表1相关数据
  284. typeData = report.getSimpleNum(physicsData)
  285. lenNum_1 = len(typeData['sData'])
  286. lenNum_1_f = len(typeData['allData'])
  287. table_1_data = pd.DataFrame({
  288. '类型': typeData['sData'].index,
  289. '数量': typeData['sData'],
  290. '合计': [typeData['sData'].sum() for _ in range(lenNum_1)]
  291. })
  292. # 表2数据
  293. table_2_data = report.getDataComplete(physicsData)
  294. table_2_data = table_2_data.reset_index()
  295. table_2_data.columns = ['指标名称', '实测数量', '应测数量']
  296. # 表3数据
  297. # table_3_data = report.checkMethod(checkData, mkdir_path)
  298. # 数据修约 表4
  299. report.getNum(physicsDataNum, mkdir_path)
  300. # 数据填报项审核 表5
  301. report.dataReportResult(physicsData, mkdir_path)
  302. # 表6数据 土壤质地类型不一致
  303. middData = physicsData[['原样品编号', '样品编号']].astype(str)
  304. middData['编号'] = middData['原样品编号']
  305. del middData['原样品编号']
  306. check_1_data = pd.merge(check_1_data, middData, how='left', on='编号')
  307. check_1_data = check_1_data.replace(np.nan, '')
  308. typeNotSame = check_1_data[check_1_data['土壤质地'] != check_1_data['土壤类型(判断)']]
  309. table_6_data = typeNotSame[['编号', '样品编号', '土壤质地', '土壤类型(判断)']]
  310. allNeedData = pd.DataFrame({})
  311. allNeedData['原样品编号'] = check_1_data['编号']
  312. getSimpleDataNumber = pd.merge(allNeedData, physicsData[['原样品编号', '样品编号']], how='left', on="原样品编号")
  313. allNeedData['样品编号'] = getSimpleDataNumber['样品编号']
  314. allNeedData['土地利用类型'] = check_1_data['土地利用类型']
  315. allNeedData['审核结果'] = check_1_data['审核结果'] + check_3_data['审核结果']
  316. allNeedData['外业'] = ['' for _ in range(len(check_1_data))]
  317. table_7_data = allNeedData[allNeedData['审核结果'] != '']
  318. del table_7_data['审核结果']
  319. # 写进表格
  320. with pd.ExcelWriter(f'{mkdir_path}/超阈值样品统计表.xlsx', engine='openpyxl') as writer:
  321. table_7_data.to_excel(writer, index=False, sheet_name='超阈值数据')
  322. # 表8数据
  323. table_8_data = report.getPHData(physicsData, mkdir_path)
  324. # 表13 所有存疑数据
  325. with pd.ExcelWriter(f'{mkdir_path}/数据审核过程存疑数据一览表.xlsx', engine='openpyxl') as writer:
  326. allNeedData[allNeedData['审核结果'] != ''].to_excel(writer, index=False, sheet_name='存疑数据')
  327. # 附表: 频度分析图
  328. # report.getFrequencyImage(physicsData, mkdir_path)
  329. table_f_2_data = report.getFrequencyInformation(data, mkdir_path)
  330. # 新建一个文档
  331. doc = Document()
  332. # 添加标题
  333. doc.add_heading(f"{areaName}第三次全国土壤普查数据审核报告", level=0)
  334. # 添加一级标题
  335. doc.add_heading('一、数据完整性审核', level=1)
  336. doc.add_heading('1、土地利用类型与检测指标符合性审核', level=2)
  337. # 插入表格1
  338. paragraph_1 = doc.add_paragraph()
  339. paragraph_1.add_run(f"表1:{areaName}三普样品数量统计表(表层)").bold = True
  340. # 设置居中
  341. paragraph_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
  342. table_1 = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
  343. table_1.alignment = WD_TABLE_ALIGNMENT.CENTER
  344. # 遍历表格 插入数据
  345. # 遍历表格的所有单元格,并填充内容
  346. for i, row in enumerate(table_1.rows):
  347. for j, cell in enumerate(row.cells):
  348. # 获取单元格中的段落对象
  349. paragraph = cell.paragraphs[0]
  350. if i == 0:
  351. r = paragraph.add_run(str(table_1_data.columns[j]))
  352. r.font.bold = True
  353. else:
  354. r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
  355. r.font.size = Pt(10.5)
  356. r.font.name = 'Times New Roman'
  357. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  358. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  359. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  360. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  361. # 合并单元格 合并第3列的第二行和第三行
  362. if lenNum_1 > 1:
  363. table_1.cell(2, 2).text = ''
  364. table_1.cell(1, 2).merge(table_1.cell(2, 2))
  365. ############test##############
  366. doc.add_heading('2、指标名称与实际检测样品数量完整性审核', level=2)
  367. # 插入表格2
  368. paragraph_2 = doc.add_paragraph()
  369. paragraph_2.add_run(f'表2:{areaName}指标名称与实际检测样品数量统计表').bold = True
  370. table_2 = doc.add_table(rows=len(table_2_data) + 1, cols=3, style='Light Shading Accent 1')
  371. paragraph_2.alignment = WD_ALIGN_PARAGRAPH.CENTER
  372. table_2.alignment = WD_TABLE_ALIGNMENT.CENTER
  373. for i, row in enumerate(table_2.rows):
  374. for j, cell in enumerate(row.cells):
  375. # 获取单元格中的段落对象
  376. paragraph = cell.paragraphs[0]
  377. if i == 0:
  378. r = paragraph.add_run(str(table_2_data.columns[j]))
  379. r.font.bold = True
  380. else:
  381. r = paragraph.add_run(str(table_2_data.iloc[i - 1, j]))
  382. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  383. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  384. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  385. r.font.size = Pt(10.5)
  386. r.font.name = 'Times New Roman'
  387. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  388. doc.add_heading('二、数据规范性审核', level=1)
  389. doc.add_heading('1、数据填报规范性审核', level=2)
  390. # 插入表3
  391. paragraph_3 = doc.add_paragraph()
  392. paragraph_3.add_run(f'表3:{areaName}土壤检测数据检测方法填报审核结果表').bold = True
  393. # table_3 = doc.add_table(rows=2, cols=2)
  394. paragraph_3.alignment = WD_ALIGN_PARAGRAPH.CENTER
  395. # table_3.alignment = WD_TABLE_ALIGNMENT.CENTER
  396. # 写入数据 这里数据写不下 嵌入链接
  397. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:检测方法审核结果.xlsx', level=4)
  398. doc.add_heading('2、数值修约规范性审核', level=2)
  399. # 插入表4
  400. paragraph_4 = doc.add_paragraph()
  401. paragraph_4.add_run(f'表4:{areaName}土壤检测数据数值修约结果表').bold = True
  402. # table_4 = doc.add_table(rows=2, cols=2)
  403. paragraph_4.alignment = WD_ALIGN_PARAGRAPH.CENTER
  404. # table_4.alignment = WD_TABLE_ALIGNMENT.CENTER
  405. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数值修约审核.xlsx', level=4)
  406. # 填入数据 这里数据也放不下 嵌入链接
  407. doc.add_heading('3、数据未检出的填报规范性审核', level=2)
  408. # 插入表5
  409. paragraph_5 = doc.add_paragraph()
  410. paragraph_5.add_run(f'表5:{areaName}土壤检测数据未检出项填报审核结果表').bold = True
  411. # table_5 = doc.add_table(rows=2, cols=2)
  412. paragraph_5.alignment = WD_ALIGN_PARAGRAPH.CENTER
  413. # table_5.alignment = WD_TABLE_ALIGNMENT.CENTER
  414. # 写入数据 这里数据也放不下 嵌入链接
  415. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据填报项审核结果.xlsx', level=4)
  416. doc.add_heading('4、土壤质地填报规范性审核', level=2)
  417. # 插入表6
  418. paragraph_6 = doc.add_paragraph()
  419. paragraph_6.add_run(f'表6:{areaName}土壤质地填报审核结果表').bold = True
  420. table_6 = doc.add_table(rows=len(table_6_data) + 1, cols=4, style='Light Shading Accent 1')
  421. paragraph_6.alignment = WD_ALIGN_PARAGRAPH.CENTER
  422. table_6.alignment = WD_TABLE_ALIGNMENT.CENTER
  423. # 提取结果表中数据
  424. # 写入数据 土壤质地类型不一致的数据提取出来
  425. for i, row in enumerate(table_6.rows):
  426. for j, cell in enumerate(row.cells):
  427. # 获取单元格中的段落对象
  428. paragraph = cell.paragraphs[0]
  429. if i == 0:
  430. r = paragraph.add_run(str(table_6_data.columns[j]))
  431. r.font.bold = True
  432. else:
  433. r = paragraph.add_run(str(table_6_data.iloc[i - 1, j]))
  434. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  435. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  436. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  437. r.font.size = Pt(10.5)
  438. r.font.name = 'Times New Roman'
  439. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  440. doc.add_heading('三、数据合理性审核', level=1)
  441. doc.add_heading('1、阈值法审核', level=2)
  442. # 插入表格
  443. paragraph_7 = doc.add_paragraph()
  444. paragraph_7.add_run(f'表7:{areaName}土壤检测数据超阈值样品统计表').bold = True
  445. # table_7 = doc.add_table(rows=2, cols=2)
  446. # paragraph_7.alignment = WD_ALIGN_PARAGRAPH.CENTER
  447. # table_7.alignment = WD_TABLE_ALIGNMENT.CENTER
  448. # 写入数据 点击查看数据 这里也不一定写的下 最好是嵌入链接
  449. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:超阈值样品统计表.xlsx', level=4)
  450. # todo 合并所有数据 审核结果不为空的数据 写入表格保存到指定文件夹
  451. doc.add_heading('2、极值法审核', level=2)
  452. doc.add_heading('(1)pH', level=3)
  453. # 插入ph分布图
  454. if os.path.isfile(f'{mkdir_path}/PH值分布图.png'):
  455. doc.add_picture(f'{mkdir_path}/PH值分布图.png', width=Inches(6.0))
  456. paragraph_t_1 = doc.add_paragraph()
  457. paragraph_t_1.add_run(f'图1:pH值分布情况').bold = True
  458. paragraph_t_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
  459. # 插入频度统计表
  460. paragraph_8 = doc.add_paragraph()
  461. paragraph_8.add_run('表8:pH数据统计表').bold = True
  462. table_8 = doc.add_table(rows=6, cols=2, style='Light Shading Accent 1')
  463. t_8 = table_8_data['频度分析']
  464. t_8 = t_8.reset_index()
  465. t_8.columns = ['指标', '数据']
  466. paragraph_8.alignment = WD_ALIGN_PARAGRAPH.CENTER
  467. table_8.alignment = WD_TABLE_ALIGNMENT.CENTER
  468. for i, row in enumerate(table_8.rows):
  469. for j, cell in enumerate(row.cells):
  470. # 获取单元格中的段落对象
  471. paragraph = cell.paragraphs[0]
  472. if i == 0:
  473. r = paragraph.add_run(str(t_8.columns[j]))
  474. r.font.bold = True
  475. else:
  476. r = paragraph.add_run(str(t_8.iloc[i - 1, j]))
  477. r.font.size = Pt(10.5)
  478. r.font.name = 'Times New Roman'
  479. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  480. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  481. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  482. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  483. # 插入异常数据提取表格 todo 这里数据多的话也可能写不下 最好是嵌入一下
  484. if not table_8_data['异常数据'].empty:
  485. paragraph_9 = doc.add_paragraph()
  486. paragraph_9.add_run('表9:pH异常数据统计表').bold = True
  487. table_9 = doc.add_table(rows=len(table_8_data['异常数据']) + 1, cols=6, style='Light Shading Accent 1')
  488. t_9 = table_8_data['异常数据']
  489. paragraph_9.alignment = WD_ALIGN_PARAGRAPH.CENTER
  490. table_9.alignment = WD_TABLE_ALIGNMENT.CENTER
  491. for i, row in enumerate(table_9.rows):
  492. for j, cell in enumerate(row.cells):
  493. # 获取单元格中的段落对象
  494. paragraph = cell.paragraphs[0]
  495. if i == 0:
  496. r = paragraph.add_run(str(t_9.columns[j]))
  497. r.font.bold = True
  498. else:
  499. r = paragraph.add_run(str(t_9.iloc[i - 1, j]))
  500. r.font.size = Pt(10.5)
  501. r.font.name = 'Times New Roman'
  502. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  503. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  504. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  505. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  506. doc.add_heading('四、审核存疑数据', level=1)
  507. paragraph_12 = doc.add_paragraph()
  508. paragraph_12.add_run(f'表10:数据审核过程存疑数据一览表').bold = True
  509. paragraph_12.alignment = WD_ALIGN_PARAGRAPH.CENTER
  510. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:物理指标数据审核过程存疑数据一览表.xlsx', level=4)
  511. doc.add_heading('五、附表', level=1)
  512. doc.add_heading('附表1:某区三普样品数量统计表(表层)', level=2)
  513. # 插入附表1
  514. table_1_f = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
  515. table_1_f.alignment = WD_TABLE_ALIGNMENT.CENTER
  516. # 遍历表格 插入数据
  517. # 遍历表格的所有单元格,并填充内容
  518. for i, row in enumerate(table_1_f.rows):
  519. for j, cell in enumerate(row.cells):
  520. # 获取单元格中的段落对象
  521. paragraph = cell.paragraphs[0]
  522. if i == 0:
  523. r = paragraph.add_run(str(table_1_data.columns[j]))
  524. r.font.bold = True
  525. else:
  526. r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
  527. r.font.size = Pt(10.5)
  528. r.font.name = 'Times New Roman'
  529. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  530. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  531. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  532. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  533. # 合并单元格 合并第3列的第二行和第三行
  534. if lenNum_1 > 1:
  535. table_1_f.cell(2, 2).text = ''
  536. table_1_f.cell(1, 2).merge(table_1_f.cell(2, 2))
  537. doc.add_heading('附表2:各指标频度分析表', level=2)
  538. # 插入表格 写入数据
  539. table_f_2_data = table_f_2_data.replace(np.nan, '')
  540. makeInfoTable(table_f_2_data, doc)
  541. # table_f_2 = doc.add_table(rows=len(table_f_2_data) + 1, cols=6, style='Light Shading Accent 1')
  542. # for i, row in enumerate(table_f_2.rows):
  543. # for j, cell in enumerate(row.cells):
  544. # # 获取单元格中的段落对象
  545. # paragraph = cell.paragraphs[0]
  546. # if i == 0:
  547. # r = paragraph.add_run(str(table_f_2_data.columns[j]))
  548. # r.font.bold = True
  549. # else:
  550. # r = paragraph.add_run(str(table_f_2_data.iloc[i - 1, j]))
  551. # r.font.size = Pt(10.5)
  552. # r.font.name = 'Times New Roman'
  553. # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  554. # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  555. # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  556. # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  557. # doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:频度分析表.xlsx', level=4)
  558. doc.add_heading('附表3:各指标频度分析图', level=2)
  559. # 插入频度信息的图形
  560. if os.path.isfile(f'{mkdir_path}/0.002mm以下颗粒含量分析图.png'):
  561. doc.add_picture(f'{mkdir_path}/0.002mm以下颗粒含量分析图.png', width=Inches(6.0))
  562. if os.path.isfile(f'{mkdir_path}/0.02~0.002mm颗粒含量分析图.png.png'):
  563. doc.add_picture(f'{mkdir_path}/0.02~0.002mm颗粒含量分析图.png', width=Inches(6.0))
  564. if os.path.isfile(f'{mkdir_path}/0.2~0.02mm颗粒含量分析图.png'):
  565. doc.add_picture(f'{mkdir_path}/0.2~0.02mm颗粒含量分析图.png', width=Inches(6.0))
  566. if os.path.isfile(f'{mkdir_path}/2~0.2mm颗粒含量分析图.png'):
  567. doc.add_picture(f'{mkdir_path}/2~0.2mm颗粒含量分析图.png', width=Inches(6.0))
  568. if os.path.isfile(f'{mkdir_path}/pH分析图.png'):
  569. doc.add_picture(f'{mkdir_path}/pH分析图.png', width=Inches(6.0))
  570. if os.path.isfile(f'{mkdir_path}/风干试样含水量(分析基)分析图.png'):
  571. doc.add_picture(f'{mkdir_path}/风干试样含水量(分析基)分析图.png', width=Inches(6.0))
  572. if os.path.isfile(f'{mkdir_path}/洗失量(吸管法需填)分析图.png'):
  573. doc.add_picture(f'{mkdir_path}/洗失量(吸管法需填)分析图.png', width=Inches(6.0))
  574. if os.path.isfile(f'{mkdir_path}/土壤容重1分析图.png'):
  575. doc.add_picture(f'{mkdir_path}/土壤容重1分析图.png', width=Inches(6.0))
  576. if os.path.isfile(f'{mkdir_path}/土壤容重2分析图.png'):
  577. doc.add_picture(f'{mkdir_path}/土壤容重2分析图.png', width=Inches(6.0))
  578. if os.path.isfile(f'{mkdir_path}/土壤容重3分析图.png'):
  579. doc.add_picture(f'{mkdir_path}/土壤容重3分析图.png', width=Inches(6.0))
  580. if os.path.isfile(f'{mkdir_path}/土壤容重4分析图.png'):
  581. doc.add_picture(f'{mkdir_path}/土壤容重4分析图.png', width=Inches(6.0))
  582. if os.path.isfile(f'{mkdir_path}/土壤容重平均值分析图.png'):
  583. doc.add_picture(f'{mkdir_path}/土壤容重平均值分析图.png', width=Inches(6.0))
  584. if os.path.isfile(f'{mkdir_path}/水稳0.5mm~1mm分析图.png'):
  585. doc.add_picture(f'{mkdir_path}/水稳0.5mm~1mm分析图.png', width=Inches(6.0))
  586. if os.path.isfile(f'{mkdir_path}/水稳0.25mm~0.5mm分析图.png'):
  587. doc.add_picture(f'{mkdir_path}/水稳0.25mm~0.5mm分析图.png', width=Inches(6.0))
  588. if os.path.isfile(f'{mkdir_path}/水稳1mm~2mm分析图.png'):
  589. doc.add_picture(f'{mkdir_path}/水稳1mm~2mm分析图.png', width=Inches(6.0))
  590. if os.path.isfile(f'{mkdir_path}/水稳2mm~3mm分析图.png'):
  591. doc.add_picture(f'{mkdir_path}/水稳2mm~3mm分析图.png', width=Inches(6.0))
  592. if os.path.isfile(f'{mkdir_path}/水稳3mm~5mm分析图.png'):
  593. doc.add_picture(f'{mkdir_path}/水稳3mm~5mm分析图.png', width=Inches(6.0))
  594. if os.path.isfile(f'{mkdir_path}/水稳5mm分析图.png'):
  595. doc.add_picture(f'{mkdir_path}/水稳5mm分析图.png', width=Inches(6.0))
  596. doc.add_heading('附表4:数值修约标准', level=2)
  597. # 读取数据 插入表格 写入数据
  598. numData = pd.read_excel('./img/数值修约要求.xlsx', sheet_name='Sheet1')
  599. table_2_f = doc.add_table(rows=len(numData) + 1, cols=2, style='Light Shading Accent 1')
  600. table_2_f.alignment = WD_TABLE_ALIGNMENT.CENTER
  601. for i, row in enumerate(table_2_f.rows):
  602. for j, cell in enumerate(row.cells):
  603. # 获取单元格中的段落对象
  604. paragraph = cell.paragraphs[0]
  605. if i == 0:
  606. r = paragraph.add_run(str(numData.columns[j]))
  607. r.font.bold = True
  608. else:
  609. r = paragraph.add_run(str(numData.iloc[i - 1, j]))
  610. r.font.size = Pt(10.5)
  611. r.font.name = 'Times New Roman'
  612. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  613. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  614. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  615. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  616. # 处理样式 遍历所有的段落 修改字体
  617. # 遍历并打印每个段落的文本
  618. paragraphs = doc.paragraphs
  619. for paragraph in paragraphs:
  620. for run in paragraph.runs:
  621. run.font.color.rgb = RGBColor(0, 0, 0)
  622. run.font.name = 'Times New Roman'
  623. run.font.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  624. # 保存Word文档
  625. doc.save(f'{mkdir_path}/{areaName}-{type}审核报告.docx')
  626. # 生成常规养分指标审核报告
  627. def getConventionalNutrientIndicators(originData,data,type, changeFileUrl, saveFileUrl, check_1_data,
  628. check_3_data,
  629. check_5_data,
  630. check_8_data, # 样品编号替换为编号
  631. check_10_data,
  632. check_12_data,
  633. check_14_data ):
  634. # 生成报告
  635. name = os.path.basename(changeFileUrl)
  636. n = name.split('.')
  637. areaName = n[0].replace('数据', '')
  638. # 生成一个新的文件夹用于存放审核报告相关的数据
  639. nowTime = time.strftime("%Y-%m-%d %H时%M分%S秒", time.localtime())
  640. dir_name = f'{areaName}-{type}数据审核报告'
  641. mkdir_path = saveFileUrl + '/' + dir_name + nowTime
  642. if not os.path.exists(mkdir_path):
  643. os.mkdir(mkdir_path)
  644. # 根据选择的路径读取数据
  645. ConventionalNutrientData = data[indexClassificationList[type]]
  646. ConventionalNutrientDataNum = originData[indexClassificationList[type]]
  647. report.getFrequencyImage(ConventionalNutrientData, mkdir_path)
  648. ConventionalNutrientData['序号'] = data['序号']
  649. ConventionalNutrientData['原样品编号'] = data['原样品编号']
  650. ConventionalNutrientData['样品编号'] = data['样品编号']
  651. ConventionalNutrientData['地理位置'] = data['地理位置']
  652. ConventionalNutrientData['母质'] = data['母质']
  653. ConventionalNutrientData['土壤类型'] = data['土壤类型']
  654. ConventionalNutrientData['土地利用类型'] = data['土地利用类型']
  655. # ConventionalNutrientData['土壤质地'] = data['土壤质地']
  656. ConventionalNutrientDataNum['序号'] = originData['序号']
  657. ConventionalNutrientDataNum['原样品编号'] = originData['原样品编号']
  658. ConventionalNutrientDataNum['样品编号'] = originData['样品编号']
  659. ConventionalNutrientDataNum['地理位置'] = originData['地理位置']
  660. ConventionalNutrientDataNum['母质'] = originData['母质']
  661. ConventionalNutrientDataNum['土壤类型'] = originData['土壤类型']
  662. ConventionalNutrientDataNum['土地利用类型'] = originData['土地利用类型']
  663. # 生成相应审核报告
  664. ConventionalNutrientData['原样品编号'] = ConventionalNutrientData['原样品编号'].astype(str)
  665. ConventionalNutrientDataNum['原样品编号'] = ConventionalNutrientDataNum['原样品编号'].astype(str)
  666. data['原样品编号'] = data['原样品编号'].astype(str)
  667. # checkData = pd.read_excel(changeFileUrl, sheet_name='检测方法')
  668. # 上面这个地址,可以纯递给函数中,用于保存表格和图片
  669. # 调用函数 开始生成报告相关内容
  670. # 表1相关数据
  671. typeData = report.getSimpleNum(ConventionalNutrientData)
  672. lenNum_1 = len(typeData['sData'])
  673. lenNum_1_f = len(typeData['allData'])
  674. table_1_data = pd.DataFrame({
  675. '类型': typeData['sData'].index,
  676. '数量': typeData['sData'],
  677. '合计': [typeData['sData'].sum() for _ in range(lenNum_1)]
  678. })
  679. # 表2数据
  680. table_2_data = report.getDataComplete(ConventionalNutrientData)
  681. table_2_data = table_2_data.reset_index()
  682. table_2_data.columns = ['指标名称', '实测数量', '应测数量']
  683. # 表3数据
  684. # table_3_data = report.checkMethod(checkData, mkdir_path)
  685. # 数据修约 表4
  686. report.getNum(ConventionalNutrientDataNum, mkdir_path)
  687. # 数据填报项审核 表5
  688. report.dataReportResult(ConventionalNutrientData, mkdir_path)
  689. # 表6数据 土壤质地类型不一致
  690. middData = data[['原样品编号', '样品编号']].astype(str)
  691. middData['编号'] = middData['原样品编号']
  692. del middData['原样品编号']
  693. check_1_data = pd.merge(check_1_data, middData, how='left', on='编号')
  694. check_1_data = check_1_data.replace(np.nan, '')
  695. # typeNotSame = check_1_data[check_1_data['土壤质地'] != check_1_data['土壤类型(判断)']]
  696. # table_6_data = typeNotSame[['编号', '样品编号', '土壤质地', '土壤类型(判断)']]
  697. allNeedData = pd.DataFrame({})
  698. allNeedData['原样品编号'] = check_1_data['编号']
  699. getSimpleDataNumber = pd.merge(allNeedData, ConventionalNutrientData[['原样品编号', '样品编号']], how='left', on="原样品编号")
  700. allNeedData['样品编号'] = getSimpleDataNumber['样品编号']
  701. allNeedData['土地利用类型'] = check_1_data['土地利用类型']
  702. allNeedData['审核结果'] = check_10_data['审核结果'] + check_12_data['审核结果']
  703. allNeedData['外业'] = ['' for _ in range(len(check_1_data))]
  704. table_7_data = allNeedData[allNeedData['审核结果'] != '']
  705. del table_7_data['审核结果']
  706. # 写进表格
  707. with pd.ExcelWriter(f'{mkdir_path}/超阈值样品统计表.xlsx', engine='openpyxl') as writer:
  708. table_7_data.to_excel(writer, index=False, sheet_name='超阈值数据')
  709. # 表8数据
  710. table_8_data = report.getPHData(ConventionalNutrientData, mkdir_path)
  711. # 表10 数据
  712. table_10_data = report.getNAndC(ConventionalNutrientData, mkdir_path)
  713. # 表11 数据:全磷和有效磷异常数据统计
  714. table_11_data = report.getPData(ConventionalNutrientData, mkdir_path)
  715. report.getKData(ConventionalNutrientData, mkdir_path)
  716. # 表13 所有存疑数据
  717. with pd.ExcelWriter(f'{mkdir_path}/数据审核过程存疑数据一览表.xlsx', engine='openpyxl') as writer:
  718. allNeedData[allNeedData['审核结果'] != ''].to_excel(writer, index=False, sheet_name='存疑数据')
  719. # 附表: 频度分析图
  720. # report.getFrequencyImage(ConventionalNutrientData, mkdir_path)
  721. table_f_2_data = report.getFrequencyInformation(data, mkdir_path)
  722. # 新建一个文档
  723. doc = Document()
  724. # 添加标题
  725. doc.add_heading(f"{areaName}第三次全国土壤普查常规养分指标检测数据审核报告", level=0)
  726. # 在文档中添加封面段落
  727. fm = doc.add_paragraph()
  728. fm = doc.add_paragraph()
  729. fm = doc.add_paragraph()
  730. fm = doc.add_paragraph()
  731. fm = doc.add_paragraph()
  732. # 插入图片,设置宽度为6英寸(可根据需求调整)
  733. run = fm.add_run()
  734. run.add_picture( 'img/第三次全国土壤普查img.png', width=Inches(2.26))
  735. fm.alignment = WD_TABLE_ALIGNMENT.CENTER
  736. # 在文档中添加封面段落
  737. fm = doc.add_paragraph()
  738. fm = doc.add_paragraph()
  739. fm = doc.add_paragraph()
  740. fm = doc.add_paragraph()
  741. fm = doc.add_paragraph()
  742. fm = doc.add_paragraph()
  743. # 获取当前日期
  744. current_date = datetime.now()
  745. # 将年份和月份转换为中文大写数字
  746. year = int(current_date.strftime("%Y")) # 转换为整数
  747. month = int(current_date.strftime("%m")) # 转换为整数
  748. # 使用 cn2an 将数字转换为中文大写
  749. year_chinese =number_to_chinese_year(year) # 年份转换
  750. month_chinese = cn2an.an2cn(month) # 月份转换
  751. current_date_formatted = f"{year_chinese}年{month_chinese}月"
  752. # 组合动态文本
  753. dynamic_text = f"安徽农业大学资源与环境学院\n{current_date_formatted}"
  754. # 添加文字并居中
  755. text_paragraph = doc.add_paragraph()
  756. text_run = text_paragraph.add_run(dynamic_text)
  757. text_run.font.name = "宋体"
  758. text_run.font.size = Pt(18)
  759. text_run.bold = True # 设置字体加粗
  760. text_paragraph.alignment = 1 # 1 表示居中对齐
  761. # 正确插入分页符
  762. doc.add_page_break()
  763. heading = doc.add_heading('总体概述', level=1)
  764. heading.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  765. # 第一段
  766. long_text1 = f"""
  767. {areaName}第三次全国土壤普查县级数据审核报告主要通过收集和整理相关数据,并对其进行内业检测数据的完整性、规范性和合理性进行审核,形成存疑样点清单及存疑样点结果判定,最终编制完成数据审核报告,同时提交( )对相关指标进行整改复测。报告整理了( )个表层样品数据(含平行样、质控样)、( )个水稳性大团聚体样品数据(含平行样)、( )个剖面样品数据(含平行样、质控样),共( )次样品检测结果分析情况。相关结果分别按照物理性指标检测数据、一般化学指标检测数据、常规养分指标检测数据和重金属指标检测数据形成四份报告。本报告为表层样常规养分指标检测数据审核报告。
  768. """
  769. para0 = doc.add_paragraph(long_text1)
  770. run0 = para0.runs[0] # 获取段落中的第一个run对象
  771. run0.font.name = '宋体' # 设置字体为宋体
  772. run0.font.size = Pt(11) # 设置字号为11磅
  773. # 设置段落的行间距为1.5倍
  774. para_format = para0.paragraph_format
  775. para_format.line_spacing = 1.5 # 设置行间距为1.5倍
  776. # 添加一级标题
  777. doc.add_heading('一、数据完整性审核', level=1)
  778. # 第二段
  779. long_text2 = """
  780. 外业信息调查采样环节:采用电子围栏和外业调查采样APP,对采样位置和填报信息进行管理,确保外业调查信息填报完整。
  781. 样品检测数据上报环节:通过土壤普查工作平台对上报数据的完整性进行筛查。( )第三次土壤普查相关指标检测数据由( )提供,数据均已通过省级质控实验室和县级土壤普查办审核;相关土壤指标历史数据则由( )第三次土壤普查办公室提供。根据《第三次全国土壤普查土壤样品制备与检测技术规范(修订版)》要求,统计各土地利用类型的样品数量,并按照耕地园地土壤样品(表层/剖面)、林地草地土壤样品(表层/剖面)以及水稳定性大团聚体样品(见表1)进行分类,编制了指标名称与实际检测样品数量统计表(见表2),其中水溶性盐分总量大于1g/kg,增加检测了八大离子(该指标在化学指标检测数据审核报告内)。
  782. """
  783. para = doc.add_paragraph(long_text2)
  784. run1 = para.runs[0]
  785. run1.font.name = '宋体' # 设置字体为宋体
  786. run1.font.size = Pt(11) # 设置字号为11磅
  787. # 设置段落的行间距为1.5倍
  788. para_format = para.paragraph_format
  789. para_format.line_spacing = 1.5 # 设置行间距为1.5倍
  790. doc.add_heading('1、土地利用类型与检测指标符合性审核', level=2)
  791. # 插入表格1
  792. paragraph_1 = doc.add_paragraph()
  793. paragraph_1.add_run(f"表1:{areaName}三普样品数量统计表(表层)").bold = True
  794. # 设置居中
  795. paragraph_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
  796. table_1 = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
  797. table_1.alignment = WD_TABLE_ALIGNMENT.CENTER
  798. # 遍历表格 插入数据
  799. # 遍历表格的所有单元格,并填充内容
  800. for i, row in enumerate(table_1.rows):
  801. for j, cell in enumerate(row.cells):
  802. # 获取单元格中的段落对象
  803. paragraph = cell.paragraphs[0]
  804. if i == 0:
  805. r = paragraph.add_run(str(table_1_data.columns[j]))
  806. r.font.bold = True
  807. else:
  808. r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
  809. r.font.size = Pt(10.5)
  810. r.font.name = 'Times New Roman'
  811. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  812. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  813. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  814. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  815. # 合并单元格 合并第3列的第二行和第三行
  816. if lenNum_1 > 1:
  817. table_1.cell(2, 2).text = ''
  818. table_1.cell(1, 2).merge(table_1.cell(2, 2))
  819. ############test##############
  820. doc.add_heading('2、指标名称与实际检测样品数量完整性审核', level=2)
  821. # 插入表格2
  822. paragraph_2 = doc.add_paragraph()
  823. paragraph_2.add_run(f'表2:{areaName}指标名称与实际检测样品数量统计表').bold = True
  824. table_2 = doc.add_table(rows=len(table_2_data) + 1, cols=3, style='Light Shading Accent 1')
  825. paragraph_2.alignment = WD_ALIGN_PARAGRAPH.CENTER
  826. table_2.alignment = WD_TABLE_ALIGNMENT.CENTER
  827. for i, row in enumerate(table_2.rows):
  828. for j, cell in enumerate(row.cells):
  829. # 获取单元格中的段落对象
  830. paragraph = cell.paragraphs[0]
  831. if i == 0:
  832. r = paragraph.add_run(str(table_2_data.columns[j]))
  833. r.font.bold = True
  834. else:
  835. r = paragraph.add_run(str(table_2_data.iloc[i - 1, j]))
  836. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  837. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  838. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  839. r.font.size = Pt(10.5)
  840. r.font.name = 'Times New Roman'
  841. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  842. doc.add_heading('二、数据规范性审核', level=1)
  843. doc.add_heading('1、数据填报规范性审核', level=2)
  844. # 插入表3
  845. paragraph_3 = doc.add_paragraph()
  846. paragraph_3.add_run(f'表3:{areaName}土壤检测数据检测方法填报审核结果表').bold = True
  847. # table_3 = doc.add_table(rows=2, cols=2)
  848. paragraph_3.alignment = WD_ALIGN_PARAGRAPH.CENTER
  849. # table_3.alignment = WD_TABLE_ALIGNMENT.CENTER
  850. # 写入数据 这里数据写不下 嵌入链接
  851. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:检测方法审核结果.xlsx', level=4)
  852. doc.add_heading('2、数值修约规范性审核', level=2)
  853. # 插入表4
  854. paragraph_4 = doc.add_paragraph()
  855. paragraph_4.add_run(f'表4:{areaName}土壤检测数据数值修约结果表').bold = True
  856. # table_4 = doc.add_table(rows=2, cols=2)
  857. paragraph_4.alignment = WD_ALIGN_PARAGRAPH.CENTER
  858. # table_4.alignment = WD_TABLE_ALIGNMENT.CENTER
  859. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数值修约审核.xlsx', level=4)
  860. # 填入数据 这里数据也放不下 嵌入链接
  861. doc.add_heading('3、数据未检出的填报规范性审核', level=2)
  862. # 插入表5
  863. paragraph_5 = doc.add_paragraph()
  864. paragraph_5.add_run(f'表5:{areaName}土壤检测数据未检出项填报审核结果表').bold = True
  865. # table_5 = doc.add_table(rows=2, cols=2)
  866. paragraph_5.alignment = WD_ALIGN_PARAGRAPH.CENTER
  867. # table_5.alignment = WD_TABLE_ALIGNMENT.CENTER
  868. # 写入数据 这里数据也放不下 嵌入链接
  869. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据填报项审核结果.xlsx', level=4)
  870. # doc.add_heading('4、土壤质地填报规范性审核', level=2)
  871. # # 插入表6
  872. # paragraph_6 = doc.add_paragraph()
  873. # paragraph_6.add_run(f'表6:{areaName}土壤质地填报审核结果表').bold = True
  874. # table_6 = doc.add_table(rows=len(table_6_data) + 1, cols=4, style='Light Shading Accent 1')
  875. # paragraph_6.alignment = WD_ALIGN_PARAGRAPH.CENTER
  876. # table_6.alignment = WD_TABLE_ALIGNMENT.CENTER
  877. # # 提取结果表中数据
  878. # # 写入数据 土壤质地类型不一致的数据提取出来
  879. # for i, row in enumerate(table_6.rows):
  880. # for j, cell in enumerate(row.cells):
  881. # # 获取单元格中的段落对象
  882. # paragraph = cell.paragraphs[0]
  883. # if i == 0:
  884. # r = paragraph.add_run(str(table_6_data.columns[j]))
  885. # r.font.bold = True
  886. # else:
  887. # r = paragraph.add_run(str(table_6_data.iloc[i - 1, j]))
  888. # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  889. # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  890. # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  891. # r.font.size = Pt(10.5)
  892. # r.font.name = 'Times New Roman'
  893. # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  894. doc.add_heading('三、数据合理性审核', level=1)
  895. doc.add_heading('1、阈值法审核', level=2)
  896. # 插入表格
  897. paragraph_7 = doc.add_paragraph()
  898. paragraph_7.add_run(f'表6:{areaName}土壤检测数据超阈值样品统计表').bold = True
  899. # table_7 = doc.add_table(rows=2, cols=2)
  900. # paragraph_7.alignment = WD_ALIGN_PARAGRAPH.CENTER
  901. # table_7.alignment = WD_TABLE_ALIGNMENT.CENTER
  902. # 写入数据 点击查看数据 这里也不一定写的下 最好是嵌入链接
  903. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据审核过程存疑数据一览表.xlsx', level=4)
  904. # todo 合并所有数据 审核结果不为空的数据 写入表格保存到指定文件夹
  905. doc.add_heading('2、极值法审核', level=2)
  906. doc.add_heading('(1)pH', level=3)
  907. # 插入ph分布图
  908. if os.path.isfile(f'{mkdir_path}/PH值分布图.png'):
  909. doc.add_picture(f'{mkdir_path}/PH值分布图.png', width=Inches(6.0))
  910. paragraph_t_1 = doc.add_paragraph()
  911. paragraph_t_1.add_run(f'图1:pH值分布情况').bold = True
  912. paragraph_t_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
  913. # 插入频度统计表
  914. paragraph_8 = doc.add_paragraph()
  915. paragraph_8.add_run('表7:pH数据统计表').bold = True
  916. table_8 = doc.add_table(rows=6, cols=2, style='Light Shading Accent 1')
  917. t_8 = table_8_data['频度分析']
  918. t_8 = t_8.reset_index()
  919. t_8.columns = ['指标', '数据']
  920. paragraph_8.alignment = WD_ALIGN_PARAGRAPH.CENTER
  921. table_8.alignment = WD_TABLE_ALIGNMENT.CENTER
  922. for i, row in enumerate(table_8.rows):
  923. for j, cell in enumerate(row.cells):
  924. # 获取单元格中的段落对象
  925. paragraph = cell.paragraphs[0]
  926. if i == 0:
  927. r = paragraph.add_run(str(t_8.columns[j]))
  928. r.font.bold = True
  929. else:
  930. r = paragraph.add_run(str(t_8.iloc[i - 1, j]))
  931. r.font.size = Pt(10.5)
  932. r.font.name = 'Times New Roman'
  933. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  934. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  935. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  936. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  937. # 插入异常数据提取表格 todo 这里数据多的话也可能写不下 最好是嵌入一下
  938. if not table_8_data['异常数据'].empty:
  939. paragraph_9 = doc.add_paragraph()
  940. paragraph_9.add_run('表8:pH异常数据统计表').bold = True
  941. table_9 = doc.add_table(rows=len(table_8_data['异常数据']) + 1, cols=6, style='Light Shading Accent 1')
  942. t_9 = table_8_data['异常数据']
  943. paragraph_9.alignment = WD_ALIGN_PARAGRAPH.CENTER
  944. table_9.alignment = WD_TABLE_ALIGNMENT.CENTER
  945. for i, row in enumerate(table_9.rows):
  946. for j, cell in enumerate(row.cells):
  947. # 获取单元格中的段落对象
  948. paragraph = cell.paragraphs[0]
  949. if i == 0:
  950. r = paragraph.add_run(str(t_9.columns[j]))
  951. r.font.bold = True
  952. else:
  953. r = paragraph.add_run(str(t_9.iloc[i - 1, j]))
  954. r.font.size = Pt(10.5)
  955. r.font.name = 'Times New Roman'
  956. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  957. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  958. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  959. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  960. doc.add_heading('3、关联分析法审核', level=2)
  961. if os.path.isfile(f'{mkdir_path}/有机质与全氮相关性分析图.png'):
  962. doc.add_picture(f'{mkdir_path}/有机质与全氮相关性分析图.png', width=Inches(6.0))
  963. paragraph_t_2 = doc.add_paragraph()
  964. paragraph_t_2.add_run(f'图2:有机质与全氮相关关系').bold = True
  965. paragraph_t_2.alignment = WD_ALIGN_PARAGRAPH.CENTER
  966. # 插入碳氮比异常数据
  967. if not table_10_data.empty:
  968. paragraph_10 = doc.add_paragraph()
  969. paragraph_10.add_run('表9:碳氮比异常数据统计表').bold = True
  970. table_10 = doc.add_table(rows=len(table_10_data) + 1, cols=8, style='Light Shading Accent 1')
  971. paragraph_10.alignment = WD_ALIGN_PARAGRAPH.CENTER
  972. table_10.alignment = WD_TABLE_ALIGNMENT.CENTER
  973. for i, row in enumerate(table_10.rows):
  974. for j, cell in enumerate(row.cells):
  975. # 获取单元格中的段落对象
  976. paragraph = cell.paragraphs[0]
  977. if i == 0:
  978. r = paragraph.add_run(str(table_10_data.columns[j]))
  979. r.font.bold = True
  980. else:
  981. r = paragraph.add_run(str(table_10_data.iloc[i - 1, j]))
  982. r.font.size = Pt(10.5)
  983. r.font.name = 'Times New Roman'
  984. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  985. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  986. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  987. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  988. doc.add_heading('4、指标综合分析', level=2)
  989. # 插入图片
  990. if os.path.isfile(f'{mkdir_path}/全磷分布图.png'):
  991. doc.add_picture(f'{mkdir_path}/全磷分布图.png', width=Inches(6.0))
  992. paragraph_t_3 = doc.add_paragraph()
  993. paragraph_t_3.add_run(f'图3:全磷分布图').bold = True
  994. paragraph_t_3.alignment = WD_ALIGN_PARAGRAPH.CENTER
  995. if os.path.isfile(f'{mkdir_path}/有效磷分布图.png'):
  996. doc.add_picture(f'{mkdir_path}/有效磷分布图.png', width=Inches(6.0))
  997. paragraph_t_4 = doc.add_paragraph()
  998. paragraph_t_4.add_run(f'图4:有效磷分布图').bold = True
  999. paragraph_t_4.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1000. # 插入图片
  1001. if os.path.isfile(f'{mkdir_path}/有效磷占全磷比分布图.png'):
  1002. doc.add_picture(f'{mkdir_path}/有效磷占全磷比分布图.png', width=Inches(6.0))
  1003. paragraph_t_5 = doc.add_paragraph()
  1004. paragraph_t_5.add_run(f'图5:有效磷含量占全磷含量比例').bold = True
  1005. paragraph_t_5.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1006. # 插入表格
  1007. if not table_11_data.empty:
  1008. paragraph_11 = doc.add_paragraph()
  1009. paragraph_11.add_run('表10:全磷与有效磷异常样品统计表').bold = True
  1010. table_11 = doc.add_table(rows=len(table_11_data) + 1, cols=7, style='Light Shading Accent 1')
  1011. paragraph_11.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1012. table_11.alignment = WD_TABLE_ALIGNMENT.CENTER
  1013. for i, row in enumerate(table_11.rows):
  1014. for j, cell in enumerate(row.cells):
  1015. # 获取单元格中的段落对象
  1016. paragraph = cell.paragraphs[0]
  1017. if i == 0:
  1018. r = paragraph.add_run(str(table_11_data.columns[j]))
  1019. r.font.bold = True
  1020. else:
  1021. r = paragraph.add_run(str(table_11_data.iloc[i - 1, j]))
  1022. r.font.size = Pt(10.5)
  1023. r.font.name = 'Times New Roman'
  1024. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1025. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1026. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1027. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1028. else:
  1029. paragraph_11 = doc.add_paragraph()
  1030. paragraph_11.add_run('表10:全磷与有效磷异常样品统计表').bold = True
  1031. paragraph_11_info = doc.add_paragraph()
  1032. paragraph_11_info.add_run('无异常数据')
  1033. paragraph_11.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1034. paragraph_11_info.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1035. # 全钾、速效钾、缓效钾
  1036. if os.path.isfile(f'{mkdir_path}/全钾与速效钾缓效钾之和关系统计图.png'):
  1037. doc.add_picture(f'{mkdir_path}/全钾与速效钾缓效钾之和关系统计图.png', width=Inches(6.0))
  1038. paragraph_t_6 = doc.add_paragraph()
  1039. paragraph_t_6.add_run(f'图6:全钾与速效钾缓效钾之和关系统计图').bold = True
  1040. paragraph_t_6.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1041. if os.path.isfile(f'{mkdir_path}/速效钾与缓效钾关系统计图.png'):
  1042. doc.add_picture(f'{mkdir_path}/速效钾与缓效钾关系统计图.png', width=Inches(6.0))
  1043. paragraph_t_7 = doc.add_paragraph()
  1044. paragraph_t_7.add_run(f'图7:速效钾与缓效钾关系统计图').bold = True
  1045. paragraph_t_7.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1046. doc.add_heading('四、审核存疑数据', level=1)
  1047. paragraph_12 = doc.add_paragraph()
  1048. paragraph_12.add_run(f'表11:数据审核过程存疑数据一览表').bold = True
  1049. paragraph_12.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1050. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据审核过程存疑数据一览表.xlsx', level=4)
  1051. doc.add_heading('五、附表', level=1)
  1052. doc.add_heading('附表1:某区三普样品数量统计表(表层)', level=2)
  1053. # 插入附表1
  1054. table_1_f = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
  1055. table_1_f.alignment = WD_TABLE_ALIGNMENT.CENTER
  1056. # 遍历表格 插入数据
  1057. # 遍历表格的所有单元格,并填充内容
  1058. for i, row in enumerate(table_1_f.rows):
  1059. for j, cell in enumerate(row.cells):
  1060. # 获取单元格中的段落对象
  1061. paragraph = cell.paragraphs[0]
  1062. if i == 0:
  1063. r = paragraph.add_run(str(table_1_data.columns[j]))
  1064. r.font.bold = True
  1065. else:
  1066. r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
  1067. r.font.size = Pt(10.5)
  1068. r.font.name = 'Times New Roman'
  1069. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1070. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1071. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1072. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1073. # 合并单元格 合并第3列的第二行和第三行
  1074. if lenNum_1 > 1:
  1075. table_1_f.cell(2, 2).text = ''
  1076. table_1_f.cell(1, 2).merge(table_1_f.cell(2, 2))
  1077. doc.add_heading('附表2:各指标频度分析表', level=2)
  1078. # 插入表格 写入数据
  1079. table_f_2_data = table_f_2_data.replace(np.nan, '')
  1080. makeInfoTable(table_f_2_data, doc)
  1081. # table_f_2 = doc.add_table(rows=len(table_f_2_data) + 1, cols=6, style='Light Shading Accent 1')
  1082. # for i, row in enumerate(table_f_2.rows):
  1083. # for j, cell in enumerate(row.cells):
  1084. # # 获取单元格中的段落对象
  1085. # paragraph = cell.paragraphs[0]
  1086. # if i == 0:
  1087. # r = paragraph.add_run(str(table_f_2_data.columns[j]))
  1088. # r.font.bold = True
  1089. # else:
  1090. # r = paragraph.add_run(str(table_f_2_data.iloc[i - 1, j]))
  1091. # r.font.size = Pt(10.5)
  1092. # r.font.name = 'Times New Roman'
  1093. # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1094. # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1095. # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1096. # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1097. # doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:频度分析表.xlsx', level=4)
  1098. doc.add_heading('附表3:各指标频度分析图', level=2)
  1099. # 插入频度信息的图形
  1100. if os.path.isfile(f'{mkdir_path}/pH分析图.png'):
  1101. doc.add_picture(f'{mkdir_path}/pH分析图.png', width=Inches(6.0))
  1102. if os.path.isfile(f'{mkdir_path}/缓效钾分析图.png'):
  1103. doc.add_picture(f'{mkdir_path}/缓效钾分析图.png', width=Inches(6.0))
  1104. if os.path.isfile(f'{mkdir_path}/全氮分析图.png'):
  1105. doc.add_picture(f'{mkdir_path}/全氮分析图.png', width=Inches(6.0))
  1106. if os.path.isfile(f'{mkdir_path}/全钾分析图.png'):
  1107. doc.add_picture(f'{mkdir_path}/全钾分析图.png', width=Inches(6.0))
  1108. if os.path.isfile(f'{mkdir_path}/全磷分析图.png'):
  1109. doc.add_picture(f'{mkdir_path}/全磷分析图.png', width=Inches(6.0))
  1110. if os.path.isfile(f'{mkdir_path}/速效钾分析图.png'):
  1111. doc.add_picture(f'{mkdir_path}/速效钾分析图.png', width=Inches(6.0))
  1112. if os.path.isfile(f'{mkdir_path}/有机质分析图.png'):
  1113. doc.add_picture(f'{mkdir_path}/有机质分析图.png', width=Inches(6.0))
  1114. if os.path.isfile(f'{mkdir_path}/有效硅分析图.png'):
  1115. doc.add_picture(f'{mkdir_path}/有效硅分析图.png', width=Inches(6.0))
  1116. if os.path.isfile(f'{mkdir_path}/有效磷分析图.png'):
  1117. doc.add_picture(f'{mkdir_path}/有效磷分析图.png', width=Inches(6.0))
  1118. if os.path.isfile(f'{mkdir_path}/有效硫分析图.png'):
  1119. doc.add_picture(f'{mkdir_path}/有效硫分析图.png', width=Inches(6.0))
  1120. if os.path.isfile(f'{mkdir_path}/有效锰分析图.png'):
  1121. doc.add_picture(f'{mkdir_path}/有效锰分析图.png', width=Inches(6.0))
  1122. if os.path.isfile(f'{mkdir_path}/有效钼分析图.png'):
  1123. doc.add_picture(f'{mkdir_path}/有效钼分析图.png', width=Inches(6.0))
  1124. if os.path.isfile(f'{mkdir_path}/有效硼分析图.png'):
  1125. doc.add_picture(f'{mkdir_path}/有效硼分析图.png', width=Inches(6.0))
  1126. if os.path.isfile(f'{mkdir_path}/有效铁分析图.png'):
  1127. doc.add_picture(f'{mkdir_path}/有效铁分析图.png', width=Inches(6.0))
  1128. if os.path.isfile(f'{mkdir_path}/有效铜分析图.png'):
  1129. doc.add_picture(f'{mkdir_path}/有效铜分析图.png', width=Inches(6.0))
  1130. if os.path.isfile(f'{mkdir_path}/有效锌分析图.png'):
  1131. doc.add_picture(f'{mkdir_path}/有效锌分析图.png', width=Inches(6.0))
  1132. doc.add_heading('附表4:数值修约标准', level=2)
  1133. # 读取数据 插入表格 写入数据
  1134. numData = pd.read_excel('./img/数值修约要求.xlsx', sheet_name='Sheet1')
  1135. table_2_f = doc.add_table(rows=len(numData) + 1, cols=2, style='Light Shading Accent 1')
  1136. table_2_f.alignment = WD_TABLE_ALIGNMENT.CENTER
  1137. for i, row in enumerate(table_2_f.rows):
  1138. for j, cell in enumerate(row.cells):
  1139. # 获取单元格中的段落对象
  1140. paragraph = cell.paragraphs[0]
  1141. if i == 0:
  1142. r = paragraph.add_run(str(numData.columns[j]))
  1143. r.font.bold = True
  1144. else:
  1145. r = paragraph.add_run(str(numData.iloc[i - 1, j]))
  1146. r.font.size = Pt(10.5)
  1147. r.font.name = 'Times New Roman'
  1148. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1149. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1150. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1151. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1152. # 处理样式 遍历所有的段落 修改字体
  1153. # 遍历并打印每个段落的文本
  1154. paragraphs = doc.paragraphs
  1155. for paragraph in paragraphs:
  1156. for run in paragraph.runs:
  1157. run.font.color.rgb = RGBColor(0, 0, 0)
  1158. run.font.name = 'Times New Roman'
  1159. run.font.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1160. # 保存Word文档
  1161. doc.save(f'{mkdir_path}/{areaName}-{type}审核报告.docx')
  1162. # 生成一般化学性指标审核报告
  1163. def getChemicalIndicators(originData,data,type, changeFileUrl, saveFileUrl, check_1_data,
  1164. check_3_data,
  1165. check_5_data,
  1166. check_8_data, # 样品编号替换为编号
  1167. check_10_data,
  1168. check_12_data,
  1169. check_14_data):
  1170. # 生成报告
  1171. name = os.path.basename(changeFileUrl)
  1172. n = name.split('.')
  1173. areaName = n[0].replace('数据', '')
  1174. # 生成一个新的文件夹用于存放审核报告相关的数据
  1175. nowTime = time.strftime("%Y-%m-%d %H时%M分%S秒", time.localtime())
  1176. dir_name = f'{areaName}-{type}数据审核报告'
  1177. mkdir_path = saveFileUrl + '/' + dir_name + nowTime
  1178. if not os.path.exists(mkdir_path):
  1179. os.mkdir(mkdir_path)
  1180. # 根据选择的路径读取数据
  1181. cheemicalData = data[indexClassificationList[type]]
  1182. cheemicalDataNum = originData[indexClassificationList[type]]
  1183. report.getFrequencyImage(cheemicalData, mkdir_path)
  1184. cheemicalData['序号'] = data['序号']
  1185. cheemicalData['原样品编号'] = data['原样品编号']
  1186. cheemicalData['样品编号'] = data['样品编号']
  1187. cheemicalData['地理位置'] = data['地理位置']
  1188. cheemicalData['母质'] = data['母质']
  1189. cheemicalData['土壤类型'] = data['土壤类型']
  1190. cheemicalData['土地利用类型'] = data['土地利用类型']
  1191. # cheemicalData['土壤质地'] = data['土壤质地']
  1192. cheemicalData['原样品编号'] = cheemicalData['原样品编号'].astype(str)
  1193. # checkData = pd.read_excel(changeFileUrl, sheet_name='检测方法')
  1194. cheemicalDataNum['序号'] = originData['序号']
  1195. cheemicalDataNum['原样品编号'] = originData['原样品编号']
  1196. cheemicalDataNum['样品编号'] = originData['样品编号']
  1197. cheemicalDataNum['地理位置'] = originData['地理位置']
  1198. cheemicalDataNum['母质'] = originData['母质']
  1199. cheemicalDataNum['土壤类型'] = originData['土壤类型']
  1200. cheemicalDataNum['土地利用类型'] = originData['土地利用类型']
  1201. # cheemicalData['土壤质地'] = data['土壤质地']
  1202. cheemicalDataNum['原样品编号'] = cheemicalDataNum['原样品编号'].astype(str)
  1203. # 上面这个地址,可以纯递给函数中,用于保存表格和图片
  1204. # 调用函数 开始生成报告相关内容
  1205. # 表1相关数据
  1206. typeData = report.getSimpleNum(cheemicalData)
  1207. lenNum_1 = len(typeData['sData'])
  1208. lenNum_1_f = len(typeData['allData'])
  1209. table_1_data = pd.DataFrame({
  1210. '类型': typeData['sData'].index,
  1211. '数量': typeData['sData'],
  1212. '合计': [typeData['sData'].sum() for _ in range(lenNum_1)]
  1213. })
  1214. # 表2数据
  1215. table_2_data = report.getDataComplete(cheemicalData)
  1216. table_2_data = table_2_data.reset_index()
  1217. table_2_data.columns = ['指标名称', '实测数量', '应测数量']
  1218. # 表3数据
  1219. # table_3_data = report.checkMethod(checkData, mkdir_path)
  1220. # 数据修约 表4
  1221. report.getNum(cheemicalDataNum, mkdir_path)
  1222. # 数据填报项审核 表5
  1223. report.dataReportResult(cheemicalData, mkdir_path)
  1224. # 表6数据 土壤质地类型不一致
  1225. middData = data[['原样品编号', '样品编号']].astype(str)
  1226. middData['编号'] = middData['原样品编号']
  1227. del middData['原样品编号']
  1228. check_1_data = pd.merge(check_1_data, middData, how='left', on='编号')
  1229. check_1_data = check_1_data.replace(np.nan, '')
  1230. # typeNotSame = check_1_data[check_1_data['土壤质地'] != check_1_data['土壤类型(判断)']]
  1231. # table_6_data = typeNotSame[['编号', '样品编号', '土壤质地', '土壤类型(判断)']]
  1232. allNeedData = pd.DataFrame({})
  1233. allNeedData['原样品编号'] = check_1_data['编号']
  1234. getSimpleDataNumber = pd.merge(allNeedData, data[['原样品编号', '样品编号']], how='left', on="原样品编号")
  1235. allNeedData['样品编号'] = getSimpleDataNumber['样品编号']
  1236. allNeedData['土地利用类型'] = check_1_data['土地利用类型']
  1237. allNeedData['审核结果'] = check_5_data['审核结果'] + check_8_data['审核结果']
  1238. allNeedData['外业'] = ['' for _ in range(len(check_1_data))]
  1239. table_7_data = allNeedData[allNeedData['审核结果'] != '']
  1240. del table_7_data['审核结果']
  1241. # 写进表格
  1242. with pd.ExcelWriter(f'{mkdir_path}/超阈值样品统计表.xlsx', engine='openpyxl') as writer:
  1243. table_7_data.to_excel(writer, index=False, sheet_name='超阈值数据')
  1244. # 表8数据
  1245. table_8_data = report.getPHData(cheemicalData, mkdir_path)
  1246. report.cationExchangeCapacity(cheemicalData, mkdir_path)
  1247. report.changeCation(cheemicalData, mkdir_path)
  1248. report.manyTypes(cheemicalData, mkdir_path)
  1249. # 表13 所有存疑数据
  1250. with pd.ExcelWriter(f'{mkdir_path}/数据审核过程存疑数据一览表.xlsx', engine='openpyxl') as writer:
  1251. allNeedData[allNeedData['审核结果'] != ''].to_excel(writer, index=False, sheet_name='存疑数据')
  1252. # 附表: 频度分析图
  1253. # report.getFrequencyImage(cheemicalData, mkdir_path)
  1254. table_f_2_data = report.getFrequencyInformation(data, mkdir_path)
  1255. # 新建一个文档
  1256. doc = Document()
  1257. # 添加标题
  1258. doc.add_heading(f"{areaName}第三次全国土壤普查数据审核报告", level=0)
  1259. # 添加一级标题
  1260. doc.add_heading('一、数据完整性审核', level=1)
  1261. doc.add_heading('1、土地利用类型与检测指标符合性审核', level=2)
  1262. # 插入表格1
  1263. paragraph_1 = doc.add_paragraph()
  1264. paragraph_1.add_run(f"表1:{areaName}三普样品数量统计表(表层)").bold = True
  1265. # 设置居中
  1266. paragraph_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1267. table_1 = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
  1268. table_1.alignment = WD_TABLE_ALIGNMENT.CENTER
  1269. # 遍历表格 插入数据
  1270. # 遍历表格的所有单元格,并填充内容
  1271. for i, row in enumerate(table_1.rows):
  1272. for j, cell in enumerate(row.cells):
  1273. # 获取单元格中的段落对象
  1274. paragraph = cell.paragraphs[0]
  1275. if i == 0:
  1276. r = paragraph.add_run(str(table_1_data.columns[j]))
  1277. r.font.bold = True
  1278. else:
  1279. r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
  1280. r.font.size = Pt(10.5)
  1281. r.font.name = 'Times New Roman'
  1282. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1283. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1284. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1285. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1286. # 合并单元格 合并第3列的第二行和第三行
  1287. if lenNum_1 > 1:
  1288. table_1.cell(2, 2).text = ''
  1289. table_1.cell(1, 2).merge(table_1.cell(2, 2))
  1290. ############test##############
  1291. doc.add_heading('2、指标名称与实际检测样品数量完整性审核', level=2)
  1292. # 插入表格2
  1293. paragraph_2 = doc.add_paragraph()
  1294. paragraph_2.add_run(f'表2:{areaName}指标名称与实际检测样品数量统计表').bold = True
  1295. table_2 = doc.add_table(rows=len(table_2_data) + 1, cols=3, style='Light Shading Accent 1')
  1296. paragraph_2.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1297. table_2.alignment = WD_TABLE_ALIGNMENT.CENTER
  1298. for i, row in enumerate(table_2.rows):
  1299. for j, cell in enumerate(row.cells):
  1300. # 获取单元格中的段落对象
  1301. paragraph = cell.paragraphs[0]
  1302. if i == 0:
  1303. r = paragraph.add_run(str(table_2_data.columns[j]))
  1304. r.font.bold = True
  1305. else:
  1306. r = paragraph.add_run(str(table_2_data.iloc[i - 1, j]))
  1307. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1308. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1309. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1310. r.font.size = Pt(10.5)
  1311. r.font.name = 'Times New Roman'
  1312. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1313. doc.add_heading('二、数据规范性审核', level=1)
  1314. doc.add_heading('1、数据填报规范性审核', level=2)
  1315. # 插入表3
  1316. paragraph_3 = doc.add_paragraph()
  1317. paragraph_3.add_run(f'表3:{areaName}土壤检测数据检测方法填报审核结果表').bold = True
  1318. # table_3 = doc.add_table(rows=2, cols=2)
  1319. paragraph_3.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1320. # table_3.alignment = WD_TABLE_ALIGNMENT.CENTER
  1321. # 写入数据 这里数据写不下 嵌入链接
  1322. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:检测方法审核结果.xlsx', level=4)
  1323. doc.add_heading('2、数值修约规范性审核', level=2)
  1324. # 插入表4
  1325. paragraph_4 = doc.add_paragraph()
  1326. paragraph_4.add_run(f'表4:{areaName}土壤检测数据数值修约结果表').bold = True
  1327. # table_4 = doc.add_table(rows=2, cols=2)
  1328. paragraph_4.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1329. # table_4.alignment = WD_TABLE_ALIGNMENT.CENTER
  1330. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数值修约审核.xlsx', level=4)
  1331. # 填入数据 这里数据也放不下 嵌入链接
  1332. doc.add_heading('3、数据未检出的填报规范性审核', level=2)
  1333. # 插入表5
  1334. paragraph_5 = doc.add_paragraph()
  1335. paragraph_5.add_run(f'表5:{areaName}土壤检测数据未检出项填报审核结果表').bold = True
  1336. # table_5 = doc.add_table(rows=2, cols=2)
  1337. paragraph_5.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1338. # table_5.alignment = WD_TABLE_ALIGNMENT.CENTER
  1339. # 写入数据 这里数据也放不下 嵌入链接
  1340. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据填报项审核结果.xlsx', level=4)
  1341. # doc.add_heading('4、土壤质地填报规范性审核', level=2)
  1342. # # 插入表6
  1343. # paragraph_6 = doc.add_paragraph()
  1344. # paragraph_6.add_run(f'表6:{areaName}土壤质地填报审核结果表').bold = True
  1345. # table_6 = doc.add_table(rows=len(table_6_data) + 1, cols=4, style='Light Shading Accent 1')
  1346. # paragraph_6.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1347. # table_6.alignment = WD_TABLE_ALIGNMENT.CENTER
  1348. # # 提取结果表中数据
  1349. # # 写入数据 土壤质地类型不一致的数据提取出来
  1350. # for i, row in enumerate(table_6.rows):
  1351. # for j, cell in enumerate(row.cells):
  1352. # # 获取单元格中的段落对象
  1353. # paragraph = cell.paragraphs[0]
  1354. # if i == 0:
  1355. # r = paragraph.add_run(str(table_6_data.columns[j]))
  1356. # r.font.bold = True
  1357. # else:
  1358. # r = paragraph.add_run(str(table_6_data.iloc[i - 1, j]))
  1359. # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1360. # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1361. # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1362. # r.font.size = Pt(10.5)
  1363. # r.font.name = 'Times New Roman'
  1364. # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1365. doc.add_heading('三、数据合理性审核', level=1)
  1366. doc.add_heading('1、阈值法审核', level=2)
  1367. # 插入表格
  1368. paragraph_7 = doc.add_paragraph()
  1369. paragraph_7.add_run(f'表6:{areaName}土壤检测数据超阈值样品统计表').bold = True
  1370. # table_7 = doc.add_table(rows=2, cols=2)
  1371. # paragraph_7.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1372. # table_7.alignment = WD_TABLE_ALIGNMENT.CENTER
  1373. # 写入数据 点击查看数据 这里也不一定写的下 最好是嵌入链接
  1374. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据审核过程存疑数据一览表.xlsx', level=4)
  1375. # todo 合并所有数据 审核结果不为空的数据 写入表格保存到指定文件夹
  1376. doc.add_heading('2、极值法审核', level=2)
  1377. doc.add_heading('(1)pH', level=3)
  1378. # 插入ph分布图
  1379. if os.path.isfile(f'{mkdir_path}/pH值分布图.png'):
  1380. doc.add_picture(f'{mkdir_path}/pH值分布图.png', width=Inches(6.0))
  1381. paragraph_t_1 = doc.add_paragraph()
  1382. paragraph_t_1.add_run(f'图1:pH值分布情况').bold = True
  1383. paragraph_t_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1384. # 插入频度统计表
  1385. paragraph_8 = doc.add_paragraph()
  1386. paragraph_8.add_run('表7:pH数据统计表').bold = True
  1387. table_8 = doc.add_table(rows=6, cols=2, style='Light Shading Accent 1')
  1388. t_8 = table_8_data['频度分析']
  1389. t_8 = t_8.reset_index()
  1390. t_8.columns = ['指标', '数据']
  1391. paragraph_8.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1392. table_8.alignment = WD_TABLE_ALIGNMENT.CENTER
  1393. for i, row in enumerate(table_8.rows):
  1394. for j, cell in enumerate(row.cells):
  1395. # 获取单元格中的段落对象
  1396. paragraph = cell.paragraphs[0]
  1397. if i == 0:
  1398. r = paragraph.add_run(str(t_8.columns[j]))
  1399. r.font.bold = True
  1400. else:
  1401. r = paragraph.add_run(str(t_8.iloc[i - 1, j]))
  1402. r.font.size = Pt(10.5)
  1403. r.font.name = 'Times New Roman'
  1404. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1405. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1406. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1407. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1408. # 插入异常数据提取表格 todo 这里数据多的话也可能写不下 最好是嵌入一下
  1409. if not table_8_data['异常数据'].empty:
  1410. paragraph_9 = doc.add_paragraph()
  1411. paragraph_9.add_run('表8:pH异常数据统计表').bold = True
  1412. table_9 = doc.add_table(rows=len(table_8_data['异常数据']) + 1, cols=6, style='Light Shading Accent 1')
  1413. t_9 = table_8_data['异常数据']
  1414. paragraph_9.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1415. table_9.alignment = WD_TABLE_ALIGNMENT.CENTER
  1416. for i, row in enumerate(table_9.rows):
  1417. for j, cell in enumerate(row.cells):
  1418. # 获取单元格中的段落对象
  1419. paragraph = cell.paragraphs[0]
  1420. if i == 0:
  1421. r = paragraph.add_run(str(t_9.columns[j]))
  1422. r.font.bold = True
  1423. else:
  1424. r = paragraph.add_run(str(t_9.iloc[i - 1, j]))
  1425. r.font.size = Pt(10.5)
  1426. r.font.name = 'Times New Roman'
  1427. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1428. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1429. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1430. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1431. doc.add_heading('3、指标综合分析', level=2)
  1432. # 阳离子交换量与交换性盐总量关系
  1433. if os.path.isfile(f'{mkdir_path}/阳离子交换量与交换性盐基总量相关关系.png'):
  1434. doc.add_picture(f'{mkdir_path}/阳离子交换量与交换性盐基总量相关关系.png', width=Inches(6.0))
  1435. paragraph_t_8 = doc.add_paragraph()
  1436. paragraph_t_8.add_run(f'图8:阳离子交换量与交换性盐总量关系图').bold = True
  1437. paragraph_t_8.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1438. # 交换性盐总量与交换性盐相关关系
  1439. if os.path.isfile(f'{mkdir_path}/交换性盐基总量与交换性盐相关关系(pH小于等于7.5).png'):
  1440. doc.add_picture(f'{mkdir_path}/交换性盐基总量与交换性盐相关关系(pH小于等于7.5).png', width=Inches(6.0))
  1441. paragraph_t_9 = doc.add_paragraph()
  1442. paragraph_t_9.add_run(f'图9:交换性盐基总量和交换性钙镁钠钾分项指标关系(pH≤7.5)').bold = True
  1443. paragraph_t_9.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1444. if os.path.isfile(f'{mkdir_path}/交换性盐基总量与交换性盐相关关系(pH大于7.5).png'):
  1445. doc.add_picture(f'{mkdir_path}/交换性盐基总量与交换性盐相关关系(pH大于7.5).png', width=Inches(6.0))
  1446. paragraph_t_10 = doc.add_paragraph()
  1447. paragraph_t_10.add_run(f'图10:交换性盐基总量和交换性钙镁钠钾分项指标关系(pH大于7.5)').bold = True
  1448. paragraph_t_10.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1449. # 水溶性盐、电导率、离子总量
  1450. if os.path.isfile(f'{mkdir_path}/全盐量分布图.png'):
  1451. doc.add_picture(f'{mkdir_path}/全盐量分布图.png', width=Inches(6.0))
  1452. paragraph_t_11 = doc.add_paragraph()
  1453. paragraph_t_11.add_run(f'图11:全盐量分布图').bold = True
  1454. paragraph_t_11.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1455. if os.path.isfile(f'{mkdir_path}/全盐量与电导率相关性分析图.png'):
  1456. doc.add_picture(f'{mkdir_path}/全盐量与电导率相关性分析图.png', width=Inches(6.0))
  1457. paragraph_t_12 = doc.add_paragraph()
  1458. paragraph_t_12.add_run(f'图12:全盐量与电导率相关性分析图').bold = True
  1459. paragraph_t_12.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1460. if os.path.isfile(f'{mkdir_path}/离子总量与水溶性盐总量关系图.png'):
  1461. doc.add_picture(f'{mkdir_path}/离子总量与水溶性盐总量关系图.png', width=Inches(6.0))
  1462. paragraph_t_13 = doc.add_paragraph()
  1463. paragraph_t_13.add_run(f'图13:水溶性盐总量与离子总量关系分析图').bold = True
  1464. paragraph_t_13.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1465. doc.add_heading('四、审核存疑数据', level=1)
  1466. paragraph_12 = doc.add_paragraph()
  1467. paragraph_12.add_run(f'表9:数据审核过程存疑数据一览表').bold = True
  1468. paragraph_12.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1469. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据审核过程存疑数据一览表.xlsx', level=4)
  1470. doc.add_heading('五、附表', level=1)
  1471. doc.add_heading('附表1:某区三普样品数量统计表(表层)', level=2)
  1472. # 插入附表1
  1473. table_1_f = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
  1474. table_1_f.alignment = WD_TABLE_ALIGNMENT.CENTER
  1475. # 遍历表格 插入数据
  1476. # 遍历表格的所有单元格,并填充内容
  1477. for i, row in enumerate(table_1_f.rows):
  1478. for j, cell in enumerate(row.cells):
  1479. # 获取单元格中的段落对象
  1480. paragraph = cell.paragraphs[0]
  1481. if i == 0:
  1482. r = paragraph.add_run(str(table_1_data.columns[j]))
  1483. r.font.bold = True
  1484. else:
  1485. r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
  1486. r.font.size = Pt(10.5)
  1487. r.font.name = 'Times New Roman'
  1488. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1489. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1490. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1491. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1492. # 合并单元格 合并第3列的第二行和第三行
  1493. if lenNum_1 > 1:
  1494. table_1_f.cell(2, 2).text = ''
  1495. table_1_f.cell(1, 2).merge(table_1_f.cell(2, 2))
  1496. doc.add_heading('附表2:各指标频度分析表', level=2)
  1497. # 插入表格 写入数据
  1498. table_f_2_data = table_f_2_data.replace(np.nan, '')
  1499. makeInfoTable(table_f_2_data, doc)
  1500. # table_f_2 = doc.add_table(rows=len(table_f_2_data) + 1, cols=6, style='Light Shading Accent 1')
  1501. # for i, row in enumerate(table_f_2.rows):
  1502. # for j, cell in enumerate(row.cells):
  1503. # # 获取单元格中的段落对象
  1504. # paragraph = cell.paragraphs[0]
  1505. # if i == 0:
  1506. # r = paragraph.add_run(str(table_f_2_data.columns[j]))
  1507. # r.font.bold = True
  1508. # else:
  1509. # r = paragraph.add_run(str(table_f_2_data.iloc[i - 1, j]))
  1510. # r.font.size = Pt(10.5)
  1511. # r.font.name = 'Times New Roman'
  1512. # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1513. # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1514. # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1515. # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1516. # doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:频度分析表.xlsx', level=4)
  1517. doc.add_heading('附表3:各指标频度分析图', level=2)
  1518. # 插入频度信息的图形
  1519. if os.path.isfile(f'{mkdir_path}/pH分析图.png'):
  1520. doc.add_picture(f'{mkdir_path}/pH分析图.png', width=Inches(6.0))
  1521. if os.path.isfile(f'{mkdir_path}/电导率分析图.png'):
  1522. doc.add_picture(f'{mkdir_path}/电导率分析图.png', width=Inches(6.0))
  1523. if os.path.isfile(f'{mkdir_path}/交换性钙分析图.png'):
  1524. doc.add_picture(f'{mkdir_path}/交换性钙分析图.png', width=Inches(6.0))
  1525. if os.path.isfile(f'{mkdir_path}/交换性钾分析图.png'):
  1526. doc.add_picture(f'{mkdir_path}/交换性钾分析图.png', width=Inches(6.0))
  1527. if os.path.isfile(f'{mkdir_path}/交换性镁分析图.png'):
  1528. doc.add_picture(f'{mkdir_path}/交换性镁分析图.png', width=Inches(6.0))
  1529. if os.path.isfile(f'{mkdir_path}/交换性钠分析图.png'):
  1530. doc.add_picture(f'{mkdir_path}/交换性钠分析图.png', width=Inches(6.0))
  1531. if os.path.isfile(f'{mkdir_path}/交换性盐基总量分析图.png'):
  1532. doc.add_picture(f'{mkdir_path}/交换性盐基总量分析图.png', width=Inches(6.0))
  1533. if os.path.isfile(f'{mkdir_path}/全盐量分析图.png'):
  1534. doc.add_picture(f'{mkdir_path}/全盐量分析图.png', width=Inches(6.0))
  1535. if os.path.isfile(f'{mkdir_path}/阳离子交换量分析图.png'):
  1536. doc.add_picture(f'{mkdir_path}/阳离子交换量分析图.png', width=Inches(6.0))
  1537. doc.add_heading('附表4:数值修约标准', level=2)
  1538. # 读取数据 插入表格 写入数据
  1539. numData = pd.read_excel('./img/数值修约要求.xlsx', sheet_name='Sheet1')
  1540. table_2_f = doc.add_table(rows=len(numData) + 1, cols=2, style='Light Shading Accent 1')
  1541. table_2_f.alignment = WD_TABLE_ALIGNMENT.CENTER
  1542. for i, row in enumerate(table_2_f.rows):
  1543. for j, cell in enumerate(row.cells):
  1544. # 获取单元格中的段落对象
  1545. paragraph = cell.paragraphs[0]
  1546. if i == 0:
  1547. r = paragraph.add_run(str(numData.columns[j]))
  1548. r.font.bold = True
  1549. else:
  1550. r = paragraph.add_run(str(numData.iloc[i - 1, j]))
  1551. r.font.size = Pt(10.5)
  1552. r.font.name = 'Times New Roman'
  1553. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1554. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1555. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1556. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1557. # 处理样式 遍历所有的段落 修改字体
  1558. # 遍历并打印每个段落的文本
  1559. paragraphs = doc.paragraphs
  1560. for paragraph in paragraphs:
  1561. for run in paragraph.runs:
  1562. run.font.color.rgb = RGBColor(0, 0, 0)
  1563. run.font.name = 'Times New Roman'
  1564. run.font.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1565. # run.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1566. # 保存Word文档
  1567. doc.save(f'{mkdir_path}/{areaName}审核报告.docx')
  1568. # 生成重金属指标审核报告
  1569. def getHeavyMetalIndicators(originData, data, type, changeFileUrl, saveFileUrl, check_1_data,
  1570. check_3_data,
  1571. check_5_data,
  1572. check_8_data, # 样品编号替换为编号
  1573. check_10_data,
  1574. check_12_data,
  1575. check_14_data):
  1576. # 生成报告
  1577. name = os.path.basename(changeFileUrl)
  1578. n = name.split('.')
  1579. areaName = n[0].replace('数据', '')
  1580. # 生成一个新的文件夹用于存放审核报告相关的数据
  1581. nowTime = time.strftime("%Y-%m-%d %H时%M分%S秒", time.localtime())
  1582. dir_name = f'{areaName}-{type}数据审核报告'
  1583. mkdir_path = saveFileUrl + '/' + dir_name + nowTime
  1584. if not os.path.exists(mkdir_path):
  1585. os.mkdir(mkdir_path)
  1586. heavyMetaData = data[indexClassificationList[type]]
  1587. heavyMetaDataNum = originData[indexClassificationList[type]]
  1588. report.getFrequencyImage(heavyMetaData, mkdir_path)
  1589. heavyMetaData['序号'] = data['序号']
  1590. heavyMetaData['原样品编号'] = data['原样品编号']
  1591. heavyMetaData['样品编号'] = data['样品编号']
  1592. heavyMetaData['地理位置'] = data['地理位置']
  1593. heavyMetaData['母质'] = data['母质']
  1594. heavyMetaData['土壤类型'] = data['土壤类型']
  1595. heavyMetaData['土地利用类型'] = data['土地利用类型']
  1596. # heavyMetaData['土壤质地'] = data['土壤质地']
  1597. heavyMetaData['原样品编号'] = heavyMetaData['原样品编号'].astype(str)
  1598. # checkData = pd.read_excel(changeFileUrl, sheet_name='检测方法')
  1599. heavyMetaDataNum['序号'] = originData['序号']
  1600. heavyMetaDataNum['原样品编号'] = originData['原样品编号']
  1601. heavyMetaDataNum['样品编号'] = originData['样品编号']
  1602. heavyMetaDataNum['地理位置'] = originData['地理位置']
  1603. heavyMetaDataNum['母质'] = originData['母质']
  1604. heavyMetaDataNum['土壤类型'] = originData['土壤类型']
  1605. heavyMetaDataNum['土地利用类型'] = originData['土地利用类型']
  1606. # heavyMetaData['土壤质地'] = data['土壤质地']
  1607. heavyMetaDataNum['原样品编号'] = heavyMetaDataNum['原样品编号'].astype(str)
  1608. # 上面这个地址,可以纯递给函数中,用于保存表格和图片
  1609. # 调用函数 开始生成报告相关内容
  1610. # 表1相关数据
  1611. typeData = report.getSimpleNum(heavyMetaData)
  1612. lenNum_1 = len(typeData['sData'])
  1613. lenNum_1_f = len(typeData['allData'])
  1614. table_1_data = pd.DataFrame({
  1615. '类型': typeData['sData'].index,
  1616. '数量': typeData['sData'],
  1617. '合计': [typeData['sData'].sum() for _ in range(lenNum_1)]
  1618. })
  1619. # 表2数据
  1620. table_2_data = report.getDataComplete(heavyMetaData)
  1621. table_2_data = table_2_data.reset_index()
  1622. table_2_data.columns = ['指标名称', '实测数量', '应测数量']
  1623. # 表3数据
  1624. # table_3_data = report.checkMethod(checkData, mkdir_path)
  1625. # 数据修约 表4
  1626. report.getNum(heavyMetaDataNum, mkdir_path)
  1627. # 数据填报项审核 表5
  1628. report.dataReportResult(heavyMetaData, mkdir_path)
  1629. # 表6数据 土壤质地类型不一致
  1630. middData = heavyMetaData[['原样品编号', '样品编号']].astype(str)
  1631. middData['编号'] = middData['原样品编号']
  1632. del middData['原样品编号']
  1633. check_1_data = pd.merge(check_1_data, middData, how='left', on='编号')
  1634. check_1_data = check_1_data.replace(np.nan, '')
  1635. # typeNotSame = check_1_data[check_1_data['土壤质地'] != check_1_data['土壤类型(判断)']]
  1636. # table_6_data = typeNotSame[['编号', '样品编号', '土壤质地', '土壤类型(判断)']]
  1637. allNeedData = pd.DataFrame({})
  1638. allNeedData['原样品编号'] = check_1_data['编号']
  1639. getSimpleDataNumber = pd.merge(allNeedData, heavyMetaData[['原样品编号', '样品编号']], how='left', on="原样品编号")
  1640. allNeedData['样品编号'] = getSimpleDataNumber['样品编号']
  1641. allNeedData['土地利用类型'] = check_1_data['土地利用类型']
  1642. allNeedData['审核结果'] = check_14_data['审核结果']
  1643. allNeedData['外业'] = ['' for _ in range(len(check_1_data))]
  1644. table_7_data = allNeedData[allNeedData['审核结果'] != '']
  1645. del table_7_data['审核结果']
  1646. # 写进表格
  1647. with pd.ExcelWriter(f'{mkdir_path}/超阈值样品统计表.xlsx', engine='openpyxl') as writer:
  1648. table_7_data.to_excel(writer, index=False, sheet_name='超阈值数据')
  1649. # 表8数据
  1650. table_8_data = report.getPHData(heavyMetaData, mkdir_path)
  1651. # 表12数据 重金属超标
  1652. caOverData = pd.merge(check_1_data[['编号', '土地利用类型']], check_14_data[
  1653. ['编号', 'pH', '镉mg/kg', '汞mg/kg', '砷mg/kg', '铅mg/kg', '铬mg/kg', '镍mg/kg', '审核结果']], how='outer',
  1654. on=['编号'])
  1655. caOverData['原样品编号'] = caOverData['编号']
  1656. caOverData = pd.merge(caOverData, heavyMetaData[['原样品编号', '样品编号']], how='left', on='原样品编号')
  1657. first_column = caOverData.pop('样品编号')
  1658. caOverData.insert(0, '样品编号', first_column)
  1659. caOverData_need = caOverData[caOverData['审核结果'] != '']
  1660. # 写进表格
  1661. with pd.ExcelWriter(f'{mkdir_path}/重金属超筛选值情况统计.xlsx', engine='openpyxl') as writer:
  1662. caOverData_need.to_excel(writer, index=False, sheet_name='重金属超筛选值情况统计')
  1663. # 表13 所有存疑数据
  1664. with pd.ExcelWriter(f'{mkdir_path}/数据审核过程存疑数据一览表.xlsx', engine='openpyxl') as writer:
  1665. allNeedData[allNeedData['审核结果'] != ''].to_excel(writer, index=False, sheet_name='存疑数据')
  1666. # 附表: 频度分析图
  1667. # report.getFrequencyImage(heavyMetaData, mkdir_path)
  1668. table_f_2_data = report.getFrequencyInformation(data, mkdir_path)
  1669. # 新建一个文档
  1670. doc = Document()
  1671. # 添加标题
  1672. doc.add_heading(f"{areaName}第三次全国土壤普查数据审核报告", level=0)
  1673. # 添加一级标题
  1674. doc.add_heading('一、数据完整性审核', level=1)
  1675. doc.add_heading('1、土地利用类型与检测指标符合性审核', level=2)
  1676. # 插入表格1
  1677. paragraph_1 = doc.add_paragraph()
  1678. paragraph_1.add_run(f"表1:{areaName}三普样品数量统计表(表层)").bold = True
  1679. # 设置居中
  1680. paragraph_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1681. table_1 = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
  1682. table_1.alignment = WD_TABLE_ALIGNMENT.CENTER
  1683. # 遍历表格 插入数据
  1684. # 遍历表格的所有单元格,并填充内容
  1685. for i, row in enumerate(table_1.rows):
  1686. for j, cell in enumerate(row.cells):
  1687. # 获取单元格中的段落对象
  1688. paragraph = cell.paragraphs[0]
  1689. if i == 0:
  1690. r = paragraph.add_run(str(table_1_data.columns[j]))
  1691. r.font.bold = True
  1692. else:
  1693. r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
  1694. r.font.size = Pt(10.5)
  1695. r.font.name = 'Times New Roman'
  1696. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1697. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1698. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1699. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1700. # 合并单元格 合并第3列的第二行和第三行
  1701. if lenNum_1 > 1:
  1702. table_1.cell(2, 2).text = ''
  1703. table_1.cell(1, 2).merge(table_1.cell(2, 2))
  1704. ############test##############
  1705. doc.add_heading('2、指标名称与实际检测样品数量完整性审核', level=2)
  1706. # 插入表格2
  1707. paragraph_2 = doc.add_paragraph()
  1708. paragraph_2.add_run(f'表2:{areaName}指标名称与实际检测样品数量统计表').bold = True
  1709. table_2 = doc.add_table(rows=len(table_2_data) + 1, cols=3, style='Light Shading Accent 1')
  1710. paragraph_2.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1711. table_2.alignment = WD_TABLE_ALIGNMENT.CENTER
  1712. for i, row in enumerate(table_2.rows):
  1713. for j, cell in enumerate(row.cells):
  1714. # 获取单元格中的段落对象
  1715. paragraph = cell.paragraphs[0]
  1716. if i == 0:
  1717. r = paragraph.add_run(str(table_2_data.columns[j]))
  1718. r.font.bold = True
  1719. else:
  1720. r = paragraph.add_run(str(table_2_data.iloc[i - 1, j]))
  1721. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1722. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1723. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1724. r.font.size = Pt(10.5)
  1725. r.font.name = 'Times New Roman'
  1726. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1727. doc.add_heading('二、数据规范性审核', level=1)
  1728. doc.add_heading('1、数据填报规范性审核', level=2)
  1729. # 插入表3
  1730. paragraph_3 = doc.add_paragraph()
  1731. paragraph_3.add_run(f'表3:{areaName}土壤检测数据检测方法填报审核结果表').bold = True
  1732. # table_3 = doc.add_table(rows=2, cols=2)
  1733. paragraph_3.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1734. # table_3.alignment = WD_TABLE_ALIGNMENT.CENTER
  1735. # 写入数据 这里数据写不下 嵌入链接
  1736. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:检测方法审核结果.xlsx', level=4)
  1737. doc.add_heading('2、数值修约规范性审核', level=2)
  1738. # 插入表4
  1739. paragraph_4 = doc.add_paragraph()
  1740. paragraph_4.add_run(f'表4:{areaName}土壤检测数据数值修约结果表').bold = True
  1741. # table_4 = doc.add_table(rows=2, cols=2)
  1742. paragraph_4.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1743. # table_4.alignment = WD_TABLE_ALIGNMENT.CENTER
  1744. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数值修约审核.xlsx', level=4)
  1745. # 填入数据 这里数据也放不下 嵌入链接
  1746. doc.add_heading('3、数据未检出的填报规范性审核', level=2)
  1747. # 插入表5
  1748. paragraph_5 = doc.add_paragraph()
  1749. paragraph_5.add_run(f'表5:{areaName}土壤检测数据未检出项填报审核结果表').bold = True
  1750. # table_5 = doc.add_table(rows=2, cols=2)
  1751. paragraph_5.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1752. # table_5.alignment = WD_TABLE_ALIGNMENT.CENTER
  1753. # 写入数据 这里数据也放不下 嵌入链接
  1754. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据填报项审核结果.xlsx', level=4)
  1755. # doc.add_heading('4、土壤质地填报规范性审核', level=2)
  1756. # 插入表6
  1757. # paragraph_6 = doc.add_paragraph()
  1758. # paragraph_6.add_run(f'表6:{areaName}土壤质地填报审核结果表').bold = True
  1759. # table_6 = doc.add_table(rows=len(table_6_data) + 1, cols=4, style='Light Shading Accent 1')
  1760. # paragraph_6.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1761. # table_6.alignment = WD_TABLE_ALIGNMENT.CENTER
  1762. # # 提取结果表中数据
  1763. # # 写入数据 土壤质地类型不一致的数据提取出来
  1764. # for i, row in enumerate(table_6.rows):
  1765. # for j, cell in enumerate(row.cells):
  1766. # # 获取单元格中的段落对象
  1767. # paragraph = cell.paragraphs[0]
  1768. # if i == 0:
  1769. # r = paragraph.add_run(str(table_6_data.columns[j]))
  1770. # r.font.bold = True
  1771. # else:
  1772. # r = paragraph.add_run(str(table_6_data.iloc[i - 1, j]))
  1773. # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1774. # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1775. # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1776. # r.font.size = Pt(10.5)
  1777. # r.font.name = 'Times New Roman'
  1778. # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1779. doc.add_heading('三、数据合理性审核', level=1)
  1780. doc.add_heading('1、阈值法审核', level=2)
  1781. # 插入表格
  1782. paragraph_7 = doc.add_paragraph()
  1783. paragraph_7.add_run(f'表6:{areaName}土壤检测数据超阈值样品统计表').bold = True
  1784. # table_7 = doc.add_table(rows=2, cols=2)
  1785. # paragraph_7.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1786. # table_7.alignment = WD_TABLE_ALIGNMENT.CENTER
  1787. # 写入数据 点击查看数据 这里也不一定写的下 最好是嵌入链接
  1788. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据审核过程存疑数据一览表.xlsx', level=4)
  1789. # todo 合并所有数据 审核结果不为空的数据 写入表格保存到指定文件夹
  1790. doc.add_heading('2、极值法审核', level=2)
  1791. doc.add_heading('(1)pH', level=3)
  1792. # 插入ph分布图
  1793. if os.path.isfile(f'{mkdir_path}/pH值分布图.png'):
  1794. doc.add_picture(f'{mkdir_path}/pH值分布图.png', width=Inches(6.0))
  1795. paragraph_t_1 = doc.add_paragraph()
  1796. paragraph_t_1.add_run(f'图1:pH值分布情况').bold = True
  1797. paragraph_t_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1798. # 插入频度统计表
  1799. paragraph_8 = doc.add_paragraph()
  1800. paragraph_8.add_run('表7:pH数据统计表').bold = True
  1801. table_8 = doc.add_table(rows=6, cols=2, style='Light Shading Accent 1')
  1802. t_8 = table_8_data['频度分析']
  1803. t_8 = t_8.reset_index()
  1804. t_8.columns = ['指标', '数据']
  1805. paragraph_8.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1806. table_8.alignment = WD_TABLE_ALIGNMENT.CENTER
  1807. for i, row in enumerate(table_8.rows):
  1808. for j, cell in enumerate(row.cells):
  1809. # 获取单元格中的段落对象
  1810. paragraph = cell.paragraphs[0]
  1811. if i == 0:
  1812. r = paragraph.add_run(str(t_8.columns[j]))
  1813. r.font.bold = True
  1814. else:
  1815. r = paragraph.add_run(str(t_8.iloc[i - 1, j]))
  1816. r.font.size = Pt(10.5)
  1817. r.font.name = 'Times New Roman'
  1818. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1819. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1820. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1821. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1822. # 插入异常数据提取表格 todo 这里数据多的话也可能写不下 最好是嵌入一下
  1823. if not table_8_data['异常数据'].empty:
  1824. paragraph_9 = doc.add_paragraph()
  1825. paragraph_9.add_run('表8:pH异常数据统计表').bold = True
  1826. table_9 = doc.add_table(rows=len(table_8_data['异常数据']) + 1, cols=6, style='Light Shading Accent 1')
  1827. t_9 = table_8_data['异常数据']
  1828. paragraph_9.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1829. table_9.alignment = WD_TABLE_ALIGNMENT.CENTER
  1830. for i, row in enumerate(table_9.rows):
  1831. for j, cell in enumerate(row.cells):
  1832. # 获取单元格中的段落对象
  1833. paragraph = cell.paragraphs[0]
  1834. if i == 0:
  1835. r = paragraph.add_run(str(t_9.columns[j]))
  1836. r.font.bold = True
  1837. else:
  1838. r = paragraph.add_run(str(t_9.iloc[i - 1, j]))
  1839. r.font.size = Pt(10.5)
  1840. r.font.name = 'Times New Roman'
  1841. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1842. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1843. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1844. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1845. doc.add_heading('4、指标综合分析', level=2)
  1846. doc.add_heading('表9:重金属超筛选值情况统计', level=4)
  1847. # todo 获取重金属数据
  1848. doc.add_heading('四、审核存疑数据', level=1)
  1849. paragraph_12 = doc.add_paragraph()
  1850. paragraph_12.add_run(f'表10:数据审核过程存疑数据一览表').bold = True
  1851. paragraph_12.alignment = WD_ALIGN_PARAGRAPH.CENTER
  1852. doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据审核过程存疑数据一览表.xlsx', level=4)
  1853. doc.add_heading('五、附表', level=1)
  1854. doc.add_heading('附表1:某区三普样品数量统计表(表层)', level=2)
  1855. # 插入附表1
  1856. table_1_f = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
  1857. table_1_f.alignment = WD_TABLE_ALIGNMENT.CENTER
  1858. # 遍历表格 插入数据
  1859. # 遍历表格的所有单元格,并填充内容
  1860. for i, row in enumerate(table_1_f.rows):
  1861. for j, cell in enumerate(row.cells):
  1862. # 获取单元格中的段落对象
  1863. paragraph = cell.paragraphs[0]
  1864. if i == 0:
  1865. r = paragraph.add_run(str(table_1_data.columns[j]))
  1866. r.font.bold = True
  1867. else:
  1868. r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
  1869. r.font.size = Pt(10.5)
  1870. r.font.name = 'Times New Roman'
  1871. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1872. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1873. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1874. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1875. # 合并单元格 合并第3列的第二行和第三行
  1876. if lenNum_1 > 1:
  1877. table_1_f.cell(2, 2).text = ''
  1878. table_1_f.cell(1, 2).merge(table_1_f.cell(2, 2))
  1879. doc.add_heading('附表2:各指标频度分析表', level=2)
  1880. # 插入表格 写入数据
  1881. table_f_2_data = table_f_2_data.replace(np.nan, '')
  1882. makeInfoTable(table_f_2_data, doc)
  1883. # table_f_2 = doc.add_table(rows=len(table_f_2_data) + 1, cols=6, style='Light Shading Accent 1')
  1884. # for i, row in enumerate(table_f_2.rows):
  1885. # for j, cell in enumerate(row.cells):
  1886. # # 获取单元格中的段落对象
  1887. # paragraph = cell.paragraphs[0]
  1888. # if i == 0:
  1889. # r = paragraph.add_run(str(table_f_2_data.columns[j]))
  1890. # r.font.bold = True
  1891. # else:
  1892. # r = paragraph.add_run(str(table_f_2_data.iloc[i - 1, j]))
  1893. # r.font.size = Pt(10.5)
  1894. # r.font.name = 'Times New Roman'
  1895. # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1896. # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1897. # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1898. # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1899. # doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:频度分析表.xlsx', level=4)
  1900. doc.add_heading('附表3:各指标频度分析图', level=2)
  1901. # 插入频度信息的图形
  1902. if os.path.isfile(f'{mkdir_path}/pH分析图.png'):
  1903. doc.add_picture(f'{mkdir_path}/pH分析图.png', width=Inches(6.0))
  1904. if os.path.isfile(f'{mkdir_path}/总镉分析图.png'):
  1905. doc.add_picture(f'{mkdir_path}/总镉分析图.png', width=Inches(6.0))
  1906. if os.path.isfile(f'{mkdir_path}/总铬分析图.png'):
  1907. doc.add_picture(f'{mkdir_path}/总铬分析图.png', width=Inches(6.0))
  1908. if os.path.isfile(f'{mkdir_path}/总汞分析图.png'):
  1909. doc.add_picture(f'{mkdir_path}/总汞分析图.png', width=Inches(6.0))
  1910. if os.path.isfile(f'{mkdir_path}/总镍分析图.png'):
  1911. doc.add_picture(f'{mkdir_path}/总镍分析图.png', width=Inches(6.0))
  1912. if os.path.isfile(f'{mkdir_path}/总砷分析图.png'):
  1913. doc.add_picture(f'{mkdir_path}/总砷分析图.png', width=Inches(6.0))
  1914. if os.path.isfile(f'{mkdir_path}/总铅分析图.png'):
  1915. doc.add_picture(f'{mkdir_path}/总铅分析图.png', width=Inches(6.0))
  1916. doc.add_heading('附表4:数值修约标准', level=2)
  1917. # 读取数据 插入表格 写入数据
  1918. numData = pd.read_excel('./img/数值修约要求.xlsx', sheet_name='Sheet1')
  1919. table_2_f = doc.add_table(rows=len(numData) + 1, cols=2, style='Light Shading Accent 1')
  1920. table_2_f.alignment = WD_TABLE_ALIGNMENT.CENTER
  1921. for i, row in enumerate(table_2_f.rows):
  1922. for j, cell in enumerate(row.cells):
  1923. # 获取单元格中的段落对象
  1924. paragraph = cell.paragraphs[0]
  1925. if i == 0:
  1926. r = paragraph.add_run(str(numData.columns[j]))
  1927. r.font.bold = True
  1928. else:
  1929. r = paragraph.add_run(str(numData.iloc[i - 1, j]))
  1930. r.font.size = Pt(10.5)
  1931. r.font.name = 'Times New Roman'
  1932. r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1933. paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  1934. paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
  1935. paragraph.paragraph_format.line_spacing = 1 # 段落行间距
  1936. # 处理样式 遍历所有的段落 修改字体
  1937. # 遍历并打印每个段落的文本
  1938. paragraphs = doc.paragraphs
  1939. for paragraph in paragraphs:
  1940. for run in paragraph.runs:
  1941. run.font.color.rgb = RGBColor(0, 0, 0)
  1942. run.font.name = 'Times New Roman'
  1943. run.font.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1944. # run.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
  1945. # 保存Word文档
  1946. doc.save(f'{mkdir_path}/{areaName}-{type}审核报告.docx')
  1947. def number_to_chinese_year(number):
  1948. # 定义数字到中文大写的映射
  1949. chinese_numerals = {'0': '〇', '1': '一', '2': '二', '3': '三',
  1950. '4': '四', '5': '五', '6': '六', '7': '七',
  1951. '8': '八', '9': '九'}
  1952. # 将数字逐个字符转换为中文大写
  1953. return ''.join(chinese_numerals[digit] for digit in str(number))