Python 实现iOS国际化字符串合并Excel表格

  • Python 实现两张Excel合并、主要是用来合并多的部分,当有行、列添加就会合并到新到Excel中

我是要成为海贼王的男人!

《Python 实现iOS国际化字符串合并Excel表格》 042ec9e15de70b28500fa5b9e06883be.jpg

开发环境
系统: MacOS
开发语言: Python
开发工具: Pycharm

from openpyxl import Workbook
from openpyxl import load_workbook
import os

# row_keyList1 = []  #  行  用于判断行添加列什么元素
# column_keyList1 = []  # 列 用于判断列添加列什么元素
# value_dictionary1 = {}  # key+value: value
# row_keyList2 = []  #  行  用于判断行添加列什么元素
# column_keyList2 = []  # 列 用于判断列添加列什么元素
# value_dictionary2 = {}  # key+value: value

def __deal_excel(path):
    row_keyList = []  # 行  用于判断行添加列什么元素
    column_keyList = []  # 列 用于判断列添加列什么元素
    value_dictionary = {}  # key+value: value
    #  加载本地Excel

    wb = load_workbook(filename=path)
    #  加载本地Sheet
    ws = wb.get_sheet_by_name('1')
    for i in range(2, ws.max_column+1):
        colKey = ws.cell(row=1, column=i).value
        if colKey is not None:
            column_keyList.append(colKey)

    for i in range(2, ws.max_row+1):
        rowKey = ws.cell(row=i, column=1).value
        if rowKey is not None:
            row_keyList.append(rowKey)

    for i in range(len(row_keyList)):
        for j in range(len(column_keyList)):
            value = ws.cell(row=i+2, column=j+2).value
            colK = ws.cell(row=1, column=j+2).value
            if row_keyList[i] is not None and colK is not None:
                key = row_keyList[i] + colK
                value_dictionary.setdefault(key, value)
    return row_keyList, column_keyList, value_dictionary

#  用于比较行添加
def __compare_row(rowList1, rowList2):

    moreRowList = []
    for c1 in rowList1:
        if c1 in rowList2:
            continue
        else:
            moreRowList.append(c1)
    return moreRowList

#  用于比较列添加
def __compare_column(columList1, columList2):

    moreColList = []
    for c1 in columList1:
        if c1 in columList2:
            continue
        else:
            moreColList.append(c1)

    return moreColList



if __name__ == '__main__':

    path1 = '/Users/tusm/Desktop/douban.xlsx'  # 旧Excel
    path2 = '/Users/tusm/Desktop/dou.xlsx'  #  新Excel
    row_keyList1, column_keyList1, value_dictionary1 = __deal_excel(path1)
    row_keyList2, column_keyList2, value_dictionary2 = __deal_excel(path2)

    wb = load_workbook(filename=path2)
    ws = wb.get_sheet_by_name('1')

    #  处理列
    colList = __compare_column(column_keyList1, column_keyList2)
    if len(colList) <=0:
        print('没有添加新的列')
    else:
        print('添加了新列')

        for i in range(len(colList)):

            ws.cell(row=1 ,column=i+2+len(column_keyList2)).value = colList[i]
            for j in range(len(row_keyList1)):
                key = row_keyList1[j]+colList[i]
                value = value_dictionary1[key]
                ws.cell(row=j+2, column=2+i+len(column_keyList2)).value = value



    #  处理行
    rowList = __compare_row(row_keyList1, row_keyList2)
    if len(rowList) <=0:
        print('没有添加新的行')
    else:
        print('添加了新行')
        for i in range(len(rowList)):

            ws.cell(row=i+len(row_keyList2)+2, column=1).value = rowList[i]
            for j in range(len(column_keyList1)):

                key = rowList[i]+column_keyList1[j]
                value = value_dictionary1[key]
                ws.cell(row=i+len(row_keyList2)+2,column=2+j).value = value

    wb.save('/Users/tusm/Desktop/dou.xlsx')




  • 代码里面应该还有bug 后续继续改进
    Python新手、关注一起学习
    原文作者:獨荹儛臨
    原文地址: https://www.jianshu.com/p/269da5ed1f6d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞