partReport.py 125 KB

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