冲突矿产披露报告CMRT管理工具
很多时候因为合规的管理,需要在整个供应链上搜集冲突矿产CMRT报告,少则几十家,多则上百家甚至更多,一旦数量多起来,那么如何审阅这些报告是比较头痛的事情,这里有一个Python小脚本,把所有搜集的报告放到一个文件夹里,然后在代码里写明这个文件夹地址,然后运行,就可以得到一个a.xlsx的文件,可以把所有报告的smelter list汇总到一起,包含相应的文件名,然后可以在Excel里判断是否有冶炼厂来自covered countries或者CAHRAs.
最后整理的Excel我就不放了,几个简单的公式即可。
下面是Python代码:
import os
import openpyxl
def copy_sheet_data(source_folder, sheet_name):
#source_directory = os.path.dirname(source_folder)
target_file = os.path.join(source_folder, "a.xlsx")
#print (source_directory)
# 如果目标文件不存在,则创建一个新的Excel文件
if not os.path.exists(target_file):
target_workbook = openpyxl.Workbook()
target_workbook.save(target_file)
target_workbook = openpyxl.load_workbook(target_file)
target_sheet = target_workbook.active
next_empty_row = len(target_sheet['A']) + 1
#print(next_empty_row)
for root, dirs, files in os.walk(source_folder):
for file in files:
if file.endswith('.xlsx') or file.endswith('.xls'):
file_path = os.path.join(root, file)
source_workbook = openpyxl.load_workbook(file_path,data_only=True)
if sheet_name in source_workbook.sheetnames:
source_sheet = source_workbook[sheet_name]
# 确定A列最后一行有内容的行号
max_row_in_a_column = max((cell.row for cell in source_sheet['A'] if cell.value), default=2)
#print (max_row_in_a_column)
for row in range(5, max_row_in_a_column + 1):
target_sheet.cell(row=next_empty_row, column=1).value = file
cell_value = source_sheet.cell(row=row, column=1).value
cell_value_metal = source_sheet.cell(row=row, column=2).value
target_sheet.cell(row=next_empty_row, column=3).value = cell_value_metal
if isinstance(cell_value, str):
target_sheet.cell(row=next_empty_row, column=2).value = cell_value
next_empty_row += 1
elif isinstance(cell_value, (int, float)):
target_sheet.cell(row=next_empty_row, column=2).value = str(cell_value)
next_empty_row += 1
if row == max_row_in_a_column:
next_empty_row += 1
source_workbook.close()
target_workbook.save(target_file)
if __name__ == "__main__":
source_folder = r"D:\aaa\bb\CMRT"
sheet_name = "Smelter List"
copy_sheet_data(source_folder, sheet_name)
最后整理的Excel我就不放了,几个简单的公式即可。
下面是Python代码:
import os
import openpyxl
def copy_sheet_data(source_folder, sheet_name):
#source_directory = os.path.dirname(source_folder)
target_file = os.path.join(source_folder, "a.xlsx")
#print (source_directory)
# 如果目标文件不存在,则创建一个新的Excel文件
if not os.path.exists(target_file):
target_workbook = openpyxl.Workbook()
target_workbook.save(target_file)
target_workbook = openpyxl.load_workbook(target_file)
target_sheet = target_workbook.active
next_empty_row = len(target_sheet['A']) + 1
#print(next_empty_row)
for root, dirs, files in os.walk(source_folder):
for file in files:
if file.endswith('.xlsx') or file.endswith('.xls'):
file_path = os.path.join(root, file)
source_workbook = openpyxl.load_workbook(file_path,data_only=True)
if sheet_name in source_workbook.sheetnames:
source_sheet = source_workbook[sheet_name]
# 确定A列最后一行有内容的行号
max_row_in_a_column = max((cell.row for cell in source_sheet['A'] if cell.value), default=2)
#print (max_row_in_a_column)
for row in range(5, max_row_in_a_column + 1):
target_sheet.cell(row=next_empty_row, column=1).value = file
cell_value = source_sheet.cell(row=row, column=1).value
cell_value_metal = source_sheet.cell(row=row, column=2).value
target_sheet.cell(row=next_empty_row, column=3).value = cell_value_metal
if isinstance(cell_value, str):
target_sheet.cell(row=next_empty_row, column=2).value = cell_value
next_empty_row += 1
elif isinstance(cell_value, (int, float)):
target_sheet.cell(row=next_empty_row, column=2).value = str(cell_value)
next_empty_row += 1
if row == max_row_in_a_column:
next_empty_row += 1
source_workbook.close()
target_workbook.save(target_file)
if __name__ == "__main__":
source_folder = r"D:\aaa\bb\CMRT"
sheet_name = "Smelter List"
copy_sheet_data(source_folder, sheet_name)