鱼C论坛

 找回密码
 立即注册
查看: 1447|回复: 3

openpyxl问题求助!!!

[复制链接]
发表于 2021-12-9 10:04:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
想写个工作上的工具,读取log类型的文件处理成想要的信息,通过geting结果显示的TEXT的文本框,处理后赋值给EXCEL文件,但报错如下:
错误类型:openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support .log file format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,.xltx,.xltm
请问大神们有好的解决办法没?
源代码如下:


from tkinter import *
from tkinter import filedialog
import tkinter.messagebox as messagebox
from openpyxl import *


class Loginpage:
    def __init__(self, master):
        self.root = master
        self.root.title('系统登录')
        self.root.resizable(width=False, height=False)
        # 获取屏幕尺寸以计算布局参数,使窗口居屏幕中央
        self.root.width = 300
        self.root.height = 180
        width = self.root.width
        height = self.root.height
        screenwidth = self.root.winfo_screenwidth()
        screenheight = self.root.winfo_screenheight()
        alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.root.geometry(alignstr)
        self.username = StringVar()
        self.password = StringVar()
        self.page = Frame(self.root)
        self.page.grid()
        self.creatpage()

    def creatpage(self):
        Label(self.page, width=15).grid(row=0, column=0)
        # 标签控件布局
        Label(self.page, text="用户名:").grid(row=1, column=0, ipadx=15)
        Entry(self.page, textvariable=self.username).grid(row=1, column=1, sticky=W)

        Label(self.page, text="密    码:").grid(row=2, column=0, ipadx=15)
        Entry(self.page, show='*', textvariable=self.password).grid(row=2, column=1, sticky=W)

        Button(self.page, text='登录', width=8, command=self.check).grid(row=3, column=0, sticky=E, pady=5)
        Button(self.page, text='退出', width=8, command=self.page.quit).grid(row=3, column=1, pady=5)

    def check(self):
        if self.username.get() == "chenpingping" and self.password.get() == "collins9505":
            messagebox.showinfo(title='welcome', message='欢迎登录诺基亚网优工具系统 ')
            self.page.destroy()
            Mainpage(self.root)
        elif self.username.get() != "chenpingping" and self.password.get() == "collins9505":
            messagebox.showinfo(title='警告', message='用户名错误,请重新填写! ')
        elif self.username.get() == "chenpingping" and self.password.get() != "collins9505":
            messagebox.showinfo(title='警告', message='密码错误,请重新填写! ')
        else:
            messagebox.showinfo(title='警告', message='用户名以及密码错误,请重新填写!')


class Mainpage:
    def __init__(self, master):
        self.root = master
        # 设置窗口标题
        self.root.title("诺基亚GSM网优工具V_1")
        # 设置窗口宽和高是否可变
        self.root.resizable(width=False, height=False)
        # 获取屏幕尺寸以计算布局参数,使窗口居屏幕中央
        self.root.width = 800
        self.root.height = 600
        width = self.root.width
        height = self.root.height
        screenwidth = self.root.winfo_screenwidth()
        screenheight = self.root.winfo_screenheight()
        alignstr = "%dx%d+%d+%d" % (
            width,
            height,
            (screenwidth - width) / 2,
            (screenheight - height) / 2,
        )
        self.root.geometry(alignstr)
        # 创建一级子目录
        menubar = Menu(self.root)
        fmenu1 = Menu(self.root, tearoff=0)
        fmenu2 = Menu(self.root, tearoff=0)
        fmenu3 = Menu(self.root, tearoff=0)
        fmenu4 = Menu(self.root, tearoff=0)
        # 创建‘工具’下二级子目录
        file_menu = ["打开", "保存", "另存为", "设置"]
        for item in file_menu:
            fmenu1.add_command(label=item)
        for item in ["注册用户"]:
            fmenu3.add_command(label=item)
        for item in ["版权信息", "软件说明"]:
            fmenu4.add_command(label=item)
        # add_cascade 的一个很重要的属性就是 menu 属性,它指明了要把那个菜单级联到该菜单项上,
        # 当然,还必不可少的就是 label 属性,用于指定该菜单项的名称
        menubar.add_cascade(label="文件", menu=fmenu1)
        menubar.add_cascade(label="工具", menu=fmenu2)
        menubar.add_cascade(label="注册", menu=fmenu3)
        menubar.add_cascade(label="关于", menu=fmenu4)
        # 添加功能
        fmenu1.add_command(label="退出系统", command=self.root.destroy)
        fmenu2.add_command(label="闭锁载频查询", command=self.crateviewpage)
        fmenu2.add_command(label="经纬度距离计算", command=self.crateview2page)
        fmenu2.add_command(label="半速率开启或关闭", command=self.crateview3page)
        # 最后可以用窗口的 menu 属性指定我们使用哪一个作为它的顶层菜单
        self.root.config(menu=menubar)
        self.fra = Frame(self.root, width=800, height=600)
        self.fra.grid()

    def crateviewpage(self):
        for widget in self.fra.winfo_children():
            widget.destroy()
        Label(self.fra, text="打开ZEEL的Log文件:").grid(row=1, column=0, sticky=W, padx=10, pady=5)
        self.file_text1 = Text(self.fra, width=50, height=2)
        self.file_text1.grid(row=2, column=0, sticky=W, padx=10)
        Button(self.fra, text="打开ZEEL.Log文件...", command=self.read_log).grid(row=2, column=1, sticky=W)
        Button(self.fra, text="保存结果...", command=self.save_reslut).grid(row=2, column=2, sticky=W)
        # 插入文本框
        self.file_text2 = Text(self.fra, width=110, height=38)
        self.file_text2.grid(row=3, column=0, columnspan=4, padx=10, pady=15)

    def read_log(self):
        global bsc, bcf, bcf_state, bts, bts_state, bts_name
        self.file_path = filedialog.askopenfilename()
        if self.file_path is None:  # fixed BUG 1
            return
        self.file_text1.insert("insert", self.file_path)
        file_opened = open(self.file_path, "r")  # 不要用关键字file做变量名
        a_list = []
        counts = 0
        str1 = ""
        # file_name = os.path.basename(self.file_path)
        for each_line in file_opened:
            a_list.append(each_line)
            if "FlexiBSC" in each_line:
                bsc = each_line[9:19].replace("\n", "")

            if "BCF-" in each_line:
                bcf = each_line[4:8]
                bcf_state = each_line[15:21].replace("\n", "")
                if len(bcf_state) < 6:
                    bcf_state += (6 - len(bcf_state)) * " "

            if " BTS-" in each_line:
                bts = each_line[5:9]
                bts_state = each_line[15:21].replace("\n", "")
                bts_name = a_list[counts - 1].replace("\n", "")
                if len(bts_state) < 6:
                    bts_state += (6 - len(bts_state)) * " "
                if len(bts_name) < 20:
                    bts_name += (20 - len(bts_name)) * " "

            if "  TRX-" in each_line:
                trx = each_line[6:9]
                trx_state = each_line[15:21].replace("\n", "")
                if len(trx_state) < 6:
                    trx_state += (6 - len(trx_state)) * " "
                str1 += (
                        "退服载频:"
                        + bsc
                        + "  BCF-"
                        + bcf
                        + "   "
                        + bcf_state
                        + "  BTS-"
                        + bts
                        + "   "
                        + bts_state
                        + "  "
                        + bts_name
                        + "TRX-"
                        + trx
                        + "  "
                        + trx_state
                        + "\n"
                )
            counts += 1
        self.file_text2.insert("insert", str1)

    def save_reslut(self):
        self.savefile_path = filedialog.asksaveasfilename()
        if self.savefile_path is None:  # fixed BUG 1
            return
        #self.savefile_opened = open(self.savefile_path, 'w')
        self.wb = Workbook()
        self.ws = self.wb.active
        self.ws.title = "result"
        self.ws.append(["BSC", "BCF", "BCF-STATE", "BTS", "BTS-STATE", "NAME", "TRX", "TRX-STATE"])
        counts = 1
        for each_row in (self.file_text2.get("1.0", "end")):
            counts += 1
            self.ws.cell(row=counts, column=1).value = each_row[5:14]
        #self.wb.save(self.savefile_path)
        #self.savefile_opened.write(self.file_text2.get("1.0", "end"))
        #self.savefile_opened.close()
        messagebox.showinfo(title='提示', message='保存成功! ')

    def crateview2page(self):
        for widget in self.fra.winfo_children():
            widget.destroy()
        Label(self.fra, text="经纬度之间距离计算器", font=10).grid(row=0, column=0, columnspan=3, padx=10, pady=10)
        Label(self.fra, text="请输入经度:").grid(row=1, column=0, sticky=W, padx=10)
        Label(self.fra, text="请输入纬度:").grid(row=2, column=0, sticky=W, padx=10)
        Label(self.fra, text="距离(m):").grid(row=3, column=0, sticky=W, padx=10)
        Entry(self.fra, width=30, textvariable=None).grid(row=1, column=1)
        Entry(self.fra, width=30, textvariable=None).grid(row=2, column=1)
        Text(self.fra, width=30, height=1).grid(row=3, column=1)
        Button(self.fra, text="计算...", command=None).grid(row=4, column=1, sticky=W, padx=5, pady=5)
        Button(self.fra, text="批量计算...", command=None).grid(row=4, column=1, sticky=W, padx=90, pady=5)

    def crateview3page(self):
        for widget in self.fra.winfo_children():
            widget.destroy()
        Label(self.fra, text="打开ZERO的Log文件:").grid(row=1, column=0, sticky=W, padx=10, pady=5)
        Text(self.fra, width=50, height=2).grid(row=2, column=0, sticky=W, padx=10)
        Button(self.fra, text="打开ZERO.Log文件...", command=None).grid(row=2, column=1, sticky=W)
        Button(self.fra, text="生成半速率命令", command=None).grid(row=2, column=2, sticky=W)
        Button(self.fra, text="生成关闭半速率命令", command=None).grid(row=2, column=3, sticky=W)
        # 插入文本框y
        Text(self.fra, width=110, height=39).grid(row=3, column=0, columnspan=4, padx=10, pady=10)


if __name__ == '__main__':
    root = Tk()
    Loginpage(root)
    root.mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-9 10:53:45 | 显示全部楼层
openpyxls是管excel文件的,想让openpyxls管理log里的数据,得加工一下才能传给openpyxls。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-9 10:58:18 | 显示全部楼层
suchocolate 发表于 2021-12-9 10:53
openpyxls是管excel文件的,想让openpyxls管理log里的数据,得加工一下才能传给openpyxls。

是我把self.file_text2.get("1.0", "end")做了切片处理还是不行,请问有什么方法处理?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-9 11:01:14 | 显示全部楼层
suchocolate 发表于 2021-12-9 10:53
openpyxls是管excel文件的,想让openpyxls管理log里的数据,得加工一下才能传给openpyxls。

才学了两个半月,也不知道有啥好的办法?能否指教哈?下面还有好多功能需要如此处理
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-29 18:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表