zoukankan      html  css  js  c++  java
  • excel在不改变格式的情况下,对两个名字的某几个日期下的内容进行替换

     1 #-*- coding: UTF-8 -*-
     2 import xlrd
     3 import sys
     4 import locale
     5 import os
     6 import xlwt
     7 from xlutils.copy import copy
     8 
     9 reload(sys)
    10 sys.setdefaultencoding( "utf-8" )
    11 homedir=os.getcwd()
    12 print u"文件路径",homedir
    13 print u"""执行文件前首先阅读以下说明文档:
    14 1.首先请把要换班的excel文件转换为xls格式,即文件打开,点击文件选项,点击另存为并选择格式为xls
    15   (可以用xlsx尝试以下,有的时候会出错)
    16 2.确定文件sheet的名称为"班表",并且sheet位置排在首位
    17 3.把xls文件放在文件路径下面,即和程序放在一个目录里
    18 4.每次的修改完成的记录会在本目录下xiugai.txt文件里面
    19 """
    20 
    21 def get_col_number(excelname,sheetname,index,name):
    22     fileHandler = xlrd.open_workbook(excelname)
    23     page = fileHandler.sheet_by_name(sheetname)
    24     col1 = page.col_values(index)
    25     for item in col1:
    26         if item ==name:
    27             nrow=col1.index(item)
    28     return nrow
    29 def changge_worktable(excelname,sheetname,sheetnumber,number1,number2,date):
    30     fileHandler = xlrd.open_workbook(excelname)
    31     page = fileHandler.sheet_by_name(sheetname)
    32     cell_value1= page.cell(number1,date).value
    33     cell_value2= page.cell(number2,date).value
    34     newwb = xlrd.open_workbook(excelname, formatting_info=True)  # formatting_info 带格式导入
    35     outwb = copy(newwb)                           # 建立一个副本来用xlwt来写
    36     # 修改值
    37     def setOutCell(outSheet, col, row, value):
    38         def _getOutCell(outSheet, colIndex, rowIndex):
    39             row = outSheet._Worksheet__rows.get(rowIndex)
    40             if not row: return None
    41             cell = row._Row__cells.get(colIndex)
    42             return cell
    43         previousCell = _getOutCell(outSheet, col, row)
    44         outSheet.write(row, col, value)
    45         if previousCell:
    46             newCell = _getOutCell(outSheet, col, row)
    47             if newCell:
    48                 newCell.xf_idx = previousCell.xf_idx
    49     outSheet = outwb.get_sheet(sheetnumber)
    50     setOutCell(outSheet, date,number1,cell_value2)
    51     setOutCell(outSheet, date, number2,cell_value1)
    52     outwb.save(excelname)
    53 
    54 def main():
    55     excelname=raw_input(u"请输入excelname(带文件类型名):".encode('utf-8')).decode(sys.stdin.encoding or locale.getpreferredencoding(True))
    56     print excelname
    57     sheetname=u"班表"
    58     sheetnumber=0
    59     for i in range(0,500):
    60         try:
    61             name1=raw_input("name1:".encode('utf-8')).decode(sys.stdin.encoding or locale.getpreferredencoding(True))
    62             print name1
    63             name2=raw_input("name2:".encode('utf-8')).decode(sys.stdin.encoding or locale.getpreferredencoding(True))
    64             print name2
    65             datenumberlist=raw_input(u"请输入datenumber(多个的话用英文逗号隔开):").split(",")
    66             print datenumberlist
    67 
    68             number1=get_col_number(excelname,sheetname,1,name1)
    69             number2=get_col_number(excelname,sheetname,1,name2)
    70             for datenumber in datenumberlist:
    71 
    72                 changge_worktable(excelname,sheetname,sheetnumber,number1,number2,int(datenumber)+3)
    73             print "i:",i
    74             with open("xiugai.txt","a") as output:
    75                 output.write(name1+"***"+name2+"***"+str(datenumberlist)+"
    ")
    76             print "success"
    77         except:
    78             print "there is an error in your enter,please check and repeat the input again"
    79             continue
    80         esc=raw_input(u"想退出的话输入esc,继续的话按回车:".encode('utf-8')).decode(sys.stdin.encoding or locale.getpreferredencoding(True))
    81         if esc=="esc":
    82             break
    83     print "game is over"
    84 
    85 if __name__ == "__main__":
    86     main()

    修改的excel格式图如下
    
    
  • 相关阅读:
    使用命令行创建制作 MACOS HIGH SIERRA 正式版 USB 安装盘
    IONIC2/3解决文本框获取焦点的问题
    一步一步实现IONIC2/3 热更新
    IONIC打包安卓遇到COM.ANDROID.SUPPORT:SUPPORT-V4错误的解决办法
    js hoisting
    js立即执行函数
    关于js sort排序方法
    js基础--substr()和substring()的区别
    关于align-items和align-content的区别和使用场景
    关于ionic开发中遇到的坑与总结
  • 原文地址:https://www.cnblogs.com/oneby/p/5454378.html
Copyright © 2011-2022 走看看