Skip ke Konten
Odoo Menu
  • Login
  • Uji coba gratis
  • Aplikasi
    Keuangan
    • Akuntansi
    • Faktur
    • Pengeluaran
    • Spreadsheet (BI)
    • Dokumen
    • Tanda Tangan
    Sales
    • CRM
    • Sales
    • POS Toko
    • POS Restoran
    • Langganan
    • Rental
    Website
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventaris
    • Manufaktur
    • PLM
    • Purchase
    • Maintenance
    • Kualitas
    Sumber Daya Manusia
    • Karyawan
    • Rekrutmen
    • Cuti
    • Appraisal
    • Referensi
    • Armada
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Acara
    • Otomatisasi Marketing
    • Survei
    Layanan
    • Project
    • Timesheet
    • Layanan Lapangan
    • Meja Bantuan
    • Planning
    • Appointment
    Produktivitas
    • Discuss
    • Kecerdasan Buatan
    • IoT
    • VoIP
    • Pengetahuan
    • WhatsApp
    Aplikasi pihak ketiga Odoo Studio Platform Odoo Cloud
  • Industri
    Retail
    • Toko Buku
    • Toko Baju
    • Toko Furnitur
    • Toko Kelontong
    • Toko Hardware
    • Toko Mainan
    Makanan & Hospitality
    • Bar dan Pub
    • Restoran
    • Fast Food
    • Rumah Tamu
    • Distributor Minuman
    • Hotel
    Real Estate
    • Agensi Real Estate
    • Firma Arsitektur
    • Konstruksi
    • Manajemen Properti
    • Perkebunan
    • Asosiasi Pemilik Properti
    Konsultansi
    • Firma Akuntansi
    • Mitra Odoo
    • Agensi Marketing
    • Firma huku
    • Talent Acquisition
    • Audit & Sertifikasi
    Manufaktur
    • Tekstil
    • Logam
    • Perabotan
    • Makanan
    • Brewery
    • Corporate Gift
    Kesehatan & Fitness
    • Sports Club
    • Toko Kacamata
    • Fitness Center
    • Wellness Practitioners
    • Farmasi
    • Salon Rambut
    Perdagangan
    • Handyman
    • IT Hardware & Support
    • Sistem-Sistem Energi Surya
    • Pembuat Sepatu
    • Cleaning Service
    • Layanan HVAC
    Lainnya
    • Organisasi Nirlaba
    • Agen Lingkungan
    • Rental Billboard
    • Fotografi
    • Penyewaan Sepeda
    • Reseller Software
    Browse semua Industri
  • Komunitas
    Belajar
    • Tutorial-tutorial
    • Dokumentasi
    • Sertifikasi
    • Pelatihan
    • Blog
    • Podcast
    Empower Education
    • Program Edukasi
    • Game Bisnis 'Scale Up!'
    • Kunjungi Odoo
    Dapatkan Softwarenya
    • Download
    • Bandingkan Edisi
    • Daftar Rilis
    Kolaborasi
    • Github
    • Forum
    • Acara
    • Terjemahan
    • Menjadi Partner
    • Layanan untuk Partner
    • Daftarkan perusahaan Akuntansi Anda.
    Dapatkan Layanan
    • Temukan Mitra
    • Temukan Akuntan
    • Konsultasi
    • Layanan Implementasi
    • Referensi Pelanggan
    • Bantuan
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dapatkan demo
  • Harga
  • Bantuan
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Help

How to download the Pivot table Excel using python code?

Langganan

Dapatkan notifikasi saat terdapat aktivitas pada post ini

Pertanyaan ini telah diberikan tanda
3 Replies
5804 Tampilan
Avatar
Tintuk Tomin

We know that in every pivot table we have the option to download the excel file of that pivot table in a single click using the download button.
Now what i need to do is to get this report and attach this report with the mail and send it to the employee every day.

I found the way to attach the document to the mail and send it automatically. But am unable to find a way to download the excel from the pivote table through code. 

Is anyone having the solution please help me.

0
Avatar
Buang
Avatar
Mohammed.Samy
Jawaban Terbai

Hi there,

You have hit a very common roadblock in Odoo development. The reason you cannot simply call a Python method to "download the pivot table" is because the pivot table export is generated client-side by JavaScript, not server-side by Python.

When a user clicks that download button, Odoo's frontend JavaScript reads the current matrix on their screen and sends a formatted dictionary to a web controller (usually /web/pivot/export_xls), which then returns the file. Because your automated action (cron job) runs entirely in the backend without a browser interface, it cannot trigger that JavaScript process.

To achieve this automatically via code, you have to recreate the data pull and the Excel file generation in Python. Here is the standard architectural approach to do this using Odoo's built-in xlsxwriter library:

1. The Python Logic

You will need to write a Python method (which your scheduled action will call) that does three things:

  1. Queries the data using read_group() (this is the same ORM method the pivot view uses).

  2. Writes that data into a virtual Excel file using xlsxwriter and io.BytesIO.

  3. Encodes the file to Base64 and creates an ir.attachment.

2. Example Code Template

Here is a boilerplate template using sale.order as an example. You will need to adapt the read_group parameters and the worksheet loops to match your specific model and columns.

Python

import io
import base64
import xlsxwriter
from odoo import models, fields, api

class DailyPivotReport(models.Model):
    _name = 'daily.pivot.report'
    _description = 'Automated Pivot Excel Generator'

    @api.model
    def generate_and_send_pivot_excel(self):
        # 1. Fetch the aggregated data (simulate the pivot backend)
        domain = [('state', 'in', ['sale', 'done'])]
        fields_to_read = ['amount_total']
        groupby = ['user_id']
        
        # read_group returns a list of dictionaries with your grouped data
        pivot_data = self.env['sale.order'].read_group(domain, fields_to_read, groupby)

        # 2. Create the Excel file in memory
        output = io.BytesIO()
        workbook = xlsxwriter.Workbook(output, {'in_memory': True})
        worksheet = workbook.add_worksheet('Daily Report')

        # Formatting (Optional)
        bold = workbook.add_format({'bold': True})

        # Write Headers
        worksheet.write(0, 0, 'Salesperson', bold)
        worksheet.write(0, 1, 'Total Sales', bold)

        # Write Data
        row = 1
        for record in pivot_data:
            # Handle cases where user_id might be False
            user_name = record['user_id'][1] if record.get('user_id') else 'Undefined'
            total = record.get('amount_total', 0.0)
            
            worksheet.write(row, 0, user_name)
            worksheet.write(row, 1, total)
            row += 1

        # Close and prepare the file for attachment
        workbook.close()
        output.seek(0)
        excel_data = output.read()
        excel_base64 = base64.b64encode(excel_data)

        # 3. Create the Attachment
        attachment = self.env['ir.attachment'].create({
            'name': 'Daily_Sales_Pivot.xlsx',
            'type': 'binary',
            'datas': excel_base64,
            'mimetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        })

        # 4. Attach to your email template and send
        template = self.env.ref('your_module.your_mail_template_id')
        
        # Temporarily link attachment to template, send, then clean up
        template.attachment_ids = [(4, attachment.id)]
        template.send_mail(self.id, force_send=True)
        template.attachment_ids = [(3, attachment.id)] 

Summary of Next Steps

Since you already have the email automation figured out, you just need to drop this logic into your cron job's target method. Adjust the domain, fields_to_read, and groupby variables to match the exact filters and groupings you have set up in your frontend pivot view.

It takes a bit of manual formatting to get the Python-generated Excel file to look exactly like the Odoo UI version, but this io.BytesIO method is the cleanest, most standard way to generate backend .xlsx files in Odoo without relying on third-party apps.

0
Avatar
Buang
Avatar
Yeison X
Jawaban Terbai

Hello! You've done it? I also want to automatically send a pivot report.

0
Avatar
Buang
Tintuk Tomin
Penulis

No... Not yet

Jothimani Rajagopal

Send Pivot reports automatically by minutes, hours, days, weeks and months from Odoo.

https://apps.odoo.com/apps/modules/13.0/pivot_scheduler

Avatar
Jothimani Rajagopal
Jawaban Terbai

Hi Tintuk Tomin,

Please review this module https://apps.odoo.com/apps/modules/13.0/pivot_scheduler, I hope it would help you.

0
Avatar
Buang
Menikmati diskusi? Jangan hanya membaca, ikuti!

Buat akun sekarang untuk menikmati fitur eksklufi dan agar terlibat dengan komunitas kami!

Daftar
Komunitas
  • Tutorial-tutorial
  • Dokumentasi
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Terjemahan
Layanan
  • Odoo.sh Hosting
  • Bantuan
  • Peningkatan
  • Custom Development
  • Pendidikan
  • Temukan Akuntan
  • Temukan Mitra
  • Menjadi Partner
Tentang Kami
  • Perusahaan kami
  • Aset Merek
  • Hubungi kami
  • Karir
  • Acara
  • Podcast
  • Blog
  • Pelanggan
  • Hukum • Privasi
  • Keamanan
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk Slovenščina Español (América Latina) Español Svenska ภาษาไทย Türkçe українська Tiếng Việt

Odoo adalah software terintegrasi dengan 70+ aplikasi seperti CRM, Akuntansi, Inventaris, Sales, eCommerce, Marketing, POS; plus fitur lokal Indonesia!

Mudah digunakan dan terintegrasi penuh pada saat yang sama adalah value proposition unik Odoo.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now