如何使用openpyxl读取合并单元格?

时间:2017-11-09 11:52:14

标签: python-2.7

from openpyxl import load_workbook,Workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
from openpyxl.utils import *
from copy import copy

fn = r'C:\Python27\ExcelT\PythonExcelRead.xlsx'
#fn= r'C:\Python27\ExcelT\PythonExcelRead.xlsm'
#fn= r'C:\Python27\ExcelT\PythonExcelRead.xls'

fnn = r'C:\Python27\ExcelT\PythonExcelWrite.xlsx'
#fnn= r'C:\Python27\ExcelT\PythonExcelWrite.xlsm'
#fnn= r'C:\Python27\ExcelT\PythonExcelWrite.xls'

#--------------Reading the excel file

#--------------if file is .xlsx
if(fn.split(".")[-1]=='xlsx'):
       wb =load_workbook(fn)

#--------------if file is .xlsm
elif(fn.split(".")[-1]=='xlsm'):
       wb = load_workbook(fn,keep_vba=True)


#--------------if file is .xls       
elif(fn.split(".")[-1]=='xls'):
       print("unsupported format")
ws = wb.active
nor1=ws.max_row+1
noc1=ws.max_column+1
r=1
for r in range(1,nor1):
       for cell in ws[r:r]:
               #for m in ws.merged_cells_ranges:
                   print("value:")
                   print(cell.value)
                   print("Font:")
                   print(cell.font.name)
                   print("Font_color:")
                   print(cell.font.color.index)
                   print("Cell_color:")
                   print(cell.fill.start_color.index)
                #   print("This is merge cell")

                   print("--------------------------")

'''def getValueWithMergeLookup(sheet, cell):
    idx = cell.coordinate
    for range_ in sheet.merged_cell_ranges:
        merged_cells = list(openpyxl.utils.rows_from_range(range_))
        for row in merged_cells:
            if idx in row:
                # If this is a merged cell,
                # return  the first cell of the merge range
                return sheet.cell(merged_cells[0][0]).value

    return sheet.cell(idx).value
getValueWithMergeLookup(ws, cell)'''

#---------------Detecting the merge cells

print("Cells given below are merge cells") 
print(ws.merged_cell_ranges)

#-------------Updating the excel file
ws['F2']='001CS'
ws['F3']='005ME'
ws['F2'].font=Font(name='impact',color='00ff0000')
ws['F3'].font=Font(name='impact',color='00ff0000')
ws['D2']=70
#-------------Reading the updated excel file

for r in range(1,nor1):
       for cell in ws[r:r]:
          print("value:")
          print(cell.value)
          print("Font:")
          print(cell.font.name)
          print("Font_color:")
          print(cell.font.color.index)
          print("Cell_color:")
          print(cell.fill.start_color.index)
          print("--------------------------")


print("Cells given below are merge cells") 
print(ws.merged_cell_ranges)

wb.save(fn)

#--------------Writing to an excel file

if(fnn.split(".")[-1]=='xlsx'):
       wb1=load_workbook(fnn)

elif(fnn.split(".")[-1]=='xlsm'):
       wb1 = load_workbook(fnn,keep_vba=True)
elif(fnn.split(".")[-1]=='xls'):
     print("unsupported format")

ws1=wb1.active
print("--------------------------")
nor2=ws.max_row+1
noc2=ws.max_column+1
r=1
for r in range(1,nor2):
       for c in range(1,noc2):
         ws1.cell(row=r,column=c).value=ws.cell(row=r,column=c).value
         ws1.cell(row=r,column=c).font=copy(ws.cell(row=r,column=c).font)
         ws1.cell(row=r,column=c).fill=copy(ws.cell(row=r,column=c).fill)
         ws1.merge_cells(start_row=8,start_column=2,end_row=8,end_column=4)
         ws1.merge_cells(start_row=9,start_column=2,end_row=9,end_column=4)
wb1.save(fnn)

我想阅读合并单元格,因为当我读取合并单元格时,说 A1:A3 我只在 A1 时得到值,而休息 A2,A3 节目'无'。如何正确读取合并单元格,因为我可以手动编写合并单元格,但无法读取它plz帮助我使用openpyxl libary解决我的问题,但我无法为此目的更改为另一个。

0 个答案:

没有答案