python 生成一个用来计算银行贷款分期还本的execl表格
时间: 2023-07-13 20:05:52 浏览: 141
python实现linux服务器批量修改密码并生成execl
可以使用Python的pandas和openpyxl库来生成Excel表格。以下是一个简单的示例代码,可以生成一个包含贷款金额、分期数、利率和每期应还本金和利息的表格:
```python
import pandas as pd
from openpyxl import Workbook
# 输入贷款信息
loan_amount = float(input("请输入贷款金额:"))
installments = int(input("请输入分期数:"))
interest_rate = float(input("请输入利率(百分比):")) / 100
# 计算每期应还本金和利息
monthly_interest_rate = interest_rate / 12
monthly_payment = loan_amount * monthly_interest_rate / (1 - (1 + monthly_interest_rate) ** -installments)
amortization_schedule = pd.DataFrame({'Period': range(1, installments+1),
'Balance': [loan_amount] * installments,
'Payment': [monthly_payment] * installments})
amortization_schedule['Interest'] = amortization_schedule['Balance'] * monthly_interest_rate
amortization_schedule['Principal'] = amortization_schedule['Payment'] - amortization_schedule['Interest']
amortization_schedule['Balance'] -= amortization_schedule['Principal']
# 创建Excel工作簿并写入数据
wb = Workbook()
ws = wb.active
ws.append(['Period', 'Balance', 'Payment', 'Interest', 'Principal'])
for row in amortization_schedule.itertuples():
ws.append([getattr(row, 'Period'), getattr(row, 'Balance'),
getattr(row, 'Payment'), getattr(row, 'Interest'),
getattr(row, 'Principal')])
# 保存Excel表格
filename = f"贷款分期还款表({loan_amount}元,{installments}期,{interest_rate*100}%利率).xlsx"
wb.save(filename)
print(f"已保存Excel表格:{filename}")
```
你可以根据需要修改输出格式、添加更多贷款信息等。
阅读全文