用python实现自动填数生成表格v1.0
先簡(jiǎn)單描述一下需求,是這樣,有時(shí)候人事部門(mén)需要對(duì)著同樣的一張表錄入數(shù)據(jù),比如說(shuō)員工退休表,這樣的表通常是一張excel表格,由于退休員工有多個(gè),那么這樣的表肯定就要經(jīng)常錄入了。錄入之后由于excel是獨(dú)立分散的,并不便于后續(xù)的數(shù)據(jù)的查找、管理等。同時(shí),每個(gè)退休員工都要錄入一張表的話,有時(shí)候工作量很大,也容易出錯(cuò)。
這樣的業(yè)務(wù)需求其實(shí)是很多的,比如生成每個(gè)學(xué)生獨(dú)立的成績(jī)單等等。
那么,有沒(méi)有什么辦法比較好解決呢?當(dāng)然是有,而且思路是很簡(jiǎn)單的。首先先把所有待處理的數(shù)據(jù)填放在一個(gè)數(shù)據(jù)源的excel表格里面,再分別制作一個(gè)模板和一個(gè)填入位置的excel。然后用python去讀取數(shù)據(jù)源、模板和填入位置,然后復(fù)制模板并且按照填入位置用數(shù)據(jù)源表格里面的每一行表格逐一生成一個(gè)個(gè)的處理后的結(jié)果表即可了。
先上代碼:
from openpyxl import Workbook from openpyxl import load_workbook import os import datetime import shutildef now(year=True,month=True,day=True,hour=True,minute=True,second=True):def complete(x):return '0' + str(x) if x<10 else str(x)d = datetime.datetime.now()result = ''if year:result += str(d.year) + '-'if month:result += complete(d.month) + '-'if day:result += complete(d.day) + ' 'if hour:result += complete(d.hour) + '-'if minute:result += complete(d.minute) + '-'if second:result += complete(d.second) + '-'return result[:-1] path = os.getcwd()def run():print(now()+' 開(kāi)始任務(wù),請(qǐng)稍候...')if not os.path.exists(os.getcwd() + '\\' + '模板.xlsx'):print('丟失模板.xlsx,請(qǐng)?jiān)O(shè)置好文件再運(yùn)行...')returntemplate_name = os.getcwd() + '\\' + '模板.xlsx'if not os.path.exists(os.getcwd() + '\\' + '數(shù)據(jù)源.xlsx'):print('丟失模板.xlsx,請(qǐng)?jiān)O(shè)置好文件再運(yùn)行...')returndata_sheet = load_workbook(os.getcwd() + '\\' + '數(shù)據(jù)源.xlsx').activeif not os.path.exists(os.getcwd() + '\\' + '填入位置.xlsx'):print('丟失模板.xlsx,請(qǐng)?jiān)O(shè)置好文件再運(yùn)行...')returnlocation_sheet = load_workbook(os.getcwd() + '\\' + '填入位置.xlsx').activelc = []for column in range(1,location_sheet.max_column+1):lc.append(str(location_sheet.cell(2,column).value).upper())outputfolder = 'output ' + now()if not os.path.exists(os.getcwd() + '\\' + outputfolder):os.mkdir(os.getcwd() + '\\' + outputfolder)for row in range(2,data_sheet.max_row+1):targetname = os.getcwd() + '\\' + outputfolder + '\\' + str(data_sheet.cell(row,1).value) +'.xlsx'shutil.copy(template_name,targetname)target = load_workbook(targetname)target_sheet = target.activefor i in range(0,len(lc)):target_sheet[lc[i]] = data_sheet.cell(row,i+1).valuetarget.save(targetname)print(now()+' 任務(wù)完成...')# except:# print('您的模板、數(shù)據(jù)源、填入位置可能填寫(xiě)有誤,出現(xiàn)異常了,請(qǐng)好好檢查再重新運(yùn)行...')run() print('請(qǐng)輸入任意鍵退出...') input()demo文件在這里:https://download.csdn.net/download/sinolzeng/15432505
總結(jié)
以上是生活随笔為你收集整理的用python实现自动填数生成表格v1.0的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 用python结束exe进程
- 下一篇: websocket python爬虫_p