需求功能模块分解
界面化,界面上能选择mdb,选择excel模板,结果输出。将mdb数据中的数据根据需要填写到excel表中,其中mdb中有多个表,excel表为xls格式,多个sheet页,只修改其中一个sheet页往其中写值,其他sheet页保留不动。
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
功能分解
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、需求是什么?
***1.有个界面,能选需要的mdb和excel模板,选择完后点击提取,输出到指定目录,输出结果为excel文件。该步骤简单,没什么特殊的需要,网上找个样例就能解决。
2.查询mdb数据,相对简单,连接完mdb后查询即可,存到临时的list中或其他什么里面。
3.写入到excle文件。比较难,需要读取,保存,而且其他sheet页数据样式都不能动,只修改shee2中的内容。***
二、使用步骤
1.导入库,需要的都导
代码如下(示例):
import tkinter as tk
from tkinter import filedialog, messagebox
import pyodbc
import pandas as pd
from openpyxl import load_workbook
def open_mdb_file():
global mdb_file_path
mdb_file_path = filedialog.askopenfilename(
initialdir="/",
title="选择MDB文件",
filetypes=(("MDB文件", "*.mdb"), ("所有文件", "*.*"))
)
mdb_file_label.config(text=f"选择的MDB文件: {mdb_file_path}")
def open_excel_template():
global excel_template_path
excel_template_path = filedialog.askopenfilename(
initialdir="/",
title="选择Excel模板",
filetypes=(("Excel文件", "*.xlsx;*.xls"), ("所有文件", "*.*"))
)
excel_template_label.config(text=f"选择的Excel模板: {excel_template_path}")
def choose_output_path():
global output_path
output_path = filedialog.asksaveasfilename(
defaultextension=".xlsx",
initialdir="/",
title="选择输出位置",
filetypes=(("Excel文件", "*.xlsx;*.xls"), ("所有文件", "*.*"))
)
output_label.config(text=f"选择的输出位置: {output_path}")
2.界面,能用就行
代码如下(示例):
# 创建主窗口
root = tk.Tk()
root.title("MDB数据提取")
root.geometry("250x200")
# 获取屏幕宽度和高度
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# 计算窗口的水平和垂直位置
x = screen_width // 2 - 125
y = screen_height // 2 - 100
# 设置窗口位置
root.geometry(f"+{x}+{y}")
# 界面布局
mdb_file_label = tk.Label(root, text="选择的MDB文件: ")
mdb_file_label.pack()
mdb_file_button = tk.Button(root, text="选择MDB文件", command=open_mdb_file)
mdb_file_button.pack()
excel_template_label = tk.Label(root, text="选择的Excel模板: ")
excel_template_label.pack()
excel_template_button = tk.Button(root, text="选择Excel模板", command=open_excel_template)
excel_template_button.pack()
output_label = tk.Label(root, text="选择的输出位置: ")
output_label.pack()
output_button = tk.Button(root, text="选择输出位置", command=choose_output_path)
output_button.pack()
extract_button = tk.Button(root, text="提取数据", command=extract_data)
extract_button.pack()
# 全局变量
mdb_file_path = None
excel_template_path = None
output_path = None
root.mainloop()
3.连接mdb并查询需要的数据
代码如下(示例):
##连接mdb数据库
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=' + mdb_file_path)
cursor = conn.cursor()
# 从Build表获取坐落区
cursor.execute("SELECT 坐落区 FROM Build")
build_data = cursor.fetchone()
df_template['坐落区'] = build_data[0]
4.读取excel文件写入数据
# 将数据写回Excel文件中的特定Sheet
df_template.to_excel(writer, sheet_name="公用建筑面积分摊系数表", index=False)
# 使用openpyxl来更新特定单元格的内容
wb = load_workbook(excel_template_path)
ws = wb["公用建筑面积分摊系数表"]
ws['E5'] = build_data[0] # 在E5单元格插入查询到的坐落区数据
# 保存Excel文件
wb.save(output_path)
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。