python Excel处理之sheet1提取信息到sheet2

# -*- coding: UTF-8 -*-
# import docx
import openpyxl
# from openpyxl import Workbook
# from openpyxl.styles import PatternFill, Border, Side, Font, Alignment
# from docx import Document
# import re


# ------------------------------------------------------------------------------
def is_chinese(string):
    """
    检查整个字符串是否包含中文
    :param string: 需要检查的字符串
    :return: bool
    """
    for ch in string:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True

    return False


# ------------------------------------------------------------------------------创建打开excel文档
type_name = 'test'
file_name_rd = type_name+'.xlsx'
wb = openpyxl.load_workbook(file_name_rd)
# 获取所有工作表名
names = wb.sheetnames
# wb.get_sheet_by_name(name) 已经废弃,使用wb[name] 获取指定工作表
sheet1 = wb[names[0]]
sheet2 = wb[names[1]]
sheet3 = wb[names[2]]
maxRow1 = sheet1.max_row
x = 0
str1 = []
str2 = []
indexStrm = ''

for row in sheet2['A0:M200']:
    for cell in row:
        cell.value = ''
# -------------------------------------------------------------------------------
for i in range(1, maxRow1+1, 1):
    j = 0
    for j in range(1, i, 1):
        if sheet1.cell(i, 2).value == sheet1.cell(j, 2).value:
            sheet1.cell(i, 1).value = '----'
            break
    text_tem0 = str(sheet1.cell(i, 1).value)
    if 'None' != text_tem0 and '' != text_tem0 and not ('-' in text_tem0):
        x += 1
        strtemp = sheet1.cell(i, 1).value
        strtemp = strtemp.replace('H', '')
        strtemp = '0x' + strtemp
        sheet2.cell(x, 1).value = strtemp
        sheet2.cell(x, 2).value = sheet1.cell(i, 2).value
        j = 3
        j1 = 3
        while j < 11:
            strtemp = str(sheet1.cell(i, j1).value)
            if is_chinese(strtemp):
                break
            else:
                if not('[' in strtemp):
                    if '/' in strtemp:
                        strtemp = strtemp.replace('/', '')
                    sheet2.cell(x, j).value = strtemp
                    j += 1
                else:
                    str1 = strtemp.split('[', 1)
                    strm = str1[0]
                    indexstrm = str1[1]
                    indexstrm = indexstrm.replace('[', '')  #
                    indexstrm = indexstrm.replace(']', '')  # 去括号
                    str2 = indexstrm.split(':', 1)
                    print(str2)
                    indexNum0 = int(str2[0])
                    indexNum1 = int(str2[1])
                    for k in range(0, indexNum0-indexNum1+1, 1):
                        sheet2.cell(x, j).value = strm + str(indexNum0-k)
                        testtemp = str(sheet2.cell(x, j).value)
                        j += 1

                j1 += 1

wb.save(file_name_rd)
print('OVER1')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值