基本信息
源码名称:ExceltoWord
源码大小:5.94KB
文件格式:.py
开发语言:Python
更新时间:2015-11-01
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
#! /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]))