Quick script to download all invoices from my Stripe account in PDF format with uniform naming convention
import stripe
import requests
stripe.api_key = os.getenv("STRIPE_API_KEY")
invoices_folder = 'path/to/folder'
existing_invoices = os.listdir(invoices_folder)
existing_invoices_ids = []
for inv in existing_invoices:
if inv.endswith(".pdf"):
parts = inv.split('_')
id = parts[1]
existing_invoices_ids.append(id)
# all_invoices = stripe.Invoice.list(limit=3)
all_invoices = stripe.Invoice.list()
for invoice in all_invoices:
number = invoice.number
print(number)
if number not in existing_invoices_ids:
amount = round(float(invoice.amount_due/100),2)
amount_int = int(amount)
currency = invoice.currency.upper()
pdf = invoice.invoice_pdf
invoice_file = requests.get(pdf)
customer = invoice.customer_name
created = invoice.created
created_date = datetime.utcfromtimestamp(created).strftime('%Y-%m-%d')
filepath = f"{invoices_folder}/{created_date}_{number}_{customer}_{amount_int}_{currency}.pdf"
with open(filepath, 'wb') as f:
f.write(invoice_file.content)