Users who frequently use Excel know the existence of merged cells. This article mainly introduces you to the relevant information on how to use Python to batch merge Excel files with merged cells. The article introduces it in detail through example code. , it has certain reference and learning value for everyone’s study or work. Friends who need it can come and take a look below.
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’s batch merging of Excel files with merged cells. It’s a bit 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:
Running result:
The above is the detailed content of Detailed explanation of Python batch merging Excel files with merged cells_python. For more information, please follow other related articles on the PHP Chinese website!