BaiHekin_g 2022-05-31 16:39 采纳率: 0%
浏览 200
已结题

AttributeError: 'NoneType' object has no attribute 'group'


import openpyxl
from docx import Document
from docx.shared import Inches
from docxtpl import DocxTemplate, InlineImage
from openpyxl import load_workbook

import os
import re


def load_excel_data():
    location='C:\\Users\\caoxiangting\\Desktop\\Test.xlsx'
    wb = openpyxl.load_workbook(location)  # 需要填入Word的Excel工作簿的地址
    ws = wb['Sheet1']  # 工作簿中表格的名称
    contexts = []

    for row in range(2, ws.max_row + 1):
        b_content = ws["A" + str(row)].value  # 字母代表表格中对应的列,顺序和列名一定要对应上
        e_content = ws["F" + str(row)].value
        f_content = ws["H" + str(row)].value
        g_content = ws["I" + str(row)].value

        if not b_content and not e_content and not f_content and not g_content:
            break

        context = {"no": b_content, "e_content": e_content, "f_content": f_content,
                   "g_content": g_content}
        contexts.append(context)
    return contexts


def package_doc(handle_data):
    title_first = ''
    title_first_arr = []
    title_second = ''
    title_second_arr = []
    title_third = ''
    title_third_arr = []
    end = True
    doc_item_list = []

    for index, v in enumerate(handle_data):
        doc_item = {}
        if not v['no'] and not v['e_content'] and not v['f_content'] and not v['g_content']:
            break
        no_str = str(v['no']) if v['no'] else None
        e_content_str = str(v['e_content']) if v['e_content'] else None
        if no_str and re.match(r'\d+[.\d]*[.\d]*', no_str) and not re.match(r'^\d+$', no_str):
            if not end:
                end = True
                title_first = ''
                title_second = ''
                title_third = ''
            # 一级标题和二级标题识别
            if title_first:
                title_second = no_str
                doc_item['title_second'] = no_str
                if no_str in title_second_arr:
                    continue
                else:
                    title_second_arr.append(no_str)
            else:
                title_first = no_str
                doc_item['title_first'] = no_str
                if no_str in title_first_arr:
                    continue
                else:
                    title_first_arr.append(no_str)
        else:
            if not no_str and v['f_content']:
                # 二级或三级标题
                if not title_second:
                    title_second = v['f_content']
                    doc_item['title_second'] = v['f_content']
                    if no_str in title_second_arr:
                        continue
                    else:
                        title_second_arr.append(v['f_content'])
                else:
                    title_third = v['f_content']
                    doc_item['title_third'] = v['f_content']
                    if no_str in title_third_arr:
                        continue
                    else:
                        title_third_arr.append(v['f_content'])
            else:
                # 正式数据
                if title_third:
                    res = re.match(r'\d+[.\d]*[.\d]*', title_third)
                    i = res.group()
                    doc_item['title_fourth'] = str(i) + '.' + no_str + ' ' + e_content_str
                else:
                    res = re.match(r'\d+[.\d]*[.\d]*', title_second)
                    i = res.group()
                    doc_item['title_third'] = str(i) + '.' + no_str + ' ' + e_content_str
                doc_item['c1'] = v['f_content']
                doc_item['c2'] = v['g_content']
                end = False
        doc_item_list.append(doc_item.copy())
    return doc_item_list


def print_doc(resource):
    location="C:\\Users\\caoxiangting\\Desktop\\Test.docx"
    location2='C:\\Program Files\\test'
    path = location2
    tpl = DocxTemplate(location)  # 需要填入的Word文档的的地址
    if not os.path.exists(path):
        os.mkdir(path)

    context = {'data': resource}
    tpl.render(context, autoescape=True)
    tpl.save(location2 + '.\\1002.docx')


if __name__ == '__main__':
    data = load_excel_data()
    doc_data = package_doc(data)
    print_doc(doc_data)

在改代码中第94这里,我跑的时候提示没有对象属性group??没太明白,代码是朋友发我的。我自己研究目前还没太懂

img

代码的目的是想自动把Excel中部分列的内容自动转移去Work文档去

  • 写回答

4条回答 默认 最新

  • 为轮子而生 2022-05-31 17:18
    关注
    获得0.30元问题酬金

    res是正则匹配re.match(r'\d+[.\d][.\d]', title_second)的返回值,而res.group()提示“NoneType”通常是匹配没有成功,输入的字符串并没有所需的格式,可使用以下代码先对匹配结果进行验证,如果验证成功再调用group()方法:

    if res:
        # 此处可继续执行后续的 doc_item.... 代码
        print('匹配的内容为:', res.group())
    else:
        print('没有匹配的内容')
    
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月8日
  • 修改了问题 5月31日
  • 创建了问题 5月31日

悬赏问题

  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图
  • ¥15 关于#DMA固件#的问题,请各位专家解答!
  • ¥15 matlab生成的x1图不趋于稳定,之后的图像是稳定的水平线
  • ¥15 请问华为OD岗位的内部职业发展通道都有哪些,以及各个级别晋升的要求
  • ¥20 微信小程序 canvas 问题
  • ¥15 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照