powerdesign pdm文件转excel

来源:互联网 发布:淘宝美工的工作内容 编辑:程序博客网 时间:2024/06/05 02:35

先前改造过一个vb脚本转换pdm文件为excel文件的代码,但是那个只能一个一个pdm文件执行,效率太低,所以花了一天时间动手写了一个解析pdm文件转excel的小程序。

#!/usr/bin/python# -*- coding: utf-8 -*- #import xlwtimport osfrom PDMHandler import PDMHandlerdef getHeadStyle():  style = xlwt.XFStyle()  # 初始化样式  font = xlwt.Font()  # 为样式创建字体  font.name = 'Times New Roman'  font.bold = True  style.font = font  # 为样式设置字体  borders = getBoder()  style.borders = borders  pattern = getPattern()  style.pattern = pattern  return styledef getBodyStyle():  style = xlwt.XFStyle()  # 初始化样式  font = xlwt.Font()  # 为样式创建字体  font.name = 'Times New Roman'  style.font = font  # 为样式设置字体  borders = getBoder()  style.borders = borders  return styledef getBoder():  borders = xlwt.Borders()  borders.left = 1  borders.right = 1  borders.top = 1  borders.bottom = 1  borders.bottom_colour = 0x3A  return bordersdef getWrapStyle():  style = xlwt.easyxf('align: wrap on')  # 初始化样式  font = xlwt.Font()  # 为样式创建字体  font.name = 'Times New Roman'  style.font = font  # 为样式设置字体  borders = getBoder()  style.borders = borders  return styledef setCellStyle(book):  book.col(0).width = 4444  # 3333 = 1" (one inch)  book.col(1).width = 4444  # 3333 = 1" (one inch)  book.col(2).width = 4444  # 3333 = 1" (one inch)  book.col(3).width = 16665  # 3333 = 1" (one inch)def getPattern():  pattern = xlwt.Pattern()  # Create the Pattern  pattern.pattern = xlwt.Pattern.SOLID_PATTERN  pattern.pattern_fore_colour = 5  return patternif __name__ == '__main__' :  import sys  reload(sys)  sys.setdefaultencoding("utf-8")  filepath = "C:\\eclipse_workspace\\CORE_DPM\\"  pathDir = os.listdir(filepath)  file = xlwt.Workbook()  for allDir in pathDir:    child = os.path.join('%s%s' % (filepath, allDir))    #print child.decode('gbk')  # .decode('gbk')是解决中文显示乱码问题    if child.endswith(".pdm") :      sheetName = os.path.basename(child).replace(".pdm","").decode("gbk")      print "===================="+sheetName+"===================="      filename = child      rowIndex = 0      ph = PDMHandler.parse(filename)      bodyStyle = getBodyStyle()      headStyle = getHeadStyle()      wrapStyle = getWrapStyle()      for pkg in PDMHandler.getPkgNodes(ph):        try:            pkg_attrs = PDMHandler.getPkgAttrs(pkg)        except Exception,e:           continue        print "P:", pkg_attrs["Name"],pkg_attrs["Code"],pkg_attrs["Creator"]        table = file.add_sheet(sheetName, cell_overwrite_ok=True)        setCellStyle(table)        for tbl in PDMHandler.getTblNodesInPkg(pkg) :          tbl_attrs = PDMHandler.getTblAttrs(tbl)          #table.write(rowIndex, 0, u'表名', headStyle)          table.write(rowIndex, 0, tbl_attrs["Code"], headStyle)          table.write_merge(rowIndex, rowIndex,1,1+2, tbl_attrs["Name"], headStyle)          rowIndex = rowIndex + 1          table.write(rowIndex, 0, u"字段中文名", headStyle)          table.write(rowIndex, 1, u"字段名", headStyle)          table.write(rowIndex, 2, u"字段类型", headStyle)          table.write(rowIndex, 3, u"字段说明", headStyle)          print " T:", tbl_attrs["Name"],tbl_attrs["Code"],tbl_attrs["Creator"]          #print "  T-PATH:",PDMHandler.getNodePath(tbl)          for col in PDMHandler.getColNodesInTbl(tbl) :            col_attrs = PDMHandler.getColAttrs(col)            rowIndex = rowIndex + 1            table.write(rowIndex, 0, col_attrs["Name"], bodyStyle)            table.write(rowIndex, 1, col_attrs["Code"], bodyStyle)            table.write(rowIndex, 2, col_attrs["DataType"], bodyStyle)            table.write(rowIndex, 3, col_attrs["Comment"], wrapStyle)            #print "  C:", col_attrs["Name"],col_attrs["Code"],col_attrs["DataType"],col_attrs["Length"],col_attrs["Column.Mandatory"],col_attrs["Comment"]          rowIndex = rowIndex + 3      file.save(u"核心系统表.xls")

效果图如下:
这里写图片描述

原创粉丝点击