在好例子网,分享、交流、成长!
您当前所在位置:首页Python 开发实例Python语言基础 → ExceltoWord

ExceltoWord

Python语言基础

下载此实例
  • 开发语言:Python
  • 实例大小:5.94KB
  • 下载次数:26
  • 浏览次数:366
  • 发布时间:2015-11-01
  • 实例类别:Python语言基础
  • 发 布 人:酸性高锰酸钾
  • 文件格式:.py
  • 所需积分:1
 相关标签: Word Excel c d

实例介绍

【实例简介】
【实例截图】

【核心代码】

#! /usr/bin/env python
# -*- coding:utf-8 -*-

import xlrd
from docx import Document
from docx.enum.table import WD_TABLE_ALIGNMENT
import MySQLdb

def open_excel(filename='stu_info.xlsx'):
    try:
        data = xlrd.open_workbook(filename)
        table = data.sheets()[0]
        return table
    except Exception, e:
        print str(e)
        return None


def get_data_by_coord(table, row=0, col=0):
    try:
        data = table.row_values(row)[col]
        return data
    except Exception, e:
        print(str(e))
        return None


def get_student_info(table, row):
    try:
        student = table.row_values(row)
        return student
    except Exception, e:
        print str(e)
        return None


def strip(string):
    start = 0
    end = 0
    flag = True
    pos = 0
    for c in string:
        if c == u';' or c == u';':
            if flag:
                start = pos   1
                flag = False
            else:
                end = pos
        pos  = 1
    return string[start:end] if end - start <= 8 else string[start:start 8]


def get_position(grade, category):
    row = 2
    if grade in (5, 7):
        row = 21
    return row   (int(category) - 1) * 4


def connect_to_db():
    conn = MySQLdb.connect(
        host='localhost',
        port=3306,
        user='root',
        passwd='',
        db='stu'
    )
    return conn


def get_stu_info(stu_id, cur):
    sql = "SELECT * FROM stu_info WHERE stu_id='%s'" % stu_id
    cur.execute(sql)
    stu_info = cur.fetchone()
    return stu_info


def get_stu_data_by_semester(stu_id, semester, cur):
    sql = "SELECT * FROM semester%d WHERE stu_id = '%s'" % (semester, stu_id)
    amount = cur.execute(sql)
    data = cur.fetchmany(amount)
    return data


def close_db(cur, conn):
    cur.close()
    conn.commit()
    conn.close()


if __name__ == '__main__':
    table = open_excel()
    info_nrows = table.nrows
    records_tables = []
    records_nrows = []
    records_cursors = []
    for i in xrange(1, 7):
        print 'Reading records from %d.xlsx...' % i
        records_tables.append(open_excel('records/%d.xlsx' % i))
        records_nrows.append(records_tables[i-1].nrows)
        records_cursors.append(1)
        print 'Done.%d records were read.' % records_nrows[i-1]
    # handle a student's information each time
    print 'Ready to generate %d records.' % info_nrows
    for i in xrange(1, info_nrows):
        print '%d(%d) completed.' % (i, info_nrows)
        stu = get_student_info(table, i)
        document = Document('table.docx')
        doc_table = document.tables[0]
        # fill in the student's information
        doc_table.rows[1].cells[6].text = stu[1]
        doc_table.rows[2].cells[6].text = stu[2]
        doc_table.rows[3].cells[6].text = stu[3]
        doc_table.rows[4].cells[6].text = stu[4]
        doc_table.rows[5].cells[6].text = stu[8]
        doc_table.rows[6].cells[6].text = stu[6]
        doc_table.rows[7].cells[6].text = '20160625'
        doc_table.rows[9].cells[6].text = stu[7]
        # done

        index = 0
        stu_records = []
        credit_sum = 0
        ideological = 0
        profession = 0
        cultural = 0
        mind_body = 0
        required = 0
        electives = 0
        for records in records_tables:
            record = records.row_values(records_cursors[index])
            while record[2] == stu[2]:
                stu_records.append(record)
                records_cursors[index]  = 1
                record = records.row_values(records_cursors[index])
            if index % 2:
                rows = [0, 0, 0, 0]
                if index in (1, 5):
                    col = 10
                else:
                    col = 17
                for r in stu_records:
                    credit = float(r[8] if r[8] != '' else 0.5)
                    credit_sum  = credit
                    if r[4][0:2] == '01':
                        ideological  = credit
                    elif r[4][0:2] == '02':
                        profession  = credit
                    elif r[4][0:2] == '03':
                        cultural  = credit
                    else:
                        mind_body  = credit
                    if r[7] == '必修':
                        required  = credit
                    else:
                        electives  = credit

                    if r[21] == u'√':
                        row = get_position(index, r[4][0:2])
                        i = int(r[4][0:2]) - 1
                        doc_table.rows[row rows[i]].cells[col].text = r[5][2:]
                        doc_table.rows[row rows[i]].cells[col 1].text = r[6][3:]
                        doc_table.rows[row rows[i]].cells[col 2].text = strip(r[10])
                        doc_table.rows[row rows[i]].cells[col 3].text = str(credit)
                        doc_table.rows[row rows[i]].cells[col 4].text = str(r[7])
                        rows[0]  = 1


                doc_table.rows[16 index].cells[1].text = str(ideological)
                doc_table.rows[16 index].cells[2].text = str(profession)
                doc_table.rows[16 index].cells[4].text = str(cultural)
                doc_table.rows[16 index].cells[5].text = str(mind_body)
                doc_table.rows[16 index].cells[7].text = str(credit_sum)

                doc_table.rows[28 index].cells[2].text = str(required)
                doc_table.rows[28 index].cells[5].text = str(electives)
                doc_table.rows[28 index].cells[7].text = str(credit_sum)
                stu_records = []
                credit_sum = 0
                ideological = 0
                profession = 0
                cultural = 0
                mind_body = 0
                required = 0
                electives = 0

            index  = 1



        document.save('output/%s %s.docx' % (str(stu[2]), stu[3]))






标签: Word Excel c d

实例下载地址

ExceltoWord

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警