excel-toolkit/script_templates/currency_convert.py

56 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
货币/汇率转换模板
将指定列的金额从一种货币转换为另一种货币
"""
import pandas as pd
import sys
from pathlib import Path
def main():
# 参数配置
file_path = "{file}"
output_path = "{output}"
column = "{column}" # 要转换的列名
from_currency = "{from_currency}" # 原货币
to_currency = "{to_currency}" # 目标货币
rate = {rate} # 汇率默认1实际使用时需要提供
# 读取文件
df = pd.read_excel(file_path)
if column not in df.columns:
print(f"错误: 列 '{column}' 不存在")
print(f"可用列: {list(df.columns)}")
sys.exit(1)
# 转换货币
original_values = df[column].copy()
df[column] = df[column] * rate
# 添加元数据列(可选)
if "{add_meta}" == "true":
df[f"{column}_original"] = original_values
df[f"{column}_rate"] = rate
df[f"{column}_currency"] = to_currency
# 保存结果
df.to_excel(output_path, index=False)
print(f"货币转换完成")
print(f"文件: {{file_path}}")
print(f"列: {{column}}")
print(f"{{from_currency}} 转换为 {{to_currency}}")
print(f"汇率: {{rate}}")
print(f"输出: {{output_path}}")
# 显示统计信息
print(f"\n转换统计:")
print(f" 总行数: {{len(df)}}")
print(f" 原始总和: {{original_values.sum():.2f}} {{from_currency}}")
print(f" 转换后总和: {{df[column].sum():.2f}} {{to_currency}}")
if __name__ == "__main__":
main()