Home  >  Article  >  Backend Development  >  How to batch merge excel tables in python

How to batch merge excel tables in python

php中世界最好的语言
php中世界最好的语言Original
2018-04-09 17:25:434696browse

This time I will show you how to batch merge exceltables in python. What are the precautions for batch merging excel tables in python? The following is a practical case, let’s take a look.

Merge cells

I believe everyone can merge cells. For example, the following simple code can achieve it:

app='Word' 
word=win32.gencache.EnsureDispatch('%s.Application' % app) 
doc=word.Documents.Add() 
word.Visible=False 
 
#Title begin  
sel =word.Selection 
sel.Font.Name = u"微软雅黑" 
sel.Font.Size = 8   
sel.Font.Bold = False  
sel.Font.Italic = False 
sel.Font.Underline = False 
sel.ParagraphFormat.Alignment = 1 
 
myRange = doc.Range(0,0) 
myRange.InsertBefore(u'标题1 测试表格') # 使用样式 
#Title end 
#Table Start 
sel.SetRange(10,10) 
tab = doc.Tables.Add(sel.Range, 9, 3) 
tab.Columns(1).SetWidth(10.35*20.35, 0) 
tab.Rows.Alignment = 1 
tab.Style = u"网格型" 
tabnow = doc.Tables(1) 
cell1 = tabnow.Cell(1,1) 
cell2 = tabnow.Cell(3,1) 
 
#myrange = doc.Range(cell1.Range.Start, cell2.Range.End) 
 
sel.SetRange(cell1.Range.Start, cell2.Range.End) 
sel.Cells.Merge()

It's very simple. This article introduces Python to batch merge Excel files with merged cells. It is somewhat difficult. Let's take a look.

Problem Description:

A teacher teaches "Python Programming" courses for different colleges, and selected Dong Fuguo according to the characteristics of different majors and courses One of the teacher series textbooks "Python Programming (2nd Edition)", "Python Programming Basics (2nd Edition)", "How to Learn Python" and "Python Programming Development Guide". At the end of the semester, fill in the Excel files of transcripts from each college. The formats of these Excel files are roughly the same, but there are slight differences. That is, some "College" columns have cells merged, while others do not.

Now I want to merge these Excel tables into one table, and merge the appropriate "College" columns as needed.

The transcripts of three of the colleges are as follows:

Reference code:

Run result:

I believe you have mastered it after reading the case in this article Method, for more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Operation excel to read and write data

##How to perform automated unit testing with Python Unittest

The above is the detailed content of How to batch merge excel tables in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn