码迷,mamicode.com
首页 > 编程语言 > 详细

整合Excel两个表的数据 - 2 | xlrd | xlwt | Python

时间:2017-12-14 21:08:39      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:datetime   coding   str   val   save   write   class   内蒙古   print   

# coding=utf-8

import os
import datetime
import xlrd
from xlwt import Workbook
from xlrd import xldate_as_tuple

agent_num = {
    "14": 1,
    "13": 1,
    "12": 1,
    "11": 1,
    "10": 1,
    "9": 1,
    "8": 1,
    "7": 1,
    "6": 1,
    "4": 1,
    "3": 1,
    "0": 1
}


def creat_agent_sheets(book):
    agent_data = {
        "14": "厦门吉事多",
        "13": "中财支付",
        "12": "内蒙古懒人网络科技有限公司",
        "11": "快汇支付",
        "10": "创富通002",
        "9": "星译付信息科技南通有限公司",
        "8": "云付泰",
        "7": "莘丽支付",
        "6": "一键联支付",
        "4": "创富通",
        "3": "闪银掌柜",
        "0": "首瀚"
    }
    agent_sheets = {}
    for a in agent_data:
        agent_sheets[a] = book.add_sheet(agent_data[a])

    return agent_sheets


def create_book():
    w = Workbook()
    w = Workbook(encoding=utf-8)
    return w


def open_book(path):
    data = xlrd.open_workbook(path)
    return data


def save_book(book):
    book.save("11fee.xlsx")
    print "-----ok-----"


if __name__ == "__main__":
    # 创建空表格
    book1 = create_book()
    # 创建工作组
    agent_sheets_dict = creat_agent_sheets(book1)

    # 打开账单表格
    path_bill = "11m.xlsx"
    data_bill = open_book(path_bill)

    # 费用
    table_fee = data_bill.sheets()[1]
    nrows_fee = table_fee.nrows

    # 打开用户表格
    path_reg = "reg.xlsx"
    data_reg = open_book(path_reg)
    table_reg = data_reg.sheets()[0]
    reg_nrows = table_reg.nrows
    # 分类
    m = 0
    for d in xrange(nrows_fee):
        if d == 0:
            continue
        row_fee = table_fee.row_values(d)
        for r in xrange(reg_nrows):
            if r == 0:
                continue
            row_reg = table_reg.row_values(r)
            if str(row_reg[3]) == str(row_fee[8]):
                reg_n = str(int(row_reg[6] if row_reg[6] else 0))
                if reg_n:
                    w_sheet = agent_sheets_dict[reg_n]
                    No = agent_num[reg_n]
                    w_sheet.write(No, 0, agent_num[reg_n])
                    w_sheet.write(No, 1, str(datetime.datetime(*xldate_as_tuple((row_fee[2]), 0))))
                    w_sheet.write(No, 2, str(row_fee[3]))
                    w_sheet.write(No, 3, str(row_fee[4]))
                    w_sheet.write(No, 4, str(row_fee[5]))
                    w_sheet.write(No, 5, str(row_fee[6]))
                    w_sheet.write(No, 6, str(row_fee[8]))
                    agent_num[reg_n] += 1
    save_book(book1)

 

整合Excel两个表的数据 - 2 | xlrd | xlwt | Python

标签:datetime   coding   str   val   save   write   class   内蒙古   print   

原文地址:http://www.cnblogs.com/pymkl/p/8039497.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!