需求
在数据(excel格式 )导入的时候,需要对数据库中的数据信息与新导入的信息做比对,显示每项信息的差异点。
解决方法
导出数据库中的文件为excel格式,使用 beyondcompare 工具直接比对两个excel文件即可
步骤
beyondcompare 执行命令,比对file1 与file2文件,输出为html文件,输出格式具体设置在 bcscript.txt 中
BCompare.exe "@bcscript.txt" "file1" "file2" "diff.html"
bcscript.txt 文件内容
file-report layout:side-by-side options:display-mismatches output-to:"%3" output-options:wrap-word,html-color "%1" "%2"
比对结果显示
显示优化
使用 BeautifulSoup 解析比对结果,使用jinja2重新渲染显示格式
from bs4 import BeautifulSoup
from jinja2 import FileSystemLoader, Environment
soup = BeautifulSoup(open('admin.html', encoding='utf-8'), 'html.parser')
tags = ''
diff_num = 0
for i in soup.find_all('tr')[1:]:
diff_num += 1
sys_stock_info = ''.join([str(x) for x in i.find_all('td')[:6]])
import_stcok_info = ''.join([str(x) for x in i.find_all('td')[7:13]])
tags += '''
<tr> <td colspan="7" ><b>差异点 %d </b></td></tr>
<tr class="SectionMiddle"><td>系统存货信息</td> %s </tr>
<tr class="SectionMiddle"><td>本次导入存货</td> %s </tr>
''' % (diff_num, sys_stock_info, import_stcok_info)
env = Environment(loader=FileSystemLoader(r'D:\codepath\kunlun_automation_bom\bom_system\utils')) # 指定模板文件的加载路径
template = env.get_template('temp.html') # 获取加载路劲下的模板文件
out_html = template.render(tags=tags) # 渲染
with open('out.html', 'w', encoding='utf-8') as f:
f.write(out_html)
temp.html 模板文件,省略css样式设置
****
<table cellspacing="0" cellpadding="0">
<tr>
<td width="90px"></td>
<td width="90px">料号</td>
<td width="90px">存货名称</td>
<td width="90px">生产类别</td>
<td>规格型号</td>
<td width="90px">品牌</td>
<td width="90px">互替品牌</td>
</tr>
{% autoescape off %}
{{ tags }}
{% endautoescape %}
</table>
****
重新生成的比对信息