You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Stripe.com下载上一年度的PDF发票?

批量下载Stripe过去一年发票的Python脚本

因为会计工作需要下载Stripe过去一年的所有发票,但官网没有直接的批量下载按钮,联系客服后得知只能通过API实现。试过第三方导出工具但无法正常使用,于是写了这个Python脚本解决批量下载问题:

前置准备

  • 安装Stripe官方Python SDK:
    pip install stripe
    
  • 登录Stripe后台,获取Secret API Key(注意妥善保管,不要泄露)
  • 确认时间范围:脚本默认获取当前日期往前推一年的所有发票,可自行调整时间参数

完整脚本

import stripe
import os
from datetime import datetime, timedelta

# 配置参数
STRIPE_SECRET_KEY = "your_stripe_secret_key_here"  # 替换为你的Stripe秘钥
SAVE_FOLDER = "stripe_invoices"  # 发票保存目录
TIME_RANGE_DAYS = 365  # 过去一年,可调整

# 初始化Stripe客户端
stripe.api_key = STRIPE_SECRET_KEY

# 创建保存目录
if not os.path.exists(SAVE_FOLDER):
    os.makedirs(SAVE_FOLDER)

# 计算起始时间戳(当前时间减指定天数)
start_date = datetime.now() - timedelta(days=TIME_RANGE_DAYS)
start_timestamp = int(start_date.timestamp())

# 分页获取所有符合条件的发票
has_more = True
starting_after = None

while has_more:
    invoices = stripe.Invoice.list(
        created={"gte": start_timestamp},
        limit=100,  # 每次请求最大数量
        starting_after=starting_after
    )
    
    # 下载每张发票的PDF
    for invoice in invoices.data:
        if invoice.status == "paid":  # 可根据需求过滤状态,比如draft/void等
            try:
                pdf_content = stripe.Invoice.retrieve(invoice.id).pdf()
                # 用发票编号作为文件名
                file_name = f"{invoice.number}.pdf"
                file_path = os.path.join(SAVE_FOLDER, file_name)
                
                with open(file_path, "wb") as f:
                    f.write(pdf_content)
                print(f"已下载: {file_name}")
            except Exception as e:
                print(f"下载失败 {invoice.id}: {str(e)}")
    
    has_more = invoices.has_more
    starting_after = invoices.data[-1].id if has_more else None

print("所有符合条件的发票下载完成!")

使用说明

  1. 替换脚本中的STRIPE_SECRET_KEY为你的Stripe秘钥
  2. 调整SAVE_FOLDER设置发票保存的本地路径
  3. 如需修改时间范围,调整TIME_RANGE_DAYS参数
  4. 运行脚本即可自动下载所有符合条件的发票PDF

内容的提问来源于stack exchange,提问作者Lucas03

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 10:41:33