12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375 |
- import pandas as pd
- import numpy as np
- import time
- import os
- from docx.shared import Inches
- from docx.shared import Pt, RGBColor
- from docx import Document
- from docx.enum.table import WD_TABLE_ALIGNMENT, WD_CELL_VERTICAL_ALIGNMENT
- from docx.oxml.ns import qn
- from docx.enum.text import WD_ALIGN_PARAGRAPH
- from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
- import report
- from datetime import datetime
- # import chinese_converter # 需要安装这个库来转换数字为中文大写
- import cn2an
- # 频度信息表生成
- def makeInfoTable(data, doc):
- rows = (int(len(data.columns) / 6) + 1)
- columnsList = np.arange(0, rows * 6, 6)
- dataList = []
- for i in columnsList:
- res = data.iloc[:, i:i + 6]
- res = res.reset_index()
- dataList.append(res)
- table_f_2 = doc.add_table(rows=rows * 6, cols=7, style='Light Shading Accent 1')
- for i, row in enumerate(table_f_2.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == columnsList[0]:
- # 第一行 显示前6个指标的列名
- if len(dataList[0].columns) > j:
- r = paragraph.add_run(dataList[0].columns[j])
- r.font.bold = True
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif len(columnsList) > 1 and i > columnsList[0] and i < columnsList[1]:
- if len(dataList[0].columns) > j:
- r = paragraph.add_run(str(dataList[0].iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif i == columnsList[1]:
- # 第6行 显示前6个指 标的列名
- if len(dataList[1].columns) > j:
- r = paragraph.add_run(dataList[1].columns[j])
- r.font.bold = True
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif len(columnsList) > 2 and i > columnsList[1] and i < columnsList[2]:
- if len(dataList[1].columns) > j:
- r = paragraph.add_run(str(dataList[1].iloc[i - 7, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif i == columnsList[2]:
- # 第6*2行 显示前6个指 标的列名
- if len(dataList[2].columns) > j:
- r = paragraph.add_run(dataList[2].columns[j])
- r.font.bold = True
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif len(columnsList) > 3 and i > columnsList[2] and i < columnsList[3]:
- if len(dataList[2].columns) > j:
- r = paragraph.add_run(str(dataList[2].iloc[i - 13, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif i == columnsList[3]:
- # 第6*3行 显示前6个指 标的列名
- if len(dataList[3].columns) > j:
- r = paragraph.add_run(dataList[3].columns[j])
- r.font.bold = True
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif len(columnsList) > 4 and i > columnsList[3] and i < columnsList[4]:
- if len(dataList[3].columns) > j:
- r = paragraph.add_run(str(dataList[3].iloc[i - 19, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif i == columnsList[4]:
- # 第6*4行 显示前6个指 标的列名
- if len(dataList[4].columns) > j:
- r = paragraph.add_run(dataList[4].columns[j])
- r.font.bold = True
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif len(columnsList) > 5 and i > columnsList[4] and i < columnsList[5]:
- if len(dataList[4].columns) > j:
- r = paragraph.add_run(str(dataList[4].iloc[i - 25, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif i == columnsList[5]:
- # 第6*5行 显示前6个指 标的列名
- if len(dataList[5].columns) > j:
- r = paragraph.add_run(dataList[5].columns[j])
- r.font.bold = True
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif len(columnsList) > 6 and i > columnsList[5] and i < columnsList[6]:
- if len(dataList[5].columns) > j:
- r = paragraph.add_run(str(dataList[5].iloc[i - 31, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif i == columnsList[6]:
- # 第6*6行 显示前6个指 标的列名
- if len(dataList[6].columns) > j:
- r = paragraph.add_run(dataList[6].columns[j])
- r.font.bold = True
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif len(columnsList) > 7 and i > columnsList[6] and i < columnsList[7]:
- if len(dataList[6].columns) > j:
- r = paragraph.add_run(str(dataList[6].iloc[i - 37, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif i == columnsList[7]:
- # 第6*7行 显示前6个指 标的列名
- if len(dataList[7].columns) > j:
- r = paragraph.add_run(dataList[7].columns[j])
- r.font.bold = True
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif len(columnsList) >= 8 and i > columnsList[7] and i < columnsList[8]:
- if len(dataList[7].columns) > j:
- r = paragraph.add_run(str(dataList[7].iloc[i - 43, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif i == columnsList[8]:
- if len(dataList[8].columns) > j:
- # 第6*8行 显示前6个指 标的列名
- r = paragraph.add_run(dataList[8].columns[j])
- r.font.bold = True
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif len(columnsList) >= 9 and i > columnsList[8] and i < columnsList[9]:
- if len(dataList[8].columns) > j:
- r = paragraph.add_run(str(dataList[8].iloc[i - 49, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif i == columnsList[9]:
- # 第6*9行 显示前6个指 标的列名
- if len(dataList[9].columns) > j:
- r = paragraph.add_run(dataList[9].columns[j])
- r.font.bold = True
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- elif len(columnsList) >= 10 and i > columnsList[9] and i <= 60:
- if len(dataList[9].columns) > j:
- r = paragraph.add_run(str(dataList[9].iloc[i - 55, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- else:
- paragraph.add_run('')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 生成子报告: 物理指标 常规养分指标 一般化学性指标 重金属指标
- indexClassificationList = {
- '物理指标': ['土壤质地', '土壤容重1(g/cm³)', '土壤容重2(g/cm³)', '土壤容重3(g/cm³)', '土壤容重4(g/cm³)', '土壤容重平均值(g/cm³)',
- '2~0.2mm颗粒含量', '0.2~0.02mm颗粒含量', '0.02~0.002mm颗粒含量', '0.002mm以下颗粒含量', '水稳>5mm(%)', '水稳3mm~5mm(%)',
- '水稳2mm~3mm(%)', '水稳1mm~2mm(%)', '水稳0.5mm~1mm(%)', '水稳0.25mm~0.5mm(%)', '水稳性大团聚体总和(%)', '洗失量(吸管法需填)', '风干试样含水量(分析基)'],
- '常规养分指标': ['有机质', '全氮', '全磷', '全钾', '有效磷', '速效钾', '有效硫', '有效硼', '有效铁', '有效锰', '有效铜', '有效锌', '有效钼', '有效硅', '缓效钾'],
- '一般化学性指标': ['pH','阳离子交换量', '交换性盐基总量', '交换性钙', '交换性镁', '交换性钠', '交换性钾', '全盐量', '电导率',
- '水溶性Na⁺含量', '水溶性K⁺含量', '水溶性Ca²⁺含量', '水溶性Mg²⁺含量', '水溶性Cl⁻含量', '水溶性CO₃²⁻含量','水溶性HCO₃⁻含量',
- '水溶性SO₄²⁻含量', '离子总量', '碳酸钙'],
- '重金属指标': [ '总汞', '总砷', '总铅', '总镉', '总铬', '总镍']
- }
- # 生成物理指标审核报告
- def getphysicsReport(originData,data,type, changeFileUrl, saveFileUrl, check_1_data,
- check_3_data,
- check_5_data ,
- check_8_data, # 样品编号替换为编号
- check_10_data,
- check_12_data,
- check_14_data ):
- """
- :param type: 指标类型
- :param changeFileUrl: 选择的数据文件路径
- :param saveFileUrl: 保存的文件路径
- :param check_1_data: 土壤容重数据
- :param check_3_data: 水稳审核数据
- :param check_5_data: 盐离子数据
- :param check_8_data: 水溶性离子数据
- :param check_10_data: 有机质及氮磷钾数据
- :param check_12_data: 有效养分数据
- :param check_14_data: 重金属数据
- :return:
- """
- # 生成报告
- name = os.path.basename(changeFileUrl)
- n = name.split('.')
- areaName = n[0].replace('数据', '')
- # 生成一个新的文件夹用于存放审核报告相关的数据
- nowTime = time.strftime("%Y-%m-%d %H时%M分%S秒", time.localtime())
- dir_name = f'{areaName}-{type}数据审核报告'
- mkdir_path = saveFileUrl + '/' + dir_name + nowTime
- if not os.path.exists(mkdir_path):
- os.mkdir(mkdir_path)
- # 获取相应指标数据
- physicsData = data[indexClassificationList[type]]
- physicsDataNum = originData[indexClassificationList[type]]
- report.getFrequencyImage(physicsData, mkdir_path)
- physicsData['序号'] = data['序号']
- physicsData['原样品编号'] = data['原样品编号']
- physicsData['样品编号'] = data['样品编号']
- physicsData['地理位置'] = data['地理位置']
- physicsData['母质'] = data['母质']
- physicsData['土壤类型'] = data['土壤类型']
- physicsData['土地利用类型'] = data['土地利用类型']
- physicsData['土壤质地'] = data['土壤质地']
- physicsDataNum['序号'] = originData['序号']
- physicsDataNum['原样品编号'] = originData['原样品编号']
- physicsDataNum['样品编号'] = originData['样品编号']
- physicsDataNum['地理位置'] = originData['地理位置']
- physicsDataNum['母质'] = originData['母质']
- physicsDataNum['土壤类型'] = originData['土壤类型']
- physicsDataNum['土地利用类型'] = originData['土地利用类型']
- physicsDataNum['土壤质地'] = originData['土壤质地']
- physicsDataNum['原样品编号'] = originData['原样品编号'].astype(str)
- # 生成相应审核报告
- # 根据选择的路径读取数据
- physicsData['原样品编号'] = physicsData['原样品编号'].astype(str)
- # todo 有数据后这里去掉注释
- # checkData = pd.read_excel(changeFileUrl, sheet_name='检测方法')
- # 上面这个地址,可以传递给函数中,用于保存表格和图片
- # 调用函数 开始生成报告相关内容
- # 表1相关数据
- typeData = report.getSimpleNum(physicsData)
- lenNum_1 = len(typeData['sData'])
- lenNum_1_f = len(typeData['allData'])
- table_1_data = pd.DataFrame({
- '类型': typeData['sData'].index,
- '数量': typeData['sData'],
- '合计': [typeData['sData'].sum() for _ in range(lenNum_1)]
- })
- # 表2数据
- table_2_data = report.getDataComplete(physicsData)
- table_2_data = table_2_data.reset_index()
- table_2_data.columns = ['指标名称', '实测数量', '应测数量']
- # 表3数据
- # table_3_data = report.checkMethod(checkData, mkdir_path)
- # 数据修约 表4
- report.getNum(physicsDataNum, mkdir_path)
- # 数据填报项审核 表5
- report.dataReportResult(physicsData, mkdir_path)
- # 表6数据 土壤质地类型不一致
- # middData = physicsData[['原样品编号', '样品编号']].astype(str)
- # middData['编号'] = middData['原样品编号']
- # del middData['原样品编号']
- # check_1_data = pd.merge(check_1_data, middData, how='left', on='编号')
- check_1_data = check_1_data.replace(np.nan, '')
- #typeNotSame = check_1_data[check_1_data['土壤质地'] != check_1_data['土壤质地(判断)']]
- #table_6_data = typeNotSame[['编号', '样品编号', '土壤质地', '土壤质地(判断)']]
- allNeedData = pd.DataFrame({})
- allNeedData['原样品编号'] = check_1_data['编号']
- getSimpleDataNumber = pd.merge(allNeedData, physicsData[['原样品编号', '样品编号']], how='left', on="原样品编号")
- allNeedData['样品编号'] = getSimpleDataNumber['样品编号']
- allNeedData['土地利用类型'] = check_1_data['土地利用类型']
- allNeedData['审核结果'] = check_1_data['审核结果'] + check_3_data['审核结果']
- allNeedData['外业'] = ['' for _ in range(len(check_1_data))]
- table_7_data = allNeedData[allNeedData['审核结果'] != '']
- del table_7_data['审核结果']
- # 写进表格
- with pd.ExcelWriter(f'{mkdir_path}/超阈值样品统计表.xlsx', engine='openpyxl') as writer:
- table_7_data.to_excel(writer, index=False, sheet_name='超阈值数据')
- # 表8数据
- #table_8_data = report.getPHData(physicsData, mkdir_path)
- # 表13 所有存疑数据
- with pd.ExcelWriter(f'{mkdir_path}/数据审核过程存疑数据一览表.xlsx', engine='openpyxl') as writer:
- allNeedData[allNeedData['审核结果'] != ''].to_excel(writer, index=False, sheet_name='存疑数据')
- # 附表: 频度分析图
- # report.getFrequencyImage(physicsData, mkdir_path)
- table_f_2_data = report.getFrequencyInformation(data, mkdir_path)
- # 新建一个文档
- doc = Document()
- # 添加标题
- doc.add_heading(f"{areaName}第三次全国土壤普查物理指标检测数据审核报告", level=0)
- # 在文档中添加封面段落
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- # 插入图片,设置宽度为6英寸(可根据需求调整)
- run = fm.add_run()
- run.add_picture('img/第三次全国土壤普查img.png', width=Inches(2.26))
- fm.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 在文档中添加封面段落
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- # 获取当前日期
- current_date = datetime.now()
- # 将年份和月份转换为中文大写数字
- year = int(current_date.strftime("%Y")) # 转换为整数
- month = int(current_date.strftime("%m")) # 转换为整数
- # 使用 cn2an 将数字转换为中文大写
- year_chinese = number_to_chinese_year(year) # 年份转换
- month_chinese = cn2an.an2cn(month) # 月份转换
- current_date_formatted = f"{year_chinese}年{month_chinese}月"
- # 组合动态文本
- dynamic_text = f"安徽农业大学资源与环境学院\n{current_date_formatted}"
- # 添加文字并居中
- text_paragraph = doc.add_paragraph()
- text_run = text_paragraph.add_run(dynamic_text)
- text_run.font.name = "宋体"
- text_run.font.size = Pt(18)
- text_run.bold = True # 设置字体加粗
- text_paragraph.alignment = 1 # 1 表示居中对齐
- # 正确插入分页符
- doc.add_page_break()
- heading = doc.add_heading('总体概述', level=1)
- heading.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # 第一段
- long_text1 = f"""
- {areaName}第三次全国土壤普查县级数据审核报告主要通过收集和整理相关数据,并对其进行内业检测数据的完整性、规范性和合理性进行审核,形成存疑样点清单及存疑样点结果判定,最终编制完成数据审核报告,同时提交( )对相关指标进行整改复测。报告整理了( )个表层样品数据(含平行样、质控样)、( )个水稳性大团聚体样品数据(含平行样)、( )个剖面样品数据(含平行样、质控样),共( )次样品检测结果分析情况。相关结果分别按照物理性指标检测数据、一般化学指标检测数据、常规养分指标检测数据和重金属指标检测数据形成四份报告。本报告为表层样常规养分指标检测数据审核报告。
- """
- para0 = doc.add_paragraph(long_text1)
- run0 = para0.runs[0] # 获取段落中的第一个run对象
- run0.font.name = '宋体' # 设置字体为宋体
- run0.font.size = Pt(11) # 设置字号为11磅
- # 设置段落的行间距为1.5倍
- para_format = para0.paragraph_format
- para_format.line_spacing = 1.5 # 设置行间距为1.5倍
- # 添加一级标题
- doc.add_heading('一、数据完整性审核', level=1)
- # 第二段
- long_text2 = """
- 外业信息调查采样环节:采用电子围栏和外业调查采样APP,对采样位置和填报信息进行管理,确保外业调查信息填报完整。
- 样品检测数据上报环节:通过土壤普查工作平台对上报数据的完整性进行筛查。( )第三次土壤普查相关指标检测数据由( )提供,数据均已通过省级质控实验室和县级土壤普查办审核;相关土壤指标历史数据则由( )第三次土壤普查办公室提供。根据《第三次全国土壤普查土壤样品制备与检测技术规范(修订版)》要求,统计各土地利用类型的样品数量,并按照耕地园地土壤样品(表层/剖面)、林地草地土壤样品(表层/剖面)以及水稳定性大团聚体样品(见表1)进行分类,编制了指标名称与实际检测样品数量统计表(见表2),其中水溶性盐分总量大于( ),增加检测了八大离子(该指标在化学指标检测数据审核报告内)。
- """
- para = doc.add_paragraph(long_text2)
- run1 = para.runs[0]
- run1.font.name = '宋体' # 设置字体为宋体
- run1.font.size = Pt(11) # 设置字号为11磅
- # 设置段落的行间距为1.5倍
- para_format = para.paragraph_format
- para_format.line_spacing = 1.5 # 设置行间距为1.5倍
- doc.add_heading('1、土地利用类型与检测指标符合性审核', level=2)
- # 插入表格1
- paragraph_1 = doc.add_paragraph()
- paragraph_1.add_run(f"表1:{areaName}三普样品数量统计表(表层)").bold = True
- # 设置居中
- paragraph_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
- table_1 = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
- table_1.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 遍历表格 插入数据
- # 遍历表格的所有单元格,并填充内容
- for i, row in enumerate(table_1.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_1_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 合并单元格 合并第3列的第二行和第三行
- if lenNum_1 > 1:
- table_1.cell(2, 2).text = ''
- table_1.cell(1, 2).merge(table_1.cell(2, 2))
- ############test##############
- doc.add_heading('2、指标名称与实际检测样品数量完整性审核', level=2)
- # 插入表格2
- paragraph_2 = doc.add_paragraph()
- paragraph_2.add_run(f'表2:{areaName}指标名称与实际检测样品数量统计表').bold = True
- table_2 = doc.add_table(rows=len(table_2_data) + 1, cols=3, style='Light Shading Accent 1')
- paragraph_2.alignment = WD_ALIGN_PARAGRAPH.CENTER
- table_2.alignment = WD_TABLE_ALIGNMENT.CENTER
- for i, row in enumerate(table_2.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_2_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_2_data.iloc[i - 1, j]))
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- doc.add_heading('二、数据规范性审核', level=1)
- long_text3 = """
- 按照《第三次全国土壤普查暂行土壤分类系统 (试行)》、《第三次全国土壤普查工作底图制作与采样点布设技术规范》、《第三次全国土壤普查土壤类型图编制技术规范》《第三次全国土壤普查土壤样品制备与检测技术规范》等规范统一标准和表现形式,土地利用方式按照第三次国土调查土地利用信息统一,种植制度按照农业区划信息进行统一。三普工作平台同时内置指标量纲、数字修约和允差等,控制数据规范性。
- 根据《第三次全国土壤普查土壤样品制备与检测技术规范(修订版)》,对检测指标对应的检测方法与相关标准或规范进行审核,得到检测方法审核、数值修约审核、数据填报项审核结果,未检出项填报以检出限的一半上报并加注*号。根据土壤普查教材中机械组成分类参考表,并进行宁国土壤质地填报审核结果表(见表6)。
- """
- para = doc.add_paragraph(long_text3)
- run1 = para.runs[0]
- run1.font.name = '宋体' # 设置字体为宋体
- run1.font.size = Pt(11) # 设置字号为11磅
- doc.add_heading('1、数据填报规范性审核', level=2)
- doc.add_heading('1、数据填报规范性审核', level=2)
- # 插入表3
- paragraph_3 = doc.add_paragraph()
- paragraph_3.add_run(f'表3:{areaName}土壤检测数据检测方法填报审核结果表').bold = True
- # table_3 = doc.add_table(rows=2, cols=2)
- paragraph_3.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_3.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 写入数据 这里数据写不下 嵌入链接
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:检测方法审核结果.xlsx', level=4)
- doc.add_heading('2、数值修约规范性审核', level=2)
- # 插入表4
- paragraph_4 = doc.add_paragraph()
- paragraph_4.add_run(f'表4:{areaName}土壤检测数据数值修约结果表').bold = True
- # table_4 = doc.add_table(rows=2, cols=2)
- paragraph_4.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_4.alignment = WD_TABLE_ALIGNMENT.CENTER
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数值修约审核.xlsx', level=4)
- # 填入数据 这里数据也放不下 嵌入链接
- doc.add_heading('3、数据未检出的填报规范性审核', level=2)
- # 插入表5
- paragraph_5 = doc.add_paragraph()
- paragraph_5.add_run(f'表5:{areaName}土壤检测数据未检出项填报审核结果表').bold = True
- # table_5 = doc.add_table(rows=2, cols=2)
- paragraph_5.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_5.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 写入数据 这里数据也放不下 嵌入链接
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据填报项审核结果.xlsx', level=4)
- doc.add_heading('4、土壤质地填报规范性审核', level=2)
- # 插入表6
- # paragraph_6 = doc.add_paragraph()
- # paragraph_6.add_run(f'表6:{areaName}土壤质地填报审核结果表').bold = True
- # table_6 = doc.add_table(rows=len(table_6_data) + 1, cols=4, style='Light Shading Accent 1')
- # paragraph_6.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_6.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 提取结果表中数据
- # 写入数据 土壤质地类型不一致的数据提取出来
- # for i, row in enumerate(table_6.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(table_6_data.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(table_6_data.iloc[i - 1, j]))
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- doc.add_heading('三、数据合理性审核', level=1)
- long_text4 = """
- 明确数据审核原则,结合外业调查采样,获取的成土环境、土壤利用现状等信息对结果进行综合评价。通过阈值分析、关联分析、逻辑分析等方法对数据准确性进行判断。
- 阈值法(国家和省级数据阈值):结合区域历史数据建立阈值,平台利用阈值法(国家和省级数据值)自动对检测数据进行初步审核,并对超出阈值范围数据做出警示标识,专家或专业技术人员结合区域实际、专家经验判定异常值并对异常数据进行驳回。超出阈值颜色标注。
- 红色: 超出国家阈值,维持试点期间的标红。
- 黄色: 省级阈值在国家阈值范围内的,数据超出省级阈值标黄。
- 紫色: 省级阈值超出国家阈值的,数据超出省级阈值标成紫色(注,此种情况超国家阈值的红色服从紫色,仅标紫色)
- 极值法: 常用的统计量是均值、标准差、最大值、最小值、分位数等,用来判断是否超出了合理的极值范围。根据相关指标历年数据与指标最大值、最小值、平均数、中位数和极差进行审查,结合相关外业情况。
- 关联分析方法: 存在量化关系的指标,通过设定组合筛选异常值,如碳氮比等关联分析。
- 指标综合分析:根据指标相互之间的关系,如有效磷在全磷中的占比、全钾与速效钾缓效钾之和的关系等。有机质、全磷、有效磷、全钾等数据异常; 全氮和有机质之间关系不合理; 速效钾含量远大于缓效钾,盐基总量远大于阳离子交换量; 部分样品阳离子交换量和交换性盐基总量测定结果偏低; 水溶盐离子总量与全盐量的相对偏差已远超过方法规定的允差范围; 容重检测偏差比较大;水稳性大团聚体土壤干燥结块,影响测定结果、重金属按照风险筛选值和风险管控值等问题。
- 数据质量溯源:异常数据溯源,可能是内业测试化验环节问题,也有可能是外业调查采样问题,要结合数据检测和异常复检情况具体判断。
- """
- para = doc.add_paragraph(long_text4)
- run1 = para.runs[0]
- run1.font.name = '宋体' # 设置字体为宋体
- run1.font.size = Pt(11) # 设置字号为11磅
- doc.add_heading('1、阈值法审核', level=2)
- # 插入表格
- paragraph_7 = doc.add_paragraph()
- paragraph_7.add_run(f'表7:{areaName}土壤检测数据超阈值样品统计表').bold = True
- # table_7 = doc.add_table(rows=2, cols=2)
- # paragraph_7.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_7.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 写入数据 点击查看数据 这里也不一定写的下 最好是嵌入链接
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:超阈值样品统计表.xlsx', level=4)
- # todo 合并所有数据 审核结果不为空的数据 写入表格保存到指定文件夹
- #doc.add_heading('2、极值法审核', level=2)
- #doc.add_heading('(1)pH', level=3)
- # 插入ph分布图
- # if os.path.isfile(f'{mkdir_path}/PH值分布图.png'):
- # doc.add_picture(f'{mkdir_path}/PH值分布图.png', width=Inches(6.0))
- # paragraph_t_1 = doc.add_paragraph()
- # paragraph_t_1.add_run(f'图1:pH值分布情况').bold = True
- # paragraph_t_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # 插入频度统计表
- # paragraph_8 = doc.add_paragraph()
- # paragraph_8.add_run('表8:pH数据统计表').bold = True
- # table_8 = doc.add_table(rows=6, cols=2, style='Light Shading Accent 1')
- # t_8 = table_8_data['频度分析']
- # t_8 = t_8.reset_index()
- # t_8.columns = ['指标', '数据']
- # paragraph_8.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_8.alignment = WD_TABLE_ALIGNMENT.CENTER
- # for i, row in enumerate(table_8.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(t_8.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(t_8.iloc[i - 1, j]))
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 插入异常数据提取表格 todo 这里数据多的话也可能写不下 最好是嵌入一下
- # if not table_8_data['异常数据'].empty:
- # paragraph_9 = doc.add_paragraph()
- # paragraph_9.add_run('表9:pH异常数据统计表').bold = True
- # table_9 = doc.add_table(rows=len(table_8_data['异常数据']) + 1, cols=6, style='Light Shading Accent 1')
- # t_9 = table_8_data['异常数据']
- #
- # paragraph_9.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_9.alignment = WD_TABLE_ALIGNMENT.CENTER
- # for i, row in enumerate(table_9.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(t_9.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(t_9.iloc[i - 1, j]))
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- doc.add_heading('四、审核存疑数据', level=1)
- paragraph_12 = doc.add_paragraph()
- paragraph_12.add_run(f'表10:数据审核过程存疑数据一览表').bold = True
- paragraph_12.alignment = WD_ALIGN_PARAGRAPH.CENTER
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:物理指标数据审核过程存疑数据一览表.xlsx', level=4)
- doc.add_heading('五、附表', level=1)
- doc.add_heading('附表1:某区三普样品数量统计表(表层)', level=2)
- # 插入附表1
- table_1_f = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
- table_1_f.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 遍历表格 插入数据
- # 遍历表格的所有单元格,并填充内容
- for i, row in enumerate(table_1_f.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_1_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 合并单元格 合并第3列的第二行和第三行
- if lenNum_1 > 1:
- table_1_f.cell(2, 2).text = ''
- table_1_f.cell(1, 2).merge(table_1_f.cell(2, 2))
- doc.add_heading('附表2:各指标频度分析表', level=2)
- # 插入表格 写入数据
- table_f_2_data = table_f_2_data.replace(np.nan, '')
- makeInfoTable(table_f_2_data, doc)
- # table_f_2 = doc.add_table(rows=len(table_f_2_data) + 1, cols=6, style='Light Shading Accent 1')
- # for i, row in enumerate(table_f_2.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(table_f_2_data.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(table_f_2_data.iloc[i - 1, j]))
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:频度分析表.xlsx', level=4)
- doc.add_heading('附表3:各指标频度分析图', level=2)
- # 插入频度信息的图形
- if os.path.isfile(f'{mkdir_path}/0.002mm以下颗粒含量分析图.png'):
- doc.add_picture(f'{mkdir_path}/0.002mm以下颗粒含量分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/0.02~0.002mm颗粒含量分析图.png.png'):
- doc.add_picture(f'{mkdir_path}/0.02~0.002mm颗粒含量分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/0.2~0.02mm颗粒含量分析图.png'):
- doc.add_picture(f'{mkdir_path}/0.2~0.02mm颗粒含量分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/2~0.2mm颗粒含量分析图.png'):
- doc.add_picture(f'{mkdir_path}/2~0.2mm颗粒含量分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/pH分析图.png'):
- doc.add_picture(f'{mkdir_path}/pH分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/风干试样含水量(分析基)分析图.png'):
- doc.add_picture(f'{mkdir_path}/风干试样含水量(分析基)分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/洗失量(吸管法需填)分析图.png'):
- doc.add_picture(f'{mkdir_path}/洗失量(吸管法需填)分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/土壤容重1分析图.png'):
- doc.add_picture(f'{mkdir_path}/土壤容重1分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/土壤容重2分析图.png'):
- doc.add_picture(f'{mkdir_path}/土壤容重2分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/土壤容重3分析图.png'):
- doc.add_picture(f'{mkdir_path}/土壤容重3分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/土壤容重4分析图.png'):
- doc.add_picture(f'{mkdir_path}/土壤容重4分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/土壤容重平均值分析图.png'):
- doc.add_picture(f'{mkdir_path}/土壤容重平均值分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/水稳0.5mm~1mm分析图.png'):
- doc.add_picture(f'{mkdir_path}/水稳0.5mm~1mm分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/水稳0.25mm~0.5mm分析图.png'):
- doc.add_picture(f'{mkdir_path}/水稳0.25mm~0.5mm分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/水稳1mm~2mm分析图.png'):
- doc.add_picture(f'{mkdir_path}/水稳1mm~2mm分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/水稳2mm~3mm分析图.png'):
- doc.add_picture(f'{mkdir_path}/水稳2mm~3mm分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/水稳3mm~5mm分析图.png'):
- doc.add_picture(f'{mkdir_path}/水稳3mm~5mm分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/水稳5mm分析图.png'):
- doc.add_picture(f'{mkdir_path}/水稳5mm分析图.png', width=Inches(6.0))
- doc.add_heading('附表4:数值修约标准', level=2)
- # 读取数据 插入表格 写入数据
- numData = pd.read_excel('./img/数值修约要求.xlsx', sheet_name='Sheet1')
- table_2_f = doc.add_table(rows=len(numData) + 1, cols=2, style='Light Shading Accent 1')
- table_2_f.alignment = WD_TABLE_ALIGNMENT.CENTER
- for i, row in enumerate(table_2_f.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(numData.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(numData.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 处理样式 遍历所有的段落 修改字体
- # 遍历并打印每个段落的文本
- paragraphs = doc.paragraphs
- for paragraph in paragraphs:
- for run in paragraph.runs:
- run.font.color.rgb = RGBColor(0, 0, 0)
- run.font.name = 'Times New Roman'
- run.font.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # 保存Word文档
- doc.save(f'{mkdir_path}/{areaName}-{type}审核报告.docx')
- # 生成常规养分指标审核报告
- def getConventionalNutrientIndicators(originData,data,type, changeFileUrl, saveFileUrl, check_1_data,
- check_3_data,
- check_5_data,
- check_8_data, # 样品编号替换为编号
- check_10_data,
- check_12_data,
- check_14_data ):
- # 生成报告
- name = os.path.basename(changeFileUrl)
- n = name.split('.')
- areaName = n[0].replace('数据', '')
- # 生成一个新的文件夹用于存放审核报告相关的数据
- nowTime = time.strftime("%Y-%m-%d %H时%M分%S秒", time.localtime())
- dir_name = f'{areaName}-{type}数据审核报告'
- mkdir_path = saveFileUrl + '/' + dir_name + nowTime
- if not os.path.exists(mkdir_path):
- os.mkdir(mkdir_path)
- # 根据选择的路径读取数据
- ConventionalNutrientData = data[indexClassificationList[type]]
- ConventionalNutrientDataNum = originData[indexClassificationList[type]]
- report.getFrequencyImage(ConventionalNutrientData, mkdir_path)
- ConventionalNutrientData['序号'] = data['序号']
- ConventionalNutrientData['原样品编号'] = data['原样品编号']
- ConventionalNutrientData['样品编号'] = data['样品编号']
- ConventionalNutrientData['地理位置'] = data['地理位置']
- ConventionalNutrientData['母质'] = data['母质']
- ConventionalNutrientData['土壤类型'] = data['土壤类型']
- ConventionalNutrientData['土地利用类型'] = data['土地利用类型']
- # ConventionalNutrientData['土壤质地'] = data['土壤质地']
- ConventionalNutrientDataNum['序号'] = originData['序号']
- ConventionalNutrientDataNum['原样品编号'] = originData['原样品编号']
- ConventionalNutrientDataNum['样品编号'] = originData['样品编号']
- ConventionalNutrientDataNum['地理位置'] = originData['地理位置']
- ConventionalNutrientDataNum['母质'] = originData['母质']
- ConventionalNutrientDataNum['土壤类型'] = originData['土壤类型']
- ConventionalNutrientDataNum['土地利用类型'] = originData['土地利用类型']
- # 生成相应审核报告
- ConventionalNutrientData['原样品编号'] = ConventionalNutrientData['原样品编号'].astype(str)
- ConventionalNutrientDataNum['原样品编号'] = ConventionalNutrientDataNum['原样品编号'].astype(str)
- data['原样品编号'] = data['原样品编号'].astype(str)
- # checkData = pd.read_excel(changeFileUrl, sheet_name='检测方法')
- # 上面这个地址,可以纯递给函数中,用于保存表格和图片
- # 调用函数 开始生成报告相关内容
- # 表1相关数据
- typeData = report.getSimpleNum(ConventionalNutrientData)
- lenNum_1 = len(typeData['sData'])
- lenNum_1_f = len(typeData['allData'])
- table_1_data = pd.DataFrame({
- '类型': typeData['sData'].index,
- '数量': typeData['sData'],
- '合计': [typeData['sData'].sum() for _ in range(lenNum_1)]
- })
- # 表2数据
- table_2_data = report.getDataComplete(ConventionalNutrientData)
- table_2_data = table_2_data.reset_index()
- table_2_data.columns = ['指标名称', '实测数量', '应测数量']
- # 表3数据
- # table_3_data = report.checkMethod(checkData, mkdir_path)
- # 数据修约 表4
- report.getNum(ConventionalNutrientDataNum, mkdir_path)
- # 数据填报项审核 表5
- report.dataReportResult(ConventionalNutrientData, mkdir_path)
- # 表6数据 土壤质地类型不一致
- middData = data[['原样品编号', '样品编号']].astype(str)
- middData['编号'] = middData['原样品编号']
- del middData['原样品编号']
- check_1_data = pd.merge(check_1_data, middData, how='left', on='编号')
- check_1_data = check_1_data.replace(np.nan, '')
- # typeNotSame = check_1_data[check_1_data['土壤质地'] != check_1_data['土壤类型(判断)']]
- # table_6_data = typeNotSame[['编号', '样品编号', '土壤质地', '土壤类型(判断)']]
- allNeedData = pd.DataFrame({})
- allNeedData['原样品编号'] = check_1_data['编号']
- getSimpleDataNumber = pd.merge(allNeedData, ConventionalNutrientData[['原样品编号', '样品编号']], how='left', on="原样品编号")
- allNeedData['样品编号'] = getSimpleDataNumber['样品编号']
- allNeedData['土地利用类型'] = check_1_data['土地利用类型']
- allNeedData['审核结果'] = check_10_data['审核结果'] + check_12_data['审核结果']
- allNeedData['外业'] = ['' for _ in range(len(check_1_data))]
- table_7_data = allNeedData[allNeedData['审核结果'] != '']
- del table_7_data['审核结果']
- # 写进表格
- with pd.ExcelWriter(f'{mkdir_path}/超阈值样品统计表.xlsx', engine='openpyxl') as writer:
- table_7_data.to_excel(writer, index=False, sheet_name='超阈值数据')
- # 表8数据
- #table_8_data = report.getPHData(ConventionalNutrientData, mkdir_path)
- # 表10 数据
- table_10_data = report.getNAndC(ConventionalNutrientData, mkdir_path)
- # 表11 数据:全磷和有效磷异常数据统计
- table_11_data = report.getPData(ConventionalNutrientData, mkdir_path)
- report.getKData(ConventionalNutrientData, mkdir_path)
- # 表13 所有存疑数据
- with pd.ExcelWriter(f'{mkdir_path}/数据审核过程存疑数据一览表.xlsx', engine='openpyxl') as writer:
- allNeedData[allNeedData['审核结果'] != ''].to_excel(writer, index=False, sheet_name='存疑数据')
- # 附表: 频度分析图
- # report.getFrequencyImage(ConventionalNutrientData, mkdir_path)
- table_f_2_data = report.getFrequencyInformation(data, mkdir_path)
- # 新建一个文档
- doc = Document()
- # 添加标题
- doc.add_heading(f"{areaName}第三次全国土壤普查常规养分指标检测数据审核报告", level=0)
- # 在文档中添加封面段落
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- # 插入图片,设置宽度为6英寸(可根据需求调整)
- run = fm.add_run()
- run.add_picture( 'img/第三次全国土壤普查img.png', width=Inches(2.26))
- fm.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 在文档中添加封面段落
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- # 获取当前日期
- current_date = datetime.now()
- # 将年份和月份转换为中文大写数字
- year = int(current_date.strftime("%Y")) # 转换为整数
- month = int(current_date.strftime("%m")) # 转换为整数
- # 使用 cn2an 将数字转换为中文大写
- year_chinese =number_to_chinese_year(year) # 年份转换
- month_chinese = cn2an.an2cn(month) # 月份转换
- current_date_formatted = f"{year_chinese}年{month_chinese}月"
- # 组合动态文本
- dynamic_text = f"安徽农业大学资源与环境学院\n{current_date_formatted}"
- # 添加文字并居中
- text_paragraph = doc.add_paragraph()
- text_run = text_paragraph.add_run(dynamic_text)
- text_run.font.name = "宋体"
- text_run.font.size = Pt(18)
- text_run.bold = True # 设置字体加粗
- text_paragraph.alignment = 1 # 1 表示居中对齐
- # 正确插入分页符
- doc.add_page_break()
- heading = doc.add_heading('总体概述', level=1)
- heading.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # 第一段
- long_text1 = f"""
- {areaName}第三次全国土壤普查县级数据审核报告主要通过收集和整理相关数据,并对其进行内业检测数据的完整性、规范性和合理性进行审核,形成存疑样点清单及存疑样点结果判定,最终编制完成数据审核报告,同时提交( )对相关指标进行整改复测。报告整理了( )个表层样品数据(含平行样、质控样)、( )个水稳性大团聚体样品数据(含平行样)、( )个剖面样品数据(含平行样、质控样),共( )次样品检测结果分析情况。相关结果分别按照物理性指标检测数据、一般化学指标检测数据、常规养分指标检测数据和重金属指标检测数据形成四份报告。本报告为表层样常规养分指标检测数据审核报告。
- """
- para0 = doc.add_paragraph(long_text1)
- run0 = para0.runs[0] # 获取段落中的第一个run对象
- run0.font.name = '宋体' # 设置字体为宋体
- run0.font.size = Pt(11) # 设置字号为11磅
- # 设置段落的行间距为1.5倍
- para_format = para0.paragraph_format
- para_format.line_spacing = 1.5 # 设置行间距为1.5倍
- # 添加一级标题
- doc.add_heading('一、数据完整性审核', level=1)
- # 第二段
- long_text2 = """
- 外业信息调查采样环节:采用电子围栏和外业调查采样APP,对采样位置和填报信息进行管理,确保外业调查信息填报完整。
- 样品检测数据上报环节:通过土壤普查工作平台对上报数据的完整性进行筛查。( )第三次土壤普查相关指标检测数据由( )提供,数据均已通过省级质控实验室和县级土壤普查办审核;相关土壤指标历史数据则由( )第三次土壤普查办公室提供。根据《第三次全国土壤普查土壤样品制备与检测技术规范(修订版)》要求,统计各土地利用类型的样品数量,并按照耕地园地土壤样品(表层/剖面)、林地草地土壤样品(表层/剖面)以及水稳定性大团聚体样品(见表1)进行分类,编制了指标名称与实际检测样品数量统计表(见表2),其中水溶性盐分总量大于1g/kg,增加检测了八大离子(该指标在化学指标检测数据审核报告内)。
- """
- para = doc.add_paragraph(long_text2)
- run1 = para.runs[0]
- run1.font.name = '宋体' # 设置字体为宋体
- run1.font.size = Pt(11) # 设置字号为11磅
- # 设置段落的行间距为1.5倍
- para_format = para.paragraph_format
- para_format.line_spacing = 1.5 # 设置行间距为1.5倍
- doc.add_heading('1、土地利用类型与检测指标符合性审核', level=2)
- # 插入表格1
- paragraph_1 = doc.add_paragraph()
- paragraph_1.add_run(f"表1:{areaName}三普样品数量统计表(表层)").bold = True
- # 设置居中
- paragraph_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
- table_1 = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
- table_1.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 遍历表格 插入数据
- # 遍历表格的所有单元格,并填充内容
- for i, row in enumerate(table_1.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_1_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 合并单元格 合并第3列的第二行和第三行
- if lenNum_1 > 1:
- table_1.cell(2, 2).text = ''
- table_1.cell(1, 2).merge(table_1.cell(2, 2))
- ############test##############
- doc.add_heading('2、指标名称与实际检测样品数量完整性审核', level=2)
- # 插入表格2
- paragraph_2 = doc.add_paragraph()
- paragraph_2.add_run(f'表2:{areaName}指标名称与实际检测样品数量统计表').bold = True
- table_2 = doc.add_table(rows=len(table_2_data) + 1, cols=3, style='Light Shading Accent 1')
- paragraph_2.alignment = WD_ALIGN_PARAGRAPH.CENTER
- table_2.alignment = WD_TABLE_ALIGNMENT.CENTER
- for i, row in enumerate(table_2.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_2_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_2_data.iloc[i - 1, j]))
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- doc.add_heading('二、数据规范性审核', level=1)
- long_text3 = """
- 按照《第三次全国土壤普查暂行土壤分类系统 (试行)》、《第三次全国土壤普查工作底图制作与采样点布设技术规范》、《第三次全国土壤普查土壤类型图编制技术规范》《第三次全国土壤普查土壤样品制备与检测技术规范》等规范统一标准和表现形式,土地利用方式按照第三次国土调查土地利用信息统一,种植制度按照农业区划信息进行统一。三普工作平台同时内置指标量纲、数字修约和允差等,控制数据规范性。
- 根据《第三次全国土壤普查土壤样品制备与检测技术规范(修订版)》,对检测指标对应的检测方法与相关标准或规范进行审核,得到检测方法审核、数值修约审核、数据填报项审核结果,未检出项填报以检出限的一半上报并加注*号。根据土壤普查教材中机械组成分类参考表,并进行宁国土壤质地填报审核结果表(见表6)。
- """
- para = doc.add_paragraph(long_text3)
- run1 = para.runs[0]
- run1.font.name = '宋体' # 设置字体为宋体
- run1.font.size = Pt(11) # 设置字号为11磅
- doc.add_heading('1、数据填报规范性审核', level=2)
- doc.add_heading('1、数据填报规范性审核', level=2)
- # 插入表3
- paragraph_3 = doc.add_paragraph()
- paragraph_3.add_run(f'表3:{areaName}土壤检测数据检测方法填报审核结果表').bold = True
- # table_3 = doc.add_table(rows=2, cols=2)
- paragraph_3.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_3.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 写入数据 这里数据写不下 嵌入链接
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:检测方法审核结果.xlsx', level=4)
- doc.add_heading('2、数值修约规范性审核', level=2)
- # 插入表4
- paragraph_4 = doc.add_paragraph()
- paragraph_4.add_run(f'表4:{areaName}土壤检测数据数值修约结果表').bold = True
- # table_4 = doc.add_table(rows=2, cols=2)
- paragraph_4.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_4.alignment = WD_TABLE_ALIGNMENT.CENTER
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数值修约审核.xlsx', level=4)
- # 填入数据 这里数据也放不下 嵌入链接
- doc.add_heading('3、数据未检出的填报规范性审核', level=2)
- # 插入表5
- paragraph_5 = doc.add_paragraph()
- paragraph_5.add_run(f'表5:{areaName}土壤检测数据未检出项填报审核结果表').bold = True
- # table_5 = doc.add_table(rows=2, cols=2)
- paragraph_5.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_5.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 写入数据 这里数据也放不下 嵌入链接
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据填报项审核结果.xlsx', level=4)
- # doc.add_heading('4、土壤质地填报规范性审核', level=2)
- # # 插入表6
- # paragraph_6 = doc.add_paragraph()
- # paragraph_6.add_run(f'表6:{areaName}土壤质地填报审核结果表').bold = True
- # table_6 = doc.add_table(rows=len(table_6_data) + 1, cols=4, style='Light Shading Accent 1')
- # paragraph_6.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_6.alignment = WD_TABLE_ALIGNMENT.CENTER
- # # 提取结果表中数据
- # # 写入数据 土壤质地类型不一致的数据提取出来
- # for i, row in enumerate(table_6.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(table_6_data.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(table_6_data.iloc[i - 1, j]))
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- doc.add_heading('三、数据合理性审核', level=1)
- long_text4 = """
- 明确数据审核原则,结合外业调查采样,获取的成土环境、土壤利用现状等信息对结果进行综合评价。通过阈值分析、关联分析、逻辑分析等方法对数据准确性进行判断。
- 阈值法(国家和省级数据阈值):结合区域历史数据建立阈值,平台利用阈值法(国家和省级数据值)自动对检测数据进行初步审核,并对超出阈值范围数据做出警示标识,专家或专业技术人员结合区域实际、专家经验判定异常值并对异常数据进行驳回。超出阈值颜色标注。
- 红色: 超出国家阈值,维持试点期间的标红。
- 黄色: 省级阈值在国家阈值范围内的,数据超出省级阈值标黄。
- 紫色: 省级阈值超出国家阈值的,数据超出省级阈值标成紫色(注,此种情况超国家阈值的红色服从紫色,仅标紫色)
- 极值法: 常用的统计量是均值、标准差、最大值、最小值、分位数等,用来判断是否超出了合理的极值范围。根据相关指标历年数据与指标最大值、最小值、平均数、中位数和极差进行审查,结合相关外业情况。
- 关联分析方法: 存在量化关系的指标,通过设定组合筛选异常值,如碳氮比等关联分析。
- 指标综合分析:根据指标相互之间的关系,如有效磷在全磷中的占比、全钾与速效钾缓效钾之和的关系等。有机质、全磷、有效磷、全钾等数据异常; 全氮和有机质之间关系不合理; 速效钾含量远大于缓效钾,盐基总量远大于阳离子交换量; 部分样品阳离子交换量和交换性盐基总量测定结果偏低; 水溶盐离子总量与全盐量的相对偏差已远超过方法规定的允差范围; 容重检测偏差比较大;水稳性大团聚体土壤干燥结块,影响测定结果、重金属按照风险筛选值和风险管控值等问题。
- 数据质量溯源:异常数据溯源,可能是内业测试化验环节问题,也有可能是外业调查采样问题,要结合数据检测和异常复检情况具体判断。
- """
- para = doc.add_paragraph(long_text4)
- run1 = para.runs[0]
- run1.font.name = '宋体' # 设置字体为宋体
- run1.font.size = Pt(11) # 设置字号为11磅
- doc.add_heading('1、阈值法审核', level=2)
- # 插入表格
- paragraph_7 = doc.add_paragraph()
- paragraph_7.add_run(f'表6:{areaName}土壤检测数据超阈值样品统计表').bold = True
- # table_7 = doc.add_table(rows=2, cols=2)
- # paragraph_7.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_7.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 写入数据 点击查看数据 这里也不一定写的下 最好是嵌入链接
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据审核过程存疑数据一览表.xlsx', level=4)
- # todo 合并所有数据 审核结果不为空的数据 写入表格保存到指定文件夹
- #doc.add_heading('2、极值法审核', level=2)
- #doc.add_heading('(1)pH', level=3)
- # 插入ph分布图
- # if os.path.isfile(f'{mkdir_path}/PH值分布图.png'):
- # doc.add_picture(f'{mkdir_path}/PH值分布图.png', width=Inches(6.0))
- # paragraph_t_1 = doc.add_paragraph()
- # paragraph_t_1.add_run(f'图1:pH值分布情况').bold = True
- # paragraph_t_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
- #
- # # 插入频度统计表
- # paragraph_8 = doc.add_paragraph()
- # paragraph_8.add_run('表7:pH数据统计表').bold = True
- # table_8 = doc.add_table(rows=6, cols=2, style='Light Shading Accent 1')
- # t_8 = table_8_data['频度分析']
- #
- # t_8 = t_8.reset_index()
- # t_8.columns = ['指标', '数据']
- # paragraph_8.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_8.alignment = WD_TABLE_ALIGNMENT.CENTER
- # for i, row in enumerate(table_8.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(t_8.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(t_8.iloc[i - 1, j]))
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- #
- # # 插入异常数据提取表格 todo 这里数据多的话也可能写不下 最好是嵌入一下
- # if not table_8_data['异常数据'].empty:
- # paragraph_9 = doc.add_paragraph()
- # paragraph_9.add_run('表8:pH异常数据统计表').bold = True
- # table_9 = doc.add_table(rows=len(table_8_data['异常数据']) + 1, cols=6, style='Light Shading Accent 1')
- # t_9 = table_8_data['异常数据']
- #
- # paragraph_9.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_9.alignment = WD_TABLE_ALIGNMENT.CENTER
- # for i, row in enumerate(table_9.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(t_9.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(t_9.iloc[i - 1, j]))
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- doc.add_heading('3、关联分析法审核', level=2)
- if os.path.isfile(f'{mkdir_path}/有机质与全氮相关性散点图.png'):
- doc.add_picture(f'{mkdir_path}/有机质与全氮相关性散点图.png', width=Inches(6.0))
- paragraph_t_2 = doc.add_paragraph()
- paragraph_t_2.add_run(f'图2:有机质与全氮相关关系').bold = True
- paragraph_t_2.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # 插入有机质全氮比异常数据
- if not table_10_data.empty:
- paragraph_10 = doc.add_paragraph()
- paragraph_10.add_run('表9:有机质全氮比异常数据统计表').bold = True
- table_10 = doc.add_table(rows=len(table_10_data) + 1, cols=8, style='Light Shading Accent 1')
- paragraph_10.alignment = WD_ALIGN_PARAGRAPH.CENTER
- table_10.alignment = WD_TABLE_ALIGNMENT.CENTER
- for i, row in enumerate(table_10.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_10_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_10_data.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- doc.add_heading('4、指标综合分析', level=2)
- # 插入图片
- if os.path.isfile(f'{mkdir_path}/全磷分布图.png'):
- doc.add_picture(f'{mkdir_path}/全磷分布图.png', width=Inches(6.0))
- paragraph_t_3 = doc.add_paragraph()
- paragraph_t_3.add_run(f'图3:全磷分布图').bold = True
- paragraph_t_3.alignment = WD_ALIGN_PARAGRAPH.CENTER
- if os.path.isfile(f'{mkdir_path}/有效磷分布图.png'):
- doc.add_picture(f'{mkdir_path}/有效磷分布图.png', width=Inches(6.0))
- paragraph_t_4 = doc.add_paragraph()
- paragraph_t_4.add_run(f'图4:有效磷分布图').bold = True
- paragraph_t_4.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # 插入图片
- if os.path.isfile(f'{mkdir_path}/有效磷占全磷比分布图.png'):
- doc.add_picture(f'{mkdir_path}/有效磷占全磷比分布图.png', width=Inches(6.0))
- paragraph_t_5 = doc.add_paragraph()
- paragraph_t_5.add_run(f'图5:有效磷含量占全磷含量比例').bold = True
- paragraph_t_5.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # 插入表格
- if not table_11_data.empty:
- paragraph_11 = doc.add_paragraph()
- paragraph_11.add_run('表10:全磷与有效磷异常样品统计表').bold = True
- table_11 = doc.add_table(rows=len(table_11_data) + 1, cols=7, style='Light Shading Accent 1')
- paragraph_11.alignment = WD_ALIGN_PARAGRAPH.CENTER
- table_11.alignment = WD_TABLE_ALIGNMENT.CENTER
- for i, row in enumerate(table_11.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_11_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_11_data.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- else:
- paragraph_11 = doc.add_paragraph()
- paragraph_11.add_run('表10:全磷与有效磷异常样品统计表').bold = True
- paragraph_11_info = doc.add_paragraph()
- paragraph_11_info.add_run('无异常数据')
- paragraph_11.alignment = WD_ALIGN_PARAGRAPH.CENTER
- paragraph_11_info.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # 全钾、速效钾、缓效钾
- if os.path.isfile(f'{mkdir_path}/全钾与速效钾缓效钾之和关系统计图.png'):
- doc.add_picture(f'{mkdir_path}/全钾与速效钾缓效钾之和关系统计图.png', width=Inches(6.0))
- paragraph_t_6 = doc.add_paragraph()
- paragraph_t_6.add_run(f'图6:全钾与速效钾缓效钾之和关系统计图').bold = True
- paragraph_t_6.alignment = WD_ALIGN_PARAGRAPH.CENTER
- if os.path.isfile(f'{mkdir_path}/速效钾与缓效钾散点图.png'):
- doc.add_picture(f'{mkdir_path}/速效钾与缓效钾散点图.png', width=Inches(6.0))
- paragraph_t_7 = doc.add_paragraph()
- paragraph_t_7.add_run(f'图7:速效钾与缓效钾散点图').bold = True
- paragraph_t_7.alignment = WD_ALIGN_PARAGRAPH.CENTER
- doc.add_heading('四、审核存疑数据', level=1)
- paragraph_12 = doc.add_paragraph()
- paragraph_12.add_run(f'表11:数据审核过程存疑数据一览表').bold = True
- paragraph_12.alignment = WD_ALIGN_PARAGRAPH.CENTER
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据审核过程存疑数据一览表.xlsx', level=4)
- doc.add_heading('五、附表', level=1)
- doc.add_heading('附表1:某区三普样品数量统计表(表层)', level=2)
- # 插入附表1
- table_1_f = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
- table_1_f.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 遍历表格 插入数据
- # 遍历表格的所有单元格,并填充内容
- for i, row in enumerate(table_1_f.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_1_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 合并单元格 合并第3列的第二行和第三行
- if lenNum_1 > 1:
- table_1_f.cell(2, 2).text = ''
- table_1_f.cell(1, 2).merge(table_1_f.cell(2, 2))
- doc.add_heading('附表2:各指标频度分析表', level=2)
- # 插入表格 写入数据
- table_f_2_data = table_f_2_data.replace(np.nan, '')
- makeInfoTable(table_f_2_data, doc)
- # table_f_2 = doc.add_table(rows=len(table_f_2_data) + 1, cols=6, style='Light Shading Accent 1')
- # for i, row in enumerate(table_f_2.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(table_f_2_data.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(table_f_2_data.iloc[i - 1, j]))
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:频度分析表.xlsx', level=4)
- doc.add_heading('附表3:各指标频度分析图', level=2)
- # 插入频度信息的图形
- if os.path.isfile(f'{mkdir_path}/pH分析图.png'):
- doc.add_picture(f'{mkdir_path}/pH分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/缓效钾分析图.png'):
- doc.add_picture(f'{mkdir_path}/缓效钾分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/全氮分析图.png'):
- doc.add_picture(f'{mkdir_path}/全氮分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/全钾分析图.png'):
- doc.add_picture(f'{mkdir_path}/全钾分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/全磷分析图.png'):
- doc.add_picture(f'{mkdir_path}/全磷分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/速效钾分析图.png'):
- doc.add_picture(f'{mkdir_path}/速效钾分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/有机质分析图.png'):
- doc.add_picture(f'{mkdir_path}/有机质分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/有效硅分析图.png'):
- doc.add_picture(f'{mkdir_path}/有效硅分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/有效磷分析图.png'):
- doc.add_picture(f'{mkdir_path}/有效磷分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/有效硫分析图.png'):
- doc.add_picture(f'{mkdir_path}/有效硫分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/有效锰分析图.png'):
- doc.add_picture(f'{mkdir_path}/有效锰分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/有效钼分析图.png'):
- doc.add_picture(f'{mkdir_path}/有效钼分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/有效硼分析图.png'):
- doc.add_picture(f'{mkdir_path}/有效硼分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/有效铁分析图.png'):
- doc.add_picture(f'{mkdir_path}/有效铁分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/有效铜分析图.png'):
- doc.add_picture(f'{mkdir_path}/有效铜分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/有效锌分析图.png'):
- doc.add_picture(f'{mkdir_path}/有效锌分析图.png', width=Inches(6.0))
- doc.add_heading('附表4:数值修约标准', level=2)
- # 读取数据 插入表格 写入数据
- numData = pd.read_excel('./img/数值修约要求.xlsx', sheet_name='Sheet1')
- table_2_f = doc.add_table(rows=len(numData) + 1, cols=2, style='Light Shading Accent 1')
- table_2_f.alignment = WD_TABLE_ALIGNMENT.CENTER
- for i, row in enumerate(table_2_f.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(numData.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(numData.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 处理样式 遍历所有的段落 修改字体
- # 遍历并打印每个段落的文本
- paragraphs = doc.paragraphs
- for paragraph in paragraphs:
- for run in paragraph.runs:
- run.font.color.rgb = RGBColor(0, 0, 0)
- run.font.name = 'Times New Roman'
- run.font.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # 保存Word文档
- doc.save(f'{mkdir_path}/{areaName}-{type}审核报告.docx')
- # 生成一般化学性指标审核报告
- def getChemicalIndicators(originData,data,type, changeFileUrl, saveFileUrl, check_1_data,
- check_3_data,
- check_5_data,
- check_8_data, # 样品编号替换为编号
- check_10_data,
- check_12_data,
- check_14_data):
- # 生成报告
- name = os.path.basename(changeFileUrl)
- n = name.split('.')
- areaName = n[0].replace('数据', '')
- # 生成一个新的文件夹用于存放审核报告相关的数据
- nowTime = time.strftime("%Y-%m-%d %H时%M分%S秒", time.localtime())
- dir_name = f'{areaName}-{type}数据审核报告'
- mkdir_path = saveFileUrl + '/' + dir_name + nowTime
- if not os.path.exists(mkdir_path):
- os.mkdir(mkdir_path)
- # 根据选择的路径读取数据
- cheemicalData = data[indexClassificationList[type]]
- cheemicalDataNum = originData[indexClassificationList[type]]
- report.getFrequencyImage(cheemicalData, mkdir_path)
- cheemicalData['序号'] = data['序号']
- cheemicalData['原样品编号'] = data['原样品编号']
- cheemicalData['样品编号'] = data['样品编号']
- cheemicalData['地理位置'] = data['地理位置']
- cheemicalData['母质'] = data['母质']
- cheemicalData['土壤类型'] = data['土壤类型']
- cheemicalData['土地利用类型'] = data['土地利用类型']
- # cheemicalData['土壤质地'] = data['土壤质地']
- cheemicalData['原样品编号'] = cheemicalData['原样品编号'].astype(str)
- # checkData = pd.read_excel(changeFileUrl, sheet_name='检测方法')
- cheemicalDataNum['序号'] = originData['序号']
- cheemicalDataNum['原样品编号'] = originData['原样品编号']
- cheemicalDataNum['样品编号'] = originData['样品编号']
- cheemicalDataNum['地理位置'] = originData['地理位置']
- cheemicalDataNum['母质'] = originData['母质']
- cheemicalDataNum['土壤类型'] = originData['土壤类型']
- cheemicalDataNum['土地利用类型'] = originData['土地利用类型']
- # cheemicalData['土壤质地'] = data['土壤质地']
- cheemicalDataNum['原样品编号'] = cheemicalDataNum['原样品编号'].astype(str)
- # 上面这个地址,可以纯递给函数中,用于保存表格和图片
- # 调用函数 开始生成报告相关内容
- # 表1相关数据
- typeData = report.getSimpleNum(cheemicalData)
- lenNum_1 = len(typeData['sData'])
- lenNum_1_f = len(typeData['allData'])
- table_1_data = pd.DataFrame({
- '类型': typeData['sData'].index,
- '数量': typeData['sData'],
- '合计': [typeData['sData'].sum() for _ in range(lenNum_1)]
- })
- # 表2数据
- table_2_data = report.getDataComplete(cheemicalData)
- table_2_data = table_2_data.reset_index()
- table_2_data.columns = ['指标名称', '实测数量', '应测数量']
- # 表3数据
- # table_3_data = report.checkMethod(checkData, mkdir_path)
- # 数据修约 表4
- report.getNum(cheemicalDataNum, mkdir_path)
- # 数据填报项审核 表5
- report.dataReportResult(cheemicalData, mkdir_path)
- # 表6数据 土壤质地类型不一致
- middData = data[['原样品编号', '样品编号']].astype(str)
- middData['编号'] = middData['原样品编号']
- del middData['原样品编号']
- check_1_data = pd.merge(check_1_data, middData, how='left', on='编号')
- check_1_data = check_1_data.replace(np.nan, '')
- # typeNotSame = check_1_data[check_1_data['土壤质地'] != check_1_data['土壤类型(判断)']]
- # table_6_data = typeNotSame[['编号', '样品编号', '土壤质地', '土壤类型(判断)']]
- allNeedData = pd.DataFrame({})
- allNeedData['原样品编号'] = check_1_data['编号']
- getSimpleDataNumber = pd.merge(allNeedData, data[['原样品编号', '样品编号']], how='left', on="原样品编号")
- allNeedData['样品编号'] = getSimpleDataNumber['样品编号']
- allNeedData['土地利用类型'] = check_1_data['土地利用类型']
- allNeedData['审核结果'] = check_5_data['审核结果'] + check_8_data['审核结果']
- allNeedData['外业'] = ['' for _ in range(len(check_1_data))]
- table_7_data = allNeedData[allNeedData['审核结果'] != '']
- del table_7_data['审核结果']
- # 写进表格
- with pd.ExcelWriter(f'{mkdir_path}/超阈值样品统计表.xlsx', engine='openpyxl') as writer:
- table_7_data.to_excel(writer, index=False, sheet_name='超阈值数据')
- # 表8数据
- table_8_data = report.getPHData(cheemicalData, mkdir_path)
- report.cationExchangeCapacity(cheemicalData, mkdir_path)
- report.changeCation(cheemicalData, mkdir_path)
- report.manyTypes(cheemicalData, mkdir_path)
- # 表13 所有存疑数据
- with pd.ExcelWriter(f'{mkdir_path}/数据审核过程存疑数据一览表.xlsx', engine='openpyxl') as writer:
- allNeedData[allNeedData['审核结果'] != ''].to_excel(writer, index=False, sheet_name='存疑数据')
- # 附表: 频度分析图
- # report.getFrequencyImage(cheemicalData, mkdir_path)
- table_f_2_data = report.getFrequencyInformation(data, mkdir_path)
- # 新建一个文档
- doc = Document()
- # 添加标题
- doc.add_heading(f"{areaName}第三次全国土壤普查一般化学性指标检测数据审核报告", level=0)
- # 在文档中添加封面段落
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- # 插入图片,设置宽度为6英寸(可根据需求调整)
- run = fm.add_run()
- run.add_picture('img/第三次全国土壤普查img.png', width=Inches(2.26))
- fm.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 在文档中添加封面段落
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- # 获取当前日期
- current_date = datetime.now()
- # 将年份和月份转换为中文大写数字
- year = int(current_date.strftime("%Y")) # 转换为整数
- month = int(current_date.strftime("%m")) # 转换为整数
- # 使用 cn2an 将数字转换为中文大写
- year_chinese = number_to_chinese_year(year) # 年份转换
- month_chinese = cn2an.an2cn(month) # 月份转换
- current_date_formatted = f"{year_chinese}年{month_chinese}月"
- # 组合动态文本
- dynamic_text = f"安徽农业大学资源与环境学院\n{current_date_formatted}"
- # 添加文字并居中
- text_paragraph = doc.add_paragraph()
- text_run = text_paragraph.add_run(dynamic_text)
- text_run.font.name = "宋体"
- text_run.font.size = Pt(18)
- text_run.bold = True # 设置字体加粗
- text_paragraph.alignment = 1 # 1 表示居中对齐
- # 正确插入分页符
- doc.add_page_break()
- heading = doc.add_heading('总体概述', level=1)
- heading.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # 第一段
- long_text1 = f"""
- {areaName}第三次全国土壤普查县级数据审核报告主要通过收集和整理相关数据,并对其进行内业检测数据的完整性、规范性和合理性进行审核,形成存疑样点清单及存疑样点结果判定,最终编制完成数据审核报告,同时提交( )对相关指标进行整改复测。报告整理了( )个表层样品数据(含平行样、质控样)、( )个水稳性大团聚体样品数据(含平行样)、( )个剖面样品数据(含平行样、质控样),共( )次样品检测结果分析情况。相关结果分别按照物理性指标检测数据、一般化学指标检测数据、常规养分指标检测数据和重金属指标检测数据形成四份报告。本报告为表层样常规养分指标检测数据审核报告。
- """
- para0 = doc.add_paragraph(long_text1)
- run0 = para0.runs[0] # 获取段落中的第一个run对象
- run0.font.name = '宋体' # 设置字体为宋体
- run0.font.size = Pt(11) # 设置字号为11磅
- # 设置段落的行间距为1.5倍
- para_format = para0.paragraph_format
- para_format.line_spacing = 1.5 # 设置行间距为1.5倍
- # 添加一级标题
- doc.add_heading('一、数据完整性审核', level=1)
- # 第二段
- long_text2 = """
- 外业信息调查采样环节:采用电子围栏和外业调查采样APP,对采样位置和填报信息进行管理,确保外业调查信息填报完整。
- 样品检测数据上报环节:通过土壤普查工作平台对上报数据的完整性进行筛查。( )第三次土壤普查相关指标检测数据由( )提供,数据均已通过省级质控实验室和县级土壤普查办审核;相关土壤指标历史数据则由( )第三次土壤普查办公室提供。根据《第三次全国土壤普查土壤样品制备与检测技术规范(修订版)》要求,统计各土地利用类型的样品数量,并按照耕地园地土壤样品(表层/剖面)、林地草地土壤样品(表层/剖面)以及水稳定性大团聚体样品(见表1)进行分类,编制了指标名称与实际检测样品数量统计表(见表2),其中水溶性盐分总量大于1g/kg,增加检测了八大离子(该指标在化学指标检测数据审核报告内)。
- """
- para = doc.add_paragraph(long_text2)
- run1 = para.runs[0]
- run1.font.name = '宋体' # 设置字体为宋体
- run1.font.size = Pt(11) # 设置字号为11磅
- # 设置段落的行间距为1.5倍
- para_format = para.paragraph_format
- para_format.line_spacing = 1.5 # 设置行间距为1.5倍
- doc.add_heading('1、土地利用类型与检测指标符合性审核', level=2)
- # 插入表格1
- paragraph_1 = doc.add_paragraph()
- paragraph_1.add_run(f"表1:{areaName}三普样品数量统计表(表层)").bold = True
- # 设置居中
- paragraph_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
- table_1 = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
- table_1.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 遍历表格 插入数据
- # 遍历表格的所有单元格,并填充内容
- for i, row in enumerate(table_1.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_1_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 合并单元格 合并第3列的第二行和第三行
- if lenNum_1 > 1:
- table_1.cell(2, 2).text = ''
- table_1.cell(1, 2).merge(table_1.cell(2, 2))
- ############test##############
- doc.add_heading('2、指标名称与实际检测样品数量完整性审核', level=2)
- # 插入表格2
- paragraph_2 = doc.add_paragraph()
- paragraph_2.add_run(f'表2:{areaName}指标名称与实际检测样品数量统计表').bold = True
- table_2 = doc.add_table(rows=len(table_2_data) + 1, cols=3, style='Light Shading Accent 1')
- paragraph_2.alignment = WD_ALIGN_PARAGRAPH.CENTER
- table_2.alignment = WD_TABLE_ALIGNMENT.CENTER
- for i, row in enumerate(table_2.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_2_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_2_data.iloc[i - 1, j]))
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- doc.add_heading('二、数据规范性审核', level=1)
- long_text3 = """
- 按照《第三次全国土壤普查暂行土壤分类系统 (试行)》、《第三次全国土壤普查工作底图制作与采样点布设技术规范》、《第三次全国土壤普查土壤类型图编制技术规范》《第三次全国土壤普查土壤样品制备与检测技术规范》等规范统一标准和表现形式,土地利用方式按照第三次国土调查土地利用信息统一,种植制度按照农业区划信息进行统一。三普工作平台同时内置指标量纲、数字修约和允差等,控制数据规范性。
- 根据《第三次全国土壤普查土壤样品制备与检测技术规范(修订版)》,对检测指标对应的检测方法与相关标准或规范进行审核,得到检测方法审核、数值修约审核、数据填报项审核结果,未检出项填报以检出限的一半上报并加注*号。根据土壤普查教材中机械组成分类参考表,并进行宁国土壤质地填报审核结果表(见表6)。
- """
- para = doc.add_paragraph(long_text3)
- run1 = para.runs[0]
- run1.font.name = '宋体' # 设置字体为宋体
- run1.font.size = Pt(11) # 设置字号为11磅
- doc.add_heading('1、数据填报规范性审核', level=2)
- doc.add_heading('1、数据填报规范性审核', level=2)
- # 插入表3
- paragraph_3 = doc.add_paragraph()
- paragraph_3.add_run(f'表3:{areaName}土壤检测数据检测方法填报审核结果表').bold = True
- # table_3 = doc.add_table(rows=2, cols=2)
- paragraph_3.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_3.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 写入数据 这里数据写不下 嵌入链接
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:检测方法审核结果.xlsx', level=4)
- doc.add_heading('2、数值修约规范性审核', level=2)
- # 插入表4
- paragraph_4 = doc.add_paragraph()
- paragraph_4.add_run(f'表4:{areaName}土壤检测数据数值修约结果表').bold = True
- # table_4 = doc.add_table(rows=2, cols=2)
- paragraph_4.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_4.alignment = WD_TABLE_ALIGNMENT.CENTER
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数值修约审核.xlsx', level=4)
- # 填入数据 这里数据也放不下 嵌入链接
- doc.add_heading('3、数据未检出的填报规范性审核', level=2)
- # 插入表5
- paragraph_5 = doc.add_paragraph()
- paragraph_5.add_run(f'表5:{areaName}土壤检测数据未检出项填报审核结果表').bold = True
- # table_5 = doc.add_table(rows=2, cols=2)
- paragraph_5.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_5.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 写入数据 这里数据也放不下 嵌入链接
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据填报项审核结果.xlsx', level=4)
- # doc.add_heading('4、土壤质地填报规范性审核', level=2)
- # # 插入表6
- # paragraph_6 = doc.add_paragraph()
- # paragraph_6.add_run(f'表6:{areaName}土壤质地填报审核结果表').bold = True
- # table_6 = doc.add_table(rows=len(table_6_data) + 1, cols=4, style='Light Shading Accent 1')
- # paragraph_6.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_6.alignment = WD_TABLE_ALIGNMENT.CENTER
- # # 提取结果表中数据
- # # 写入数据 土壤质地类型不一致的数据提取出来
- # for i, row in enumerate(table_6.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(table_6_data.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(table_6_data.iloc[i - 1, j]))
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- doc.add_heading('三、数据合理性审核', level=1)
- long_text4 = """
- 明确数据审核原则,结合外业调查采样,获取的成土环境、土壤利用现状等信息对结果进行综合评价。通过阈值分析、关联分析、逻辑分析等方法对数据准确性进行判断。
- 阈值法(国家和省级数据阈值):结合区域历史数据建立阈值,平台利用阈值法(国家和省级数据值)自动对检测数据进行初步审核,并对超出阈值范围数据做出警示标识,专家或专业技术人员结合区域实际、专家经验判定异常值并对异常数据进行驳回。超出阈值颜色标注。
- 红色: 超出国家阈值,维持试点期间的标红。
- 黄色: 省级阈值在国家阈值范围内的,数据超出省级阈值标黄。
- 紫色: 省级阈值超出国家阈值的,数据超出省级阈值标成紫色(注,此种情况超国家阈值的红色服从紫色,仅标紫色)
- 极值法: 常用的统计量是均值、标准差、最大值、最小值、分位数等,用来判断是否超出了合理的极值范围。根据相关指标历年数据与指标最大值、最小值、平均数、中位数和极差进行审查,结合相关外业情况。
- 关联分析方法: 存在量化关系的指标,通过设定组合筛选异常值,如碳氮比等关联分析。
- 指标综合分析:根据指标相互之间的关系,如有效磷在全磷中的占比、全钾与速效钾缓效钾之和的关系等。有机质、全磷、有效磷、全钾等数据异常; 全氮和有机质之间关系不合理; 速效钾含量远大于缓效钾,盐基总量远大于阳离子交换量; 部分样品阳离子交换量和交换性盐基总量测定结果偏低; 水溶盐离子总量与全盐量的相对偏差已远超过方法规定的允差范围; 容重检测偏差比较大;水稳性大团聚体土壤干燥结块,影响测定结果、重金属按照风险筛选值和风险管控值等问题。
- 数据质量溯源:异常数据溯源,可能是内业测试化验环节问题,也有可能是外业调查采样问题,要结合数据检测和异常复检情况具体判断。
- """
- para = doc.add_paragraph(long_text4)
- run1 = para.runs[0]
- run1.font.name = '宋体' # 设置字体为宋体
- run1.font.size = Pt(11) # 设置字号为11磅
- doc.add_heading('1、阈值法审核', level=2)
- # 插入表格
- paragraph_7 = doc.add_paragraph()
- paragraph_7.add_run(f'表6:{areaName}土壤检测数据超阈值样品统计表').bold = True
- # table_7 = doc.add_table(rows=2, cols=2)
- # paragraph_7.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_7.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 写入数据 点击查看数据 这里也不一定写的下 最好是嵌入链接
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据审核过程存疑数据一览表.xlsx', level=4)
- # todo 合并所有数据 审核结果不为空的数据 写入表格保存到指定文件夹
- doc.add_heading('2、极值法审核', level=2)
- doc.add_heading('(1)pH', level=3)
- # 插入ph分布图
- if os.path.isfile(f'{mkdir_path}/pH值分布图.png'):
- doc.add_picture(f'{mkdir_path}/pH值分布图.png', width=Inches(6.0))
- paragraph_t_1 = doc.add_paragraph()
- paragraph_t_1.add_run(f'图1:pH值分布情况').bold = True
- paragraph_t_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # 插入频度统计表
- paragraph_8 = doc.add_paragraph()
- paragraph_8.add_run('表7:pH数据统计表').bold = True
- table_8 = doc.add_table(rows=6, cols=2, style='Light Shading Accent 1')
- t_8 = table_8_data['频度分析']
- t_8 = t_8.reset_index()
- t_8.columns = ['指标', '数据']
- paragraph_8.alignment = WD_ALIGN_PARAGRAPH.CENTER
- table_8.alignment = WD_TABLE_ALIGNMENT.CENTER
- for i, row in enumerate(table_8.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(t_8.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(t_8.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 插入异常数据提取表格 todo 这里数据多的话也可能写不下 最好是嵌入一下
- if not table_8_data['异常数据'].empty:
- paragraph_9 = doc.add_paragraph()
- paragraph_9.add_run('表8:pH异常数据统计表').bold = True
- table_9 = doc.add_table(rows=len(table_8_data['异常数据']) + 1, cols=6, style='Light Shading Accent 1')
- t_9 = table_8_data['异常数据']
- paragraph_9.alignment = WD_ALIGN_PARAGRAPH.CENTER
- table_9.alignment = WD_TABLE_ALIGNMENT.CENTER
- for i, row in enumerate(table_9.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(t_9.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(t_9.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- doc.add_heading('3、指标综合分析', level=2)
- # 阳离子交换量与交换性盐总量关系
- if os.path.isfile(f'{mkdir_path}/阳离子交换量与交换性盐基总量相关性散点图.png'):
- doc.add_picture(f'{mkdir_path}/阳离子交换量与交换性盐基总量相关性散点图.png', width=Inches(6.0))
- paragraph_t_8 = doc.add_paragraph()
- paragraph_t_8.add_run(f'图8:阳离子交换量与交换性盐基总量相关性散点图').bold = True
- paragraph_t_8.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # 交换性盐总量与交换性盐相关关系
- if os.path.isfile(f'{mkdir_path}/交换性盐基总量与交换性盐相关关系(pH小于等于7.5).png'):
- doc.add_picture(f'{mkdir_path}/交换性盐基总量与交换性盐相关关系(pH小于等于7.5).png', width=Inches(6.0))
- paragraph_t_9 = doc.add_paragraph()
- paragraph_t_9.add_run(f'图9:交换性盐基总量和交换性钙镁钠钾分项指标关系(pH≤7.5)').bold = True
- paragraph_t_9.alignment = WD_ALIGN_PARAGRAPH.CENTER
- if os.path.isfile(f'{mkdir_path}/交换性盐基总量与交换性盐相关关系(pH大于7.5).png'):
- doc.add_picture(f'{mkdir_path}/交换性盐基总量与交换性盐相关关系(pH大于7.5).png', width=Inches(6.0))
- paragraph_t_10 = doc.add_paragraph()
- paragraph_t_10.add_run(f'图10:交换性盐基总量和交换性钙镁钠钾分项指标关系(pH大于7.5)').bold = True
- paragraph_t_10.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # 水溶性盐、电导率、离子总量
- if os.path.isfile(f'{mkdir_path}/全盐量分布图.png'):
- doc.add_picture(f'{mkdir_path}/全盐量分布图.png', width=Inches(6.0))
- paragraph_t_11 = doc.add_paragraph()
- paragraph_t_11.add_run(f'图11:全盐量分布图').bold = True
- paragraph_t_11.alignment = WD_ALIGN_PARAGRAPH.CENTER
- if os.path.isfile(f'{mkdir_path}/全盐量与电导率相关性散点图.png'):
- doc.add_picture(f'{mkdir_path}/全盐量与电导率相关性散点图.png', width=Inches(6.0))
- paragraph_t_12 = doc.add_paragraph()
- paragraph_t_12.add_run(f'图12:全盐量与电导率相关性散点图').bold = True
- paragraph_t_12.alignment = WD_ALIGN_PARAGRAPH.CENTER
- if os.path.isfile(f'{mkdir_path}/全盐量与离子总量相关性散点图.png'):
- doc.add_picture(f'{mkdir_path}/全盐量与离子总量相关性散点图.png', width=Inches(6.0))
- paragraph_t_13 = doc.add_paragraph()
- paragraph_t_13.add_run(f'图13:全盐量与离子总量相关性散点图').bold = True
- paragraph_t_13.alignment = WD_ALIGN_PARAGRAPH.CENTER
- doc.add_heading('四、审核存疑数据', level=1)
- paragraph_12 = doc.add_paragraph()
- paragraph_12.add_run(f'表9:数据审核过程存疑数据一览表').bold = True
- paragraph_12.alignment = WD_ALIGN_PARAGRAPH.CENTER
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据审核过程存疑数据一览表.xlsx', level=4)
- doc.add_heading('五、附表', level=1)
- doc.add_heading('附表1:某区三普样品数量统计表(表层)', level=2)
- # 插入附表1
- table_1_f = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
- table_1_f.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 遍历表格 插入数据
- # 遍历表格的所有单元格,并填充内容
- for i, row in enumerate(table_1_f.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_1_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 合并单元格 合并第3列的第二行和第三行
- if lenNum_1 > 1:
- table_1_f.cell(2, 2).text = ''
- table_1_f.cell(1, 2).merge(table_1_f.cell(2, 2))
- doc.add_heading('附表2:各指标频度分析表', level=2)
- # 插入表格 写入数据
- table_f_2_data = table_f_2_data.replace(np.nan, '')
- makeInfoTable(table_f_2_data, doc)
- # table_f_2 = doc.add_table(rows=len(table_f_2_data) + 1, cols=6, style='Light Shading Accent 1')
- # for i, row in enumerate(table_f_2.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(table_f_2_data.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(table_f_2_data.iloc[i - 1, j]))
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:频度分析表.xlsx', level=4)
- doc.add_heading('附表3:各指标频度分析图', level=2)
- # 插入频度信息的图形
- if os.path.isfile(f'{mkdir_path}/pH分析图.png'):
- doc.add_picture(f'{mkdir_path}/pH分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/电导率分析图.png'):
- doc.add_picture(f'{mkdir_path}/电导率分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/交换性钙分析图.png'):
- doc.add_picture(f'{mkdir_path}/交换性钙分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/交换性钾分析图.png'):
- doc.add_picture(f'{mkdir_path}/交换性钾分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/交换性镁分析图.png'):
- doc.add_picture(f'{mkdir_path}/交换性镁分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/交换性钠分析图.png'):
- doc.add_picture(f'{mkdir_path}/交换性钠分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/交换性盐基总量分析图.png'):
- doc.add_picture(f'{mkdir_path}/交换性盐基总量分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/全盐量分析图.png'):
- doc.add_picture(f'{mkdir_path}/全盐量分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/阳离子交换量分析图.png'):
- doc.add_picture(f'{mkdir_path}/阳离子交换量分析图.png', width=Inches(6.0))
- doc.add_heading('附表4:数值修约标准', level=2)
- # 读取数据 插入表格 写入数据
- numData = pd.read_excel('./img/数值修约要求.xlsx', sheet_name='Sheet1')
- table_2_f = doc.add_table(rows=len(numData) + 1, cols=2, style='Light Shading Accent 1')
- table_2_f.alignment = WD_TABLE_ALIGNMENT.CENTER
- for i, row in enumerate(table_2_f.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(numData.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(numData.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 处理样式 遍历所有的段落 修改字体
- # 遍历并打印每个段落的文本
- paragraphs = doc.paragraphs
- for paragraph in paragraphs:
- for run in paragraph.runs:
- run.font.color.rgb = RGBColor(0, 0, 0)
- run.font.name = 'Times New Roman'
- run.font.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # run.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # 保存Word文档
- doc.save(f'{mkdir_path}/{areaName}-{type}审核报告.docx')
- # 生成重金属指标审核报告
- def getHeavyMetalIndicators(originData, data, type, changeFileUrl, saveFileUrl, check_1_data,
- check_3_data,
- check_5_data,
- check_8_data, # 样品编号替换为编号
- check_10_data,
- check_12_data,
- check_14_data):
- # 生成报告
- name = os.path.basename(changeFileUrl)
- n = name.split('.')
- areaName = n[0].replace('数据', '')
- # 生成一个新的文件夹用于存放审核报告相关的数据
- nowTime = time.strftime("%Y-%m-%d %H时%M分%S秒", time.localtime())
- dir_name = f'{areaName}-{type}数据审核报告'
- mkdir_path = saveFileUrl + '/' + dir_name + nowTime
- if not os.path.exists(mkdir_path):
- os.mkdir(mkdir_path)
- heavyMetaData = data[indexClassificationList[type]]
- heavyMetaDataNum = originData[indexClassificationList[type]]
- report.getFrequencyImage(heavyMetaData, mkdir_path)
- heavyMetaData['序号'] = data['序号']
- heavyMetaData['原样品编号'] = data['原样品编号']
- heavyMetaData['样品编号'] = data['样品编号']
- heavyMetaData['地理位置'] = data['地理位置']
- heavyMetaData['母质'] = data['母质']
- heavyMetaData['土壤类型'] = data['土壤类型']
- heavyMetaData['土地利用类型'] = data['土地利用类型']
- # heavyMetaData['土壤质地'] = data['土壤质地']
- heavyMetaData['原样品编号'] = heavyMetaData['原样品编号'].astype(str)
- # checkData = pd.read_excel(changeFileUrl, sheet_name='检测方法')
- heavyMetaDataNum['序号'] = originData['序号']
- heavyMetaDataNum['原样品编号'] = originData['原样品编号']
- heavyMetaDataNum['样品编号'] = originData['样品编号']
- heavyMetaDataNum['地理位置'] = originData['地理位置']
- heavyMetaDataNum['母质'] = originData['母质']
- heavyMetaDataNum['土壤类型'] = originData['土壤类型']
- heavyMetaDataNum['土地利用类型'] = originData['土地利用类型']
- # heavyMetaData['土壤质地'] = data['土壤质地']
- heavyMetaDataNum['原样品编号'] = heavyMetaDataNum['原样品编号'].astype(str)
- # 上面这个地址,可以纯递给函数中,用于保存表格和图片
- # 调用函数 开始生成报告相关内容
- # 表1相关数据
- typeData = report.getSimpleNum(heavyMetaData)
- lenNum_1 = len(typeData['sData'])
- lenNum_1_f = len(typeData['allData'])
- table_1_data = pd.DataFrame({
- '类型': typeData['sData'].index,
- '数量': typeData['sData'],
- '合计': [typeData['sData'].sum() for _ in range(lenNum_1)]
- })
- # 表2数据
- table_2_data = report.getDataComplete(heavyMetaData)
- table_2_data = table_2_data.reset_index()
- table_2_data.columns = ['指标名称', '实测数量', '应测数量']
- # 表3数据
- # table_3_data = report.checkMethod(checkData, mkdir_path)
- # 数据修约 表4
- report.getNum(heavyMetaDataNum, mkdir_path)
- # 数据填报项审核 表5
- report.dataReportResult(heavyMetaData, mkdir_path)
- # 表6数据 土壤质地类型不一致
- middData = heavyMetaData[['原样品编号', '样品编号']].astype(str)
- middData['编号'] = middData['原样品编号']
- del middData['原样品编号']
- check_1_data = pd.merge(check_1_data, middData, how='left', on='编号')
- check_1_data = check_1_data.replace(np.nan, '')
- # typeNotSame = check_1_data[check_1_data['土壤质地'] != check_1_data['土壤类型(判断)']]
- # table_6_data = typeNotSame[['编号', '样品编号', '土壤质地', '土壤类型(判断)']]
- allNeedData = pd.DataFrame({})
- allNeedData['原样品编号'] = check_1_data['编号']
- getSimpleDataNumber = pd.merge(allNeedData, heavyMetaData[['原样品编号', '样品编号']], how='left', on="原样品编号")
- allNeedData['样品编号'] = getSimpleDataNumber['样品编号']
- allNeedData['土地利用类型'] = check_1_data['土地利用类型']
- allNeedData['审核结果'] = check_14_data['审核结果']
- allNeedData['外业'] = ['' for _ in range(len(check_1_data))]
- table_7_data = allNeedData[allNeedData['审核结果'] != '']
- del table_7_data['审核结果']
- # 写进表格
- with pd.ExcelWriter(f'{mkdir_path}/超阈值样品统计表.xlsx', engine='openpyxl') as writer:
- table_7_data.to_excel(writer, index=False, sheet_name='超阈值数据')
- # 表8数据
- #table_8_data = report.getPHData(heavyMetaData, mkdir_path)
- # 表12数据 重金属超标
- caOverData = pd.merge(check_1_data[['编号', '土地利用类型']], check_14_data[
- ['编号', 'pH', '镉mg/kg', '汞mg/kg', '砷mg/kg', '铅mg/kg', '铬mg/kg', '镍mg/kg', '审核结果']], how='outer',
- on=['编号'])
- caOverData['原样品编号'] = caOverData['编号']
- caOverData = pd.merge(caOverData, heavyMetaData[['原样品编号', '样品编号']], how='left', on='原样品编号')
- first_column = caOverData.pop('样品编号')
- caOverData.insert(0, '样品编号', first_column)
- caOverData_need = caOverData[caOverData['审核结果'] != '']
- # 写进表格
- with pd.ExcelWriter(f'{mkdir_path}/重金属超筛选值情况统计.xlsx', engine='openpyxl') as writer:
- caOverData_need.to_excel(writer, index=False, sheet_name='重金属超筛选值情况统计')
- # 表13 所有存疑数据
- with pd.ExcelWriter(f'{mkdir_path}/数据审核过程存疑数据一览表.xlsx', engine='openpyxl') as writer:
- allNeedData[allNeedData['审核结果'] != ''].to_excel(writer, index=False, sheet_name='存疑数据')
- # 附表: 频度分析图
- # report.getFrequencyImage(heavyMetaData, mkdir_path)
- table_f_2_data = report.getFrequencyInformation(data, mkdir_path)
- # 新建一个文档
- doc = Document()
- # 添加标题
- doc.add_heading(f"{areaName}第三次全国土壤普查重金属指标检测数据审核报告", level=0)
- # 在文档中添加封面段落
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- # 插入图片,设置宽度为6英寸(可根据需求调整)
- run = fm.add_run()
- run.add_picture('img/第三次全国土壤普查img.png', width=Inches(2.26))
- fm.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 在文档中添加封面段落
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- fm = doc.add_paragraph()
- # 获取当前日期
- current_date = datetime.now()
- # 将年份和月份转换为中文大写数字
- year = int(current_date.strftime("%Y")) # 转换为整数
- month = int(current_date.strftime("%m")) # 转换为整数
- # 使用 cn2an 将数字转换为中文大写
- year_chinese = number_to_chinese_year(year) # 年份转换
- month_chinese = cn2an.an2cn(month) # 月份转换
- current_date_formatted = f"{year_chinese}年{month_chinese}月"
- # 组合动态文本
- dynamic_text = f"安徽农业大学资源与环境学院\n{current_date_formatted}"
- # 添加文字并居中
- text_paragraph = doc.add_paragraph()
- text_run = text_paragraph.add_run(dynamic_text)
- text_run.font.name = "宋体"
- text_run.font.size = Pt(18)
- text_run.bold = True # 设置字体加粗
- text_paragraph.alignment = 1 # 1 表示居中对齐
- # 正确插入分页符
- doc.add_page_break()
- heading = doc.add_heading('总体概述', level=1)
- heading.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # 第一段
- long_text1 = f"""
- {areaName}第三次全国土壤普查县级数据审核报告主要通过收集和整理相关数据,并对其进行内业检测数据的完整性、规范性和合理性进行审核,形成存疑样点清单及存疑样点结果判定,最终编制完成数据审核报告,同时提交( )对相关指标进行整改复测。报告整理了( )个表层样品数据(含平行样、质控样)、( )个水稳性大团聚体样品数据(含平行样)、( )个剖面样品数据(含平行样、质控样),共( )次样品检测结果分析情况。相关结果分别按照物理性指标检测数据、一般化学指标检测数据、常规养分指标检测数据和重金属指标检测数据形成四份报告。本报告为表层样常规养分指标检测数据审核报告。
- """
- para0 = doc.add_paragraph(long_text1)
- run0 = para0.runs[0] # 获取段落中的第一个run对象
- run0.font.name = '宋体' # 设置字体为宋体
- run0.font.size = Pt(11) # 设置字号为11磅
- # 设置段落的行间距为1.5倍
- para_format = para0.paragraph_format
- para_format.line_spacing = 1.5 # 设置行间距为1.5倍
- # 添加一级标题
- doc.add_heading('一、数据完整性审核', level=1)
- # 第二段
- long_text2 = """
- 外业信息调查采样环节:采用电子围栏和外业调查采样APP,对采样位置和填报信息进行管理,确保外业调查信息填报完整。
- 样品检测数据上报环节:通过土壤普查工作平台对上报数据的完整性进行筛查。( )第三次土壤普查相关指标检测数据由( )提供,数据均已通过省级质控实验室和县级土壤普查办审核;相关土壤指标历史数据则由( )第三次土壤普查办公室提供。根据《第三次全国土壤普查土壤样品制备与检测技术规范(修订版)》要求,统计各土地利用类型的样品数量,并按照耕地园地土壤样品(表层/剖面)、林地草地土壤样品(表层/剖面)以及水稳定性大团聚体样品(见表1)进行分类,编制了指标名称与实际检测样品数量统计表(见表2),其中水溶性盐分总量大于1g/kg,增加检测了八大离子(该指标在化学指标检测数据审核报告内)。
- """
- para = doc.add_paragraph(long_text2)
- run1 = para.runs[0]
- run1.font.name = '宋体' # 设置字体为宋体
- run1.font.size = Pt(11) # 设置字号为11磅
- # 设置段落的行间距为1.5倍
- para_format = para.paragraph_format
- para_format.line_spacing = 1.5 # 设置行间距为1.5倍
- doc.add_heading('1、土地利用类型与检测指标符合性审核', level=2)
- # 插入表格1
- paragraph_1 = doc.add_paragraph()
- paragraph_1.add_run(f"表1:{areaName}三普样品数量统计表(表层)").bold = True
- # 设置居中
- paragraph_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
- table_1 = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
- table_1.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 遍历表格 插入数据
- # 遍历表格的所有单元格,并填充内容
- for i, row in enumerate(table_1.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_1_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 合并单元格 合并第3列的第二行和第三行
- if lenNum_1 > 1:
- table_1.cell(2, 2).text = ''
- table_1.cell(1, 2).merge(table_1.cell(2, 2))
- ############test##############
- doc.add_heading('2、指标名称与实际检测样品数量完整性审核', level=2)
- # 插入表格2
- paragraph_2 = doc.add_paragraph()
- paragraph_2.add_run(f'表2:{areaName}指标名称与实际检测样品数量统计表').bold = True
- table_2 = doc.add_table(rows=len(table_2_data) + 1, cols=3, style='Light Shading Accent 1')
- paragraph_2.alignment = WD_ALIGN_PARAGRAPH.CENTER
- table_2.alignment = WD_TABLE_ALIGNMENT.CENTER
- for i, row in enumerate(table_2.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_2_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_2_data.iloc[i - 1, j]))
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- doc.add_heading('二、数据规范性审核', level=1)
- long_text3 = """
- 按照《第三次全国土壤普查暂行土壤分类系统 (试行)》、《第三次全国土壤普查工作底图制作与采样点布设技术规范》、《第三次全国土壤普查土壤类型图编制技术规范》《第三次全国土壤普查土壤样品制备与检测技术规范》等规范统一标准和表现形式,土地利用方式按照第三次国土调查土地利用信息统一,种植制度按照农业区划信息进行统一。三普工作平台同时内置指标量纲、数字修约和允差等,控制数据规范性。
- 根据《第三次全国土壤普查土壤样品制备与检测技术规范(修订版)》,对检测指标对应的检测方法与相关标准或规范进行审核,得到检测方法审核、数值修约审核、数据填报项审核结果,未检出项填报以检出限的一半上报并加注*号。根据土壤普查教材中机械组成分类参考表,并进行宁国土壤质地填报审核结果表(见表6)。
- """
- para = doc.add_paragraph(long_text3)
- run1 = para.runs[0]
- run1.font.name = '宋体' # 设置字体为宋体
- run1.font.size = Pt(11) # 设置字号为11磅
- doc.add_heading('1、数据填报规范性审核', level=2)
- doc.add_heading('1、数据填报规范性审核', level=2)
- # 插入表3
- paragraph_3 = doc.add_paragraph()
- paragraph_3.add_run(f'表3:{areaName}土壤检测数据检测方法填报审核结果表').bold = True
- # table_3 = doc.add_table(rows=2, cols=2)
- paragraph_3.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_3.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 写入数据 这里数据写不下 嵌入链接
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:检测方法审核结果.xlsx', level=4)
- doc.add_heading('2、数值修约规范性审核', level=2)
- # 插入表4
- paragraph_4 = doc.add_paragraph()
- paragraph_4.add_run(f'表4:{areaName}土壤检测数据数值修约结果表').bold = True
- # table_4 = doc.add_table(rows=2, cols=2)
- paragraph_4.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_4.alignment = WD_TABLE_ALIGNMENT.CENTER
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数值修约审核.xlsx', level=4)
- # 填入数据 这里数据也放不下 嵌入链接
- doc.add_heading('3、数据未检出的填报规范性审核', level=2)
- # 插入表5
- paragraph_5 = doc.add_paragraph()
- paragraph_5.add_run(f'表5:{areaName}土壤检测数据未检出项填报审核结果表').bold = True
- # table_5 = doc.add_table(rows=2, cols=2)
- paragraph_5.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_5.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 写入数据 这里数据也放不下 嵌入链接
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据填报项审核结果.xlsx', level=4)
- # doc.add_heading('4、土壤质地填报规范性审核', level=2)
- # 插入表6
- # paragraph_6 = doc.add_paragraph()
- # paragraph_6.add_run(f'表6:{areaName}土壤质地填报审核结果表').bold = True
- # table_6 = doc.add_table(rows=len(table_6_data) + 1, cols=4, style='Light Shading Accent 1')
- # paragraph_6.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_6.alignment = WD_TABLE_ALIGNMENT.CENTER
- # # 提取结果表中数据
- # # 写入数据 土壤质地类型不一致的数据提取出来
- # for i, row in enumerate(table_6.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(table_6_data.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(table_6_data.iloc[i - 1, j]))
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- doc.add_heading('三、数据合理性审核', level=1)
- long_text4 = """
- 明确数据审核原则,结合外业调查采样,获取的成土环境、土壤利用现状等信息对结果进行综合评价。通过阈值分析、关联分析、逻辑分析等方法对数据准确性进行判断。
- 阈值法(国家和省级数据阈值):结合区域历史数据建立阈值,平台利用阈值法(国家和省级数据值)自动对检测数据进行初步审核,并对超出阈值范围数据做出警示标识,专家或专业技术人员结合区域实际、专家经验判定异常值并对异常数据进行驳回。超出阈值颜色标注。
- 红色: 超出国家阈值,维持试点期间的标红。
- 黄色: 省级阈值在国家阈值范围内的,数据超出省级阈值标黄。
- 紫色: 省级阈值超出国家阈值的,数据超出省级阈值标成紫色(注,此种情况超国家阈值的红色服从紫色,仅标紫色)
- 极值法: 常用的统计量是均值、标准差、最大值、最小值、分位数等,用来判断是否超出了合理的极值范围。根据相关指标历年数据与指标最大值、最小值、平均数、中位数和极差进行审查,结合相关外业情况。
- 关联分析方法: 存在量化关系的指标,通过设定组合筛选异常值,如碳氮比等关联分析。
- 指标综合分析:根据指标相互之间的关系,如有效磷在全磷中的占比、全钾与速效钾缓效钾之和的关系等。有机质、全磷、有效磷、全钾等数据异常; 全氮和有机质之间关系不合理; 速效钾含量远大于缓效钾,盐基总量远大于阳离子交换量; 部分样品阳离子交换量和交换性盐基总量测定结果偏低; 水溶盐离子总量与全盐量的相对偏差已远超过方法规定的允差范围; 容重检测偏差比较大;水稳性大团聚体土壤干燥结块,影响测定结果、重金属按照风险筛选值和风险管控值等问题。
- 数据质量溯源:异常数据溯源,可能是内业测试化验环节问题,也有可能是外业调查采样问题,要结合数据检测和异常复检情况具体判断。
- """
- para = doc.add_paragraph(long_text4)
- run1 = para.runs[0]
- run1.font.name = '宋体' # 设置字体为宋体
- run1.font.size = Pt(11) # 设置字号为11磅
- doc.add_heading('1、阈值法审核', level=2)
- # 插入表格
- paragraph_7 = doc.add_paragraph()
- paragraph_7.add_run(f'表6:{areaName}土壤检测数据超阈值样品统计表').bold = True
- # table_7 = doc.add_table(rows=2, cols=2)
- # paragraph_7.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_7.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 写入数据 点击查看数据 这里也不一定写的下 最好是嵌入链接
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据审核过程存疑数据一览表.xlsx', level=4)
- # todo 合并所有数据 审核结果不为空的数据 写入表格保存到指定文件夹
- # doc.add_heading('2、极值法审核', level=2)
- # doc.add_heading('(1)pH', level=3)
- # # 插入ph分布图
- # if os.path.isfile(f'{mkdir_path}/pH值分布图.png'):
- # doc.add_picture(f'{mkdir_path}/pH值分布图.png', width=Inches(6.0))
- # paragraph_t_1 = doc.add_paragraph()
- # paragraph_t_1.add_run(f'图1:pH值分布情况').bold = True
- # paragraph_t_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
- #
- # # 插入频度统计表
- # paragraph_8 = doc.add_paragraph()
- # paragraph_8.add_run('表7:pH数据统计表').bold = True
- # table_8 = doc.add_table(rows=6, cols=2, style='Light Shading Accent 1')
- # t_8 = table_8_data['频度分析']
- #
- # t_8 = t_8.reset_index()
- # t_8.columns = ['指标', '数据']
- # paragraph_8.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_8.alignment = WD_TABLE_ALIGNMENT.CENTER
- # for i, row in enumerate(table_8.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(t_8.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(t_8.iloc[i - 1, j]))
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- #
- # # 插入异常数据提取表格 todo 这里数据多的话也可能写不下 最好是嵌入一下
- # if not table_8_data['异常数据'].empty:
- # paragraph_9 = doc.add_paragraph()
- # paragraph_9.add_run('表8:pH异常数据统计表').bold = True
- # table_9 = doc.add_table(rows=len(table_8_data['异常数据']) + 1, cols=6, style='Light Shading Accent 1')
- # t_9 = table_8_data['异常数据']
- #
- # paragraph_9.alignment = WD_ALIGN_PARAGRAPH.CENTER
- # table_9.alignment = WD_TABLE_ALIGNMENT.CENTER
- # for i, row in enumerate(table_9.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(t_9.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(t_9.iloc[i - 1, j]))
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- doc.add_heading('4、指标综合分析', level=2)
- doc.add_heading('表9:重金属超筛选值情况统计', level=4)
- # todo 获取重金属数据
- doc.add_heading('四、审核存疑数据', level=1)
- paragraph_12 = doc.add_paragraph()
- paragraph_12.add_run(f'表10:数据审核过程存疑数据一览表').bold = True
- paragraph_12.alignment = WD_ALIGN_PARAGRAPH.CENTER
- doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:数据审核过程存疑数据一览表.xlsx', level=4)
- doc.add_heading('五、附表', level=1)
- doc.add_heading('附表1:某区三普样品数量统计表(表层)', level=2)
- # 插入附表1
- table_1_f = doc.add_table(rows=lenNum_1 + 1, cols=3, style='Light Shading Accent 1')
- table_1_f.alignment = WD_TABLE_ALIGNMENT.CENTER
- # 遍历表格 插入数据
- # 遍历表格的所有单元格,并填充内容
- for i, row in enumerate(table_1_f.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(table_1_data.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(table_1_data.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 合并单元格 合并第3列的第二行和第三行
- if lenNum_1 > 1:
- table_1_f.cell(2, 2).text = ''
- table_1_f.cell(1, 2).merge(table_1_f.cell(2, 2))
- doc.add_heading('附表2:各指标频度分析表', level=2)
- # 插入表格 写入数据
- table_f_2_data = table_f_2_data.replace(np.nan, '')
- makeInfoTable(table_f_2_data, doc)
- # table_f_2 = doc.add_table(rows=len(table_f_2_data) + 1, cols=6, style='Light Shading Accent 1')
- # for i, row in enumerate(table_f_2.rows):
- # for j, cell in enumerate(row.cells):
- # # 获取单元格中的段落对象
- # paragraph = cell.paragraphs[0]
- # if i == 0:
- # r = paragraph.add_run(str(table_f_2_data.columns[j]))
- # r.font.bold = True
- # else:
- # r = paragraph.add_run(str(table_f_2_data.iloc[i - 1, j]))
- # r.font.size = Pt(10.5)
- # r.font.name = 'Times New Roman'
- # r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- # paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- # paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # doc.add_heading('为避免数据量过多无法显示,请至数据保存文件夹中查看数据表:频度分析表.xlsx', level=4)
- doc.add_heading('附表3:各指标频度分析图', level=2)
- # 插入频度信息的图形
- if os.path.isfile(f'{mkdir_path}/pH分析图.png'):
- doc.add_picture(f'{mkdir_path}/pH分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/总镉分析图.png'):
- doc.add_picture(f'{mkdir_path}/总镉分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/总铬分析图.png'):
- doc.add_picture(f'{mkdir_path}/总铬分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/总汞分析图.png'):
- doc.add_picture(f'{mkdir_path}/总汞分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/总镍分析图.png'):
- doc.add_picture(f'{mkdir_path}/总镍分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/总砷分析图.png'):
- doc.add_picture(f'{mkdir_path}/总砷分析图.png', width=Inches(6.0))
- if os.path.isfile(f'{mkdir_path}/总铅分析图.png'):
- doc.add_picture(f'{mkdir_path}/总铅分析图.png', width=Inches(6.0))
- doc.add_heading('附表4:数值修约标准', level=2)
- # 读取数据 插入表格 写入数据
- numData = pd.read_excel('./img/数值修约要求.xlsx', sheet_name='Sheet1')
- table_2_f = doc.add_table(rows=len(numData) + 1, cols=2, style='Light Shading Accent 1')
- table_2_f.alignment = WD_TABLE_ALIGNMENT.CENTER
- for i, row in enumerate(table_2_f.rows):
- for j, cell in enumerate(row.cells):
- # 获取单元格中的段落对象
- paragraph = cell.paragraphs[0]
- if i == 0:
- r = paragraph.add_run(str(numData.columns[j]))
- r.font.bold = True
- else:
- r = paragraph.add_run(str(numData.iloc[i - 1, j]))
- r.font.size = Pt(10.5)
- r.font.name = 'Times New Roman'
- r.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
- paragraph.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 对齐
- paragraph.paragraph_format.line_spacing = 1 # 段落行间距
- # 处理样式 遍历所有的段落 修改字体
- # 遍历并打印每个段落的文本
- paragraphs = doc.paragraphs
- for paragraph in paragraphs:
- for run in paragraph.runs:
- run.font.color.rgb = RGBColor(0, 0, 0)
- run.font.name = 'Times New Roman'
- run.font.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # run.element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
- # 保存Word文档
- doc.save(f'{mkdir_path}/{areaName}-{type}审核报告.docx')
- def number_to_chinese_year(number):
- # 定义数字到中文大写的映射
- chinese_numerals = {'0': '〇', '1': '一', '2': '二', '3': '三',
- '4': '四', '5': '五', '6': '六', '7': '七',
- '8': '八', '9': '九'}
- # 将数字逐个字符转换为中文大写
- return ''.join(chinese_numerals[digit] for digit in str(number))
|