Python กับการ Export xls

Microsoft Office Knowledge Word, Excel, powerpoint, line , โปรแกรมเสริมต่างๆ

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41336
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Python กับการ Export xls

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

Python กับการ Export xls
ต้อง
import xlwt

ตัวอย่างโค้ด

โค้ด: เลือกทั้งหมด

 workbook = xlwt.Workbook()
        M_header_tstyle = format_common.font_style(position='center', bold=1, border=1, fontos='black', font_height=700)
        header_tstyle = format_common.font_style(position='left', bold=1, border=1, fontos='black', font_height=180, color='grey')
        header_tstyle_c = format_common.font_style(position='center', bold=1, border=1, fontos='black', font_height=180, color='grey')
        header_tstyle_r = format_common.font_style(position='right', bold=1, border=1, fontos='black', font_height=180, color='grey')
        view_tstyle = format_common.font_style(position='left', bold=1, fontos='black', font_height=180)
        view_tstyle_r = format_common.font_style(position='right', bold=1, fontos='black', font_height=180)
        other_tstyle = format_common.font_style(position='left', fontos='black', font_height=180)
        other_tstyle_c = format_common.font_style(position='center', fontos='black', font_height=180)
        other_tstyle_r = format_common.font_style(position='right', fontos='black', font_height=180)

        sheet = workbook.add_sheet('Trial Balance')
        sheet.row(0).height = 256 * 3
        sheet.write_merge(0, 0, 0, 4, 'Trial Balance', M_header_tstyle)

        sheet.write(3, 0, 'Chart of Account', header_tstyle_c)
        sheet.write(4, 0, tbal_obj._get_account(data) or '', other_tstyle_c)
        sheet.write(3, 1, 'Fiscal Year', header_tstyle_c)
        sheet.write(4, 1, tbal_obj._get_fiscalyear(data) or '' , other_tstyle_c)
        sheet.write(3, 2, 'Display Account', header_tstyle_c)
        sheet.write(4, 2, display_account , other_tstyle_c)
        sheet.write(3, 3, 'Filter By', header_tstyle_c)
        sheet.write(4, 3, filter_by_title, other_tstyle_c)
        sheet.write(3, 4, 'Target Moves', header_tstyle_c)
        sheet.write(4, 4, tbal_obj._get_target_move(data), other_tstyle_c)

        sheet.col(0).width = 256 * 30
        sheet.col(1).width = 256 * 50
        sheet.col(2).width = 256 * 30
        sheet.col(3).width = 256 * 30
        sheet.col(4).width = 256 * 30

        sheet.write(6, 0, 'Code', header_tstyle_c)
        sheet.write(6, 1, 'Account', header_tstyle_c)
        sheet.write(6, 2, 'Debit', header_tstyle_c)
        sheet.write(6, 3, 'Credit', header_tstyle_c)
        sheet.write(6, 4, 'Balance', header_tstyle_c)

        row = 7
        cell_count = 0
        for val in tbal_obj.lines(data['form'], ids=[data['form']['chart_account_id']]):
            for key in ['code', 'name', 'debit', 'credit', 'balance']:
                if val['type'] == 'view':
                    Style = view_tstyle
                    if key not in ('code','name'):
                        Style  = view_tstyle_r
                    sheet.write(row, cell_count, val[key], Style)
                else:
                    Style = other_tstyle
                    if key not in ('code','name'):
                        Style  = other_tstyle_r
                    sheet.write(row, cell_count, val[key], Style)
                cell_count += 1
            cell_count = 0
            row += 1

        stream = cStringIO.StringIO()
        workbook.save(stream)
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 49