Skip to Content
Odoo Menu
  • Log ind
  • Prøv gratis
  • Apps
    Økonomi
    • Bogføring
    • Fakturering
    • Udgifter
    • Regneark (BI)
    • Dokumenter
    • e-Signatur
    Salg
    • CRM
    • Salg
    • POS Butik
    • POS Restaurant
    • Abonnementer
    • Udlejning
    Hjemmeside
    • Hjemmesidebygger
    • e-Handel
    • Blog
    • Forum
    • LiveChat
    • e-Læring
    Forsyningskæde
    • Lagerbeholdning
    • Produktion
    • PLM
    • Indkøb
    • Vedligeholdelse
    • Kvalitet
    HR
    • Medarbejdere
    • Rekruttering
    • Fravær
    • Medarbejdersamtaler
    • Anbefalinger
    • Flåde
    Marketing
    • Markedsføring på sociale medier
    • E-mailmarketing
    • SMS-marketing
    • Arrangementer
    • Automatiseret marketing
    • Spørgeundersøgelser
    Tjenester
    • Projekt
    • Timesedler
    • Udkørende Service
    • Kundeservice
    • Planlægning
    • Aftaler
    Produktivitet
    • Dialog
    • Kunstig intelligens
    • IoT
    • VoIP
    • Vidensdeling
    • WhatsApp
    Tredjepartsapps Odoo Studio Odoo Cloud-platform
  • Brancher
    Detailhandel
    • Boghandel
    • Tøjforretning
    • Møbelforretning
    • Dagligvarebutik
    • Byggemarked
    • Legetøjsforretning
    Mad og værtsskab
    • Bar og pub
    • Restaurant
    • Fastfood
    • Gæstehus
    • Drikkevareforhandler
    • Hotel
    Ejendom
    • Ejendomsmægler
    • Arkitektfirma
    • Byggeri
    • Ejendomsadministration
    • Havearbejde
    • Boligejerforening
    Rådgivning
    • Regnskabsfirma
    • Odoo-partner
    • Marketingbureau
    • Advokatfirma
    • Rekruttering
    • Audit & certificering
    Produktion
    • Tekstil
    • Metal
    • Møbler
    • Fødevareproduktion
    • Bryggeri
    • Firmagave
    Heldbred & Fitness
    • Sportsklub
    • Optiker
    • Fitnesscenter
    • Kosmetolog
    • Apotek
    • Frisør
    Håndværk
    • Handyman
    • IT-hardware og support
    • Solenergisystemer
    • Skomager
    • Rengøringsservicer
    • VVS- og ventilationsservice
    Andet
    • Nonprofitorganisation
    • Miljøagentur
    • Udlejning af billboards
    • Fotografi
    • Cykeludlejning
    • Softwareforhandler
    Gennemse alle brancher
  • Community
    Få mere at vide
    • Tutorials
    • Dokumentation
    • Certificeringer
    • Oplæring
    • Blog
    • Podcast
    Bliv klogere
    • Udannelselsesprogram
    • Scale Up!-forretningsspillet
    • Besøg Odoo
    Få softwaren
    • Download
    • Sammenlign versioner
    • Udgaver
    Samarbejde
    • Github
    • Forum
    • Arrangementer
    • Oversættelser
    • Bliv partner
    • Tjenester til partnere
    • Registrér dit regnskabsfirma
    Modtag tjenester
    • Find en partner
    • Find en bogholder
    • Kontakt en rådgiver
    • Implementeringstjenester
    • Kundereferencer
    • Support
    • Opgraderinger
    Github Youtube Twitter LinkedIn Instagram Facebook Spotify
    +1 (650) 691-3277
    Få en demo
  • Prissætning
  • Hjælp
Du skal være registreret for at interagere med fællesskabet.
Alle bidrag Personer Emblemer
Tags (Vis alle)
odoo accounting v14 pos v15
Om dette forum
Du skal være registreret for at interagere med fællesskabet.
Alle bidrag Personer Emblemer
Tags (Vis alle)
odoo accounting v14 pos v15
Om dette forum
Hjælp

How to download the Pivot table Excel using python code?

Tilmeld

Få besked, når der er aktivitet på dette indlæg

Dette spørgsmål er blevet anmeldt
3 Besvarelser
5852 Visninger
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
Kassér
Avatar
Mohammed.Samy
Bedste svar

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
Kassér
Avatar
Yeison X
Bedste svar

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

0
Avatar
Kassér
Tintuk Tomin
Forfatter

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
Bedste svar

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
Kassér
Kan du lide at diskutere? Læs ikke bare med, deltag aktivt i debatten!

Opret en konto i dag for at få glæde af eksklusive funktioner, og bliv en del af vores skønne fællesskab!

Tilmeld dig
Community
  • Tutorials
  • Dokumentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Oversættelser
Tjenester
  • Odoo.sh-hosting
  • Support
  • Opgradere
  • Individuelt tilpasset udvikling
  • Uddannelse
  • Find en bogholder
  • Find en partner
  • Bliv partner
Om os
  • Vores virksomhed
  • Brandaktiver
  • Kontakt os
  • Stillinger
  • Arrangementer
  • Podcast
  • Blog
  • Kunder
  • Juridiske dokumenter • Privatlivspolitik
  • Sikkerhedspolitik
الْعَرَبيّة 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 er en samling open source-forretningsapps, der dækker alle dine virksomhedsbehov – lige fra CRM, e-handel og bogføring til lagerstyring, POS, projektledelse og meget mere.

Det unikke ved Odoo er, at systemet både er brugervenligt og fuldt integreret.

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