[發明專利]一種利用Python實現兩類多個Excel電子表格合并數據的方法在審
| 申請號: | 201710836075.7 | 申請日: | 2017-09-16 |
| 公開(公告)號: | CN107544814A | 公開(公告)日: | 2018-01-05 |
| 發明(設計)人: | 陳興雷;任金樂 | 申請(專利權)人: | 陳興雷;任金樂 |
| 主分類號: | G06F9/44 | 分類號: | G06F9/44 |
| 代理公司: | 暫無信息 | 代理人: | 暫無信息 |
| 地址: | 233040 安徽省蚌*** | 國省代碼: | 安徽;34 |
| 權利要求書: | 查看更多 | 說明書: | 查看更多 |
| 摘要: | |||
| 搜索關鍵詞: | 一種 利用 python 實現 兩類多個 excel 電子表格 合并 數據 方法 | ||
1.一種利用Python實現兩類多個Excel電子表格合并數據的方法,在Windows XP或Win7系統中安裝有Microsoft Excel2003或2007和Python2.7.2,并安裝有xlrd1.1.0、xlwt1.3.0和xlutils2.0.0站點包(3個站點包下載解壓后將其xlrd、xlwt和xlutils文件夾拷備至C:/Python27/Lib/site-packages目錄下即可)的前提下,具體步驟為:
(1) 將待合并的文件拷備至桌面目錄下;
(2) 運行程序/Python2.7/IDLE(Python GUI),通過Ctr+N或File/New Window新建一Untitled代碼窗口;
(3) 將Python程序代碼拷備至此窗口中,按F5保存文件到桌面目錄(與待合并文件同一目錄)并運行,Python程序如下:
#!/usr/bin/env python
#coding=GBK
import os,sys,time,tkFileDialog,shutil,xlrd,xlwt,xlutils.copy
def fill_in(files_i,filename):
master_open=xlrd.open_workbook(filename)
open_master=xlutils.copy.copy(master_open)
master_table0=master_open.sheet_by_index(0)
master_sheet0=open_master.get_sheet(0)
master_nrows=master_table0.nrows
master_ncols=master_table0.ncols
for L in files_i:
file_i_open=xlrd.open_workbook(L)
open_file_i=xlutils.copy.copy(file_i_open)
file_i_table0=file_i_open.sheet_by_index(0)
file_i_sheet0=open_file_i.get_sheet(0)
i=0
j=0
for i in range(0,master_nrows):
for j in range(0,master_ncols):
master_cell_ij=master_table0.cell(i,j).value
file_i_cell_ij=file_i_table0.cell(i,j).value
if master_cell_ij!=file_i_cell_ij:
master_sheet0.write(i,j,file_i_cell_ij)
else:
pass
open_master.save(os.getcwd()+'/匯總結果_填表式合并匯總.xls')
print "*************************************************"
print "***"+L.split('//')[-1]+">>>>完成匯總***"
print "***>>>請到當前目錄中查看匯總結果_填表式合并匯總.xls文件"
def fill_incells(files_i,filename):
master_open=xlrd.open_workbook(filename)
open_master=xlutils.copy.copy(master_open)
master_table0=master_open.sheet_by_index(0)
master_sheet0=open_master.get_sheet(0)
master_nrows=master_table0.nrows#行數
master_ncols=master_table0.ncols#列數
Nrow_Sum=0
print "/n/n請注意,需要輸入如下參量,輸入大了會丟數據,輸小了會出現原標題***"
flag=int(input("母文件標題行數有幾行,如果是3行,則輸入3:"))
for L in files_i:
file_i_open=xlrd.open_workbook(L)
open_file_i=xlutils.copy.copy(file_i_open)
file_i_table0=file_i_open.sheet_by_index(0)
file_i__nrows=file_i_table0.nrows#行數
file_i__ncols=file_i_table0.ncols#列數
file_i_sheet0=open_file_i.get_sheet(0)
i=0
j=0
Nrow_Sum=Nrow_Sum+flag
for i in range(flag,file_i__nrows):
for j in range(0,file_i__ncols):
cell_ij=file_i_table0.cell(i,j).value
master_sheet0.write(Nrow_Sum,j,cell_ij)
Nrow_Sum+=1
Nrow_Sum=Nrow_Sum-flag+1
print "*************************************************"
print "***"+L.split('//')[-1]+">>>>完成匯總***"
open_master.save(os.getcwd()+'/匯總結果_行增式合并匯總.xls')
print "***>>>請到當前目錄中查看匯總結果_行增式合并匯總.xls"
filename = tkFileDialog.askopenfilename(title=r"請選擇母文件",initialdir=(os.getcwd()))
filename=filename.encode("GBK")#碼轉換windows編碼為GBK
filenames = tkFileDialog.askopenfilenames(title=r"請選擇子文件",initialdir=(os.getcwd()),initialfile='')
filenames=filenames.encode("GBK")#碼轉換windows編碼為GBK
files_i=[]
filenames=filenames.split('} {')
for L in filenames:
L=L.strip('}')
L=L.strip('{')
files_i.append(L)
a=[]
a=filename.split('/')
filename=os.getcwd()+"/"+a[-1]
choice=input("/n***選擇合并文件的方式,填表式合并輸入1,還是行增式合并輸入0:")
if int(choice)==1:
#填表式合并主要工具
fill_in(files_i,filename)
time.sleep(3)
else:
if int(choice)==0:
#行式合并主要工具
fill_incells(files_i,filename)
time.sleep(3)
else:
print '輸入錯誤,請重新運行程序'
time.sleep(3)
time.sleep(1)
os.system("explorer.exe %s" % os.getcwd())
kill_command='taskkill /FI "WINDOWTITLE eq tk" /IM py* /T /F'
os.system(kill_command)
(4) 按提示對話框選擇合并文件的母文件與子文件(多個子文件應一次性框選或按Ctr點選);
(5) 按合并文件的類型選擇文件合并的方式,如果是填表式合并,輸入1,則直接輸出合并結果到當前目錄中,完成合并;如果是行增式合并,輸入0,再依提示輸入行標題的行數后,如輸入3,將同樣直接輸出合并結果到當前目錄,完成合并。
該專利技術資料僅供研究查看技術是否侵權等信息,商用須獲得專利權人授權。該專利全部權利屬于陳興雷;任金樂,未經陳興雷;任金樂許可,擅自商用是侵權行為。如果您想購買此專利、獲得商業授權和技術合作,請聯系【客服】
本文鏈接:http://www.szxzyx.cn/pat/books/201710836075.7/1.html,轉載請聲明來源鉆瓜專利網。
- 上一篇:一種靜態庫配置的切換方法和系統
- 下一篇:一種軟件包下發和加載方法及裝置





