Bỏ qua để đến Nội dung
Odoo Menu
  • Đăng nhập
  • Dùng thử miễn phí
  • Ứng dụng
    Tài chính
    • Kế toán
    • Hóa đơn
    • Chi phí
    • Bảng tính (BI)
    • Tài liệu
    • Ký tên
    Bán hàng
    • CRM
    • Bán hàng
    • POS Cửa hàng
    • POS Nhà hàng
    • Đăng ký
    • Cho thuê
    Trang web
    • Trình tạo trang web
    • Thương mại điện tử
    • Blog
    • Diễn đàn
    • Trò chuyện trực tiếp
    • Học trực tuyến
    Chuỗi cung ứng
    • Tồn kho
    • Sản xuất
    • PLM
    • Mua hàng
    • Bảo dưỡng
    • Chất lượng
    Nhân sự
    • Nhân viên
    • Tuyển dụng
    • Ngày nghỉ
    • Đánh giá
    • Giới thiệu
    • Đội xe
    Marketing
    • Marketing trên MXH
    • Marketing qua email
    • Marketing qua SMS
    • Sự kiện
    • Tự động hóa Marketing
    • Khảo sát
    Dịch vụ
    • Dự án
    • Bảng chấm công
    • Dịch vụ hiện trường
    • Hỗ trợ
    • Kế hoạch
    • Lịch hẹn
    Năng suất
    • Thảo luận
    • Trí tuệ nhân tạo (AI)
    • IoT
    • VoIP
    • Kiến thức
    • WhatsApp
    Ứng dụng của bên thứ ba Studio Odoo Nền tảng Đám mây Odoo
  • Ngành
    Bán lẻ
    • Nhà sách
    • Cửa hàng quần áo
    • Cửa hàng nội thất
    • Cửa hàng tạp hóa
    • Cửa hàng đồ kim khí
    • Cửa hàng đồ chơi
    Ẩm thực & Dịch vụ lưu trú
    • Bar và quán rượu
    • Nhà hàng
    • Đồ ăn nhanh
    • Guest house
    • Nhà phân phối đồ uống
    • Khách sạn
    Bất động sản
    • Công ty môi giới bất động sản
    • Công ty kiến trúc
    • Xây dựng
    • Quản lý bất động sản
    • Làm vườn
    • Hiệp hội chủ sở hữu bất động sản
    Tư vấn
    • Công ty kế toán
    • Đối tác Odoo
    • Công ty marketing
    • Công ty luật
    • Tuyển dụng
    • Thanh tra & chứng nhận
    Sản xuất
    • Dệt may
    • Kim loại
    • Nội thất
    • Ẩm thực
    • Nhà máy bia
    • Quà tặng doanh nghiệp
    Sức khoẻ & fitness
    • CLB thể thao
    • Cửa hàng kính mắt
    • Trung tâm fitness
    • Chuyên gia chăm sóc sức khỏe
    • Hiệu thuốc
    • Tiệm làm tóc
    Thương mại
    • Dịch vụ sửa chữa
    • Phần cứng CNTT & Hỗ trợ
    • Hệ thống năng lượng mặt trời
    • Công ty sản xuất giày
    • Dịch vụ vệ sinh
    • Dịch vụ HVAC
    Khác
    • Tổ chức phi lợi nhuận
    • Cơ quan môi trường
    • Cho thuê biển quảng cáo
    • Nhiếp ảnh
    • Cho thuê xe đạp
    • Đại lý phần mềm
    Xem tất cả ngành
  • Cộng đồng
    Học tập
    • Khóa học
    • Tài liệu
    • Chứng chỉ
    • Đào tạo
    • Blog
    • Podcast
    Thúc đẩy đào tạo
    • Chương trình đào tạo
    • Trò chơi kinh doanh Scale Up!
    • Tham quan Odoo
    Nhận phần mềm
    • Tải xuống
    • So sánh các phiên bản
    • Phiên bản
    Hợp tác
    • Github
    • Diễn đàn
    • Sự kiện
    • Dịch thuật
    • Trở thành đối tác
    • Dịch vụ dành cho Đối tác
    • Đăng ký công ty kế toán của bạn
    Nhận dịch vụ
    • Tìm đối tác
    • Tìm kế toán
    • Gặp chuyên gia hỗ trợ
    • Dịch vụ Triển khai
    • Khách hàng tham khảo
    • Hỗ trợ
    • Nâng cấp
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Nhận một buổi demo
  • Bảng giá
  • Trợ giúp
Bạn cần phải đăng ký để tương tác với cộng đồng.
Tất cả bài viết Người Huy hiệu
Thẻ (Xem tất cả)
odoo accounting v14 pos v15
Về diễn đàn này
Bạn cần phải đăng ký để tương tác với cộng đồng.
Tất cả bài viết Người Huy hiệu
Thẻ (Xem tất cả)
odoo accounting v14 pos v15
Về diễn đàn này
Hỗ trợ

How to download the Pivot table Excel using python code?

Đăng ký nhận tin

Nhận thông báo khi có hoạt động trên bài viết này

Câu hỏi này đã bị gắn cờ
3 Trả lời
5789 Lượt xem
Ảnh đại diện
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
Ảnh đại diện
Huỷ bỏ
Ảnh đại diện
Mohammed.Samy
Câu trả lời hay nhất

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
Ảnh đại diện
Huỷ bỏ
Ảnh đại diện
Yeison X
Câu trả lời hay nhất

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

0
Ảnh đại diện
Huỷ bỏ
Tintuk Tomin
Tác giả

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

Ảnh đại diện
Jothimani Rajagopal
Câu trả lời hay nhất

Hi Tintuk Tomin,

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

0
Ảnh đại diện
Huỷ bỏ
Bạn có hứng thú với cuộc thảo luận không? Đừng chỉ đọc, hãy tham gia nhé!

Tạo tài khoản ngay hôm nay để tận hưởng các tính năng độc đáo và tham gia cộng đồng tuyệt vời của chúng tôi!

Đăng ký
Cộng đồng
  • Khóa học
  • Tài liệu
  • Diễn đàn
Open source
  • Tải xuống
  • Github
  • Runbot
  • Dịch thuật
Dịch vụ
  • Lưu trữ Odoo.sh
  • Hỗ trợ
  • Nâng cấp
  • Phát triển tùy chỉnh
  • Đào tạo
  • Tìm kế toán
  • Tìm đối tác
  • Trở thành đối tác
Giới thiệu công ty
  • Công ty của chúng tôi
  • Tài sản thương hiệu
  • Liên hệ
  • Việc làm
  • Sự kiện
  • Podcast
  • Blog
  • Khách hàng
  • Pháp lý • Riêng tư
  • Bảo mật
الْعَرَبيّة 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 là bộ ứng dụng kinh doanh có open source đáp ứng tất cả các nhu cầu của công ty bạn: CRM, thương mại điện tử, kế toán, tồn kho, POS, quản lý dự án, v.v.

Định hướng giá trị riêng biệt của Odoo là tích hợp hoàn toàn và dễ dàng sử dụng.

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