If you’ve ever tried to export WooCommerce orders with their line items into a flat CSV, you know the pain: WooCommerce’s REST API nests line items inside each order object. One order might have five products, but you need each product on its own row, with the order number, customer, and totals repeated. Doing this manually is error-prone. Python can automate the entire job in under 50 lines of code.
This guide walks you through the complete process — from creating API credentials to saving a clean, Excel-friendly CSV with one row per purchased item.
Prerequisites
Before you start, make sure you have:
- A WooCommerce store (version 3.5 or later recommended).
- Python 3.7+ installed on your machine.
- The
requestslibrary installed (pip install requests).

You’ll also need REST API credentials from your WooCommerce store. Here’s how to get them:
- Go to WooCommerce > Settings > Advanced > REST API.
- Click Add Key.
- Give it a description like “CSV Export Script”.
- Choose Read permissions (you only need read access for exporting).
- Click Generate API Key.
- Copy the Consumer Key and Consumer Secret.
Store these securely — they grant access to your store data.
Step 1: Set Up Authentication and Fetch Orders
WooCommerce uses OAuth 1.0a for REST API authentication. The woocommerce Python package handles the signing, but you can also use raw requests with oauthlib. For simplicity, we’ll use the woocommerce package:

Now, create a Python script (e.g., export_orders.py) and initialize the API client:
Why v3? WooCommerce v3 API is the most stable and supports all features. It’s widely used as of 2026.
Step 2: Download Orders with Nested Line Items
The API returns orders with a line_items array. Each item contains name, quantity, price, total, tax, and sku. But the API limits results to 100 orders per page (default). We need pagination.

Let’s fetch the first page:
Check if the request succeeded:
Step 3: Expand Line Items into Separate Rows
For each order, iterate over its line items and create one row per item, repeating the order-level fields (order ID, customer name, etc.).

We’ll build a list of dictionaries, then use csv.DictWriter to write the file.
Why utf-8-sig? Excel on Windows expects a BOM to correctly interpret UTF‑8. Without it, special characters (e.g., é, ü) may appear garbled.
Step 4: Handle Pagination for Large Stores
If you have more than 100 orders, you need to loop through pages. The API response headers include X-WP-TotalPages.
Now process all_orders as above.
Step 5: Filter by Order Status or Date Range
To export only completed orders, pass a status parameter:
For a date range (e.g., last 30 days), use after and before:
Combine both if needed. The API uses ISO 8601 format.
Pro Tips / Common Mistakes
- JSONDecodeError: If the API returns an HTML page (e.g., a 404 or maintenance page), the JSON parser fails. Always check
response.status_codefirst. - Rate limiting: WooCommerce REST API has no hard limit, but aggressive requests can slow your store. Add
time.sleep(0.5)between pages if you hit issues. - Memory: For stores with 10,000+ orders, consider writing rows incrementally instead of building a huge list in memory.
- Skip empty SKUs: Some products (e.g., variations) may have no SKU. Use
item.get('sku', '')to avoid KeyError.
FAQ
Can I export orders from a specific date range?
Yes. Use the after and before parameters with ISO 8601 timestamps (e.g., after=2024-01-01T00:00:00).
Why does my CSV show garbled characters in Excel?
Excel expects a UTF-8 BOM. Use encoding='utf-8-sig' when opening the file.
How do I handle pagination for stores with many orders?
Check the X-WP-TotalPages header and loop through all pages, concatenating the results.
Can I export only certain order statuses?
Yes. Pass a status parameter (e.g., status=completed) in the request.
What if I get a 401 or 403 error?
Your API credentials may be invalid or have wrong permissions. Regenerate them with at least Read access.
Conclusion
With this Python script, you can export WooCommerce orders and line items to a clean CSV — each product on its own row with the order number, customer, SKU, quantity, and total. The script handles pagination, status and date filters, and saves an Excel-friendly UTF-8 CSV.
Copy the code, update your store URL and API keys, and run it. You’ll have a fully flattened dataset ready for analysis or accounting.









