当前位置: 首页 > news >正文

做网站的害处罗村网站开发

做网站的害处,罗村网站开发,推广方式英文,wordpress 生态我们平时从网上下载一些文件,文件名很多都是一大串字母和数字,不打开看看,根本不知道里面是什么内容。 我想能不能做个工具,把我们一个文件夹下面的所有word、excel、ppt、pdf文件重命名为文件内容的第一行。 我们有些朋友可能不会…

我们平时从网上下载一些文件,文件名很多都是一大串字母和数字,不打开看看,根本不知道里面是什么内容。

在这里插入图片描述

我想能不能做个工具,把我们一个文件夹下面的所有word、excel、ppt、pdf文件重命名为文件内容的第一行。

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

我们有些朋友可能不会编程,别慌,不会编程也没关系,我们可以让AI帮我们写一个Python程序。

下面我就让Kimi帮我们写一个程序:

在这里插入图片描述

我把上面这段代码复制到vscode中,把‘path_to_your_directory’替换成自己的文件夹路径,直接运行一下。

在这里插入图片描述

很不幸,报错了,没事,把错误提示复制出来,继续问Kimi:

在这里插入图片描述

再报错,再问:

在这里插入图片描述

你就不厌其烦的问。

AI的一个好处是他永远不会发脾气,哈哈,你只管问就行,不管你的问题有多白痴,它总是会耐心回答,也不会笑话你,这点AI比人类要强百倍。

最终AI给出了一个可运行的版本:

import os
from docx import Document
from openpyxl import load_workbook
from pptx import Presentation
from pdfminer.high_level import extract_textdef get_first_line_from_word(file_path):try:doc = Document(file_path)return doc.paragraphs[0].text if doc.paragraphs else ''except Exception as e:print(f"Error reading Word file {file_path}: {e}")return ''def get_first_line_from_excel(file_path):try:wb = load_workbook(file_path)sheet = wb.activefor row in sheet.iter_rows(min_row=1, values_only=True):for value in row:if value is not None:return str(value)return ''except Exception as e:print(f"Error reading Excel file {file_path}: {e}")return ''def get_first_line_from_ppt(file_path):try:presentation = Presentation(file_path)for slide in presentation.slides:for shape in slide.shapes:if hasattr(shape, 'text') and shape.text:return shape.text[:shape.text.index('\n')] if '\n' in shape.text else shape.textreturn ''except Exception as e:print(f"Error reading PPT file {file_path}: {e}")return ''def get_first_line_from_pdf(file_path):try:text = extract_text(file_path)return text.split('\n', 1)[0] if text else ''except Exception as e:print(f"Error reading PDF file {file_path}: {e}")return ''def rename_files(directory):for filename in os.listdir(directory):if filename.lower().endswith(('.docx', '.xlsx', '.pptx', '.pdf')) and not filename.lower().startswith('.~') :file_path = os.path.join(directory, filename)first_line = ''if filename.lower().endswith('.docx'):first_line = get_first_line_from_word(file_path)elif filename.lower().endswith('.xlsx'):first_line = get_first_line_from_excel(file_path)elif filename.lower().endswith('.pptx'):first_line = get_first_line_from_ppt(file_path)elif filename.lower().endswith('.pdf'):first_line = get_first_line_from_pdf(file_path)if first_line:new_filename = first_line.strip() + os.path.splitext(filename)[1]new_file_path = os.path.join(directory, new_filename)os.rename(file_path, new_file_path)print(f"Renamed {filename} to {new_filename}")else:print(f"No first line found for {filename}")# Specify the directory containing the files
directory = '/Users/zhan/Documents/test'  # Replace with the path to your directory
rename_files(directory)

运行效果如下:

在这里插入图片描述

我们还需要一个UI界面,让我们可以在图形界面上操作,我们继续问Kimi:

在这里插入图片描述
最终代码如下:

import os
from docx import Document
from openpyxl import load_workbook
from pptx import Presentation
from pdfminer.high_level import extract_text
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter import ttkdef get_first_line_from_word(file_path):try:doc = Document(file_path)return doc.paragraphs[0].text if doc.paragraphs else ''except Exception as e:print(f"Error reading Word file {file_path}: {e}")return ''def get_first_line_from_excel(file_path):try:wb = load_workbook(file_path)sheet = wb.activefor row in sheet.iter_rows(min_row=1, values_only=True):for value in row:if value is not None:return str(value)return ''except Exception as e:print(f"Error reading Excel file {file_path}: {e}")return ''def get_first_line_from_ppt(file_path):try:presentation = Presentation(file_path)for slide in presentation.slides:for shape in slide.shapes:if hasattr(shape, 'text') and shape.text:return shape.text[:shape.text.index('\n')] if '\n' in shape.text else shape.textreturn ''except Exception as e:print(f"Error reading PPT file {file_path}: {e}")return ''def get_first_line_from_pdf(file_path):try:text = extract_text(file_path)return text.split('\n', 1)[0] if text else ''except Exception as e:print(f"Error reading PDF file {file_path}: {e}")return ''def rename_files(directory):for filename in os.listdir(directory):if filename.lower().endswith(('.docx', '.xlsx', '.pptx', '.pdf')) and not filename.lower().startswith('.~') :file_path = os.path.join(directory, filename)first_line = ''if filename.lower().endswith('.docx'):first_line = get_first_line_from_word(file_path)elif filename.lower().endswith('.xlsx'):first_line = get_first_line_from_excel(file_path)elif filename.lower().endswith('.pptx'):first_line = get_first_line_from_ppt(file_path)elif filename.lower().endswith('.pdf'):first_line = get_first_line_from_pdf(file_path)if first_line:new_filename = first_line.strip() + os.path.splitext(filename)[1]new_file_path = os.path.join(directory, new_filename)os.rename(file_path, new_file_path)print(f"Renamed {filename} to {new_filename}")else:print(f"No first line found for {filename}")# Specify the directory containing the files
directory = '/Users/zhan/Documents/test'  # Replace with the path to your directory      # 这里是之前定义的rename_files函数和子函数
def choose_directory():directory = filedialog.askdirectory()if directory:entry.delete(0, tk.END)entry.insert(0, directory)def rename_files_with_ui():directory = entry.get()if not directory:messagebox.showerror("错误", "请选择一个文件夹")returnif not os.path.isdir(directory):messagebox.showerror("错误", "所选路径不是一个文件夹")returntry:rename_files(directory)messagebox.showinfo("完成", "文件重命名完成")except Exception as e:messagebox.showerror("错误", f"发生错误: {e}")# 创建主窗口
root = tk.Tk()
root.title("文件批量重命名工具")# 创建一个标签和输入框用于显示选择的文件夹路径
label = ttk.Label(root, text="请选择文件夹:")
label.pack()
entry = tk.Entry(root, width=30)
entry.pack()# 创建一个按钮,点击时弹出选择文件夹的对话框
browse_button = ttk.Button(root, text="浏览", command=choose_directory)
browse_button.pack()# 创建一个按钮,点击时执行重命名操作
rename_button = ttk.Button(root, text="确定", command=rename_files_with_ui)
rename_button.pack()# 设置窗口的尺寸
width = 350  # 宽度
height = 200  # 高度# 获取屏幕的宽度和高度
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()# 计算窗口的中心坐标
x = (screen_width / 2) - (width / 2)
y = (screen_height / 2) - (height / 2)# 将窗口放置在屏幕中心
root.geometry(f'{width}x{height}+{int(x)}+{int(y)}')# 运行主循环
root.mainloop()

我们看下运行效果:

在这里插入图片描述

试了一下,功能和上面的程序是一样的。

最后一步就是打包程序了,同样我们问下AI:

在这里插入图片描述
一共就两步:

  1. 安装PyInstaller
pip install pyinstaller
  1. 使用PyInstaller打包
pyinstaller --onefile --windowed file_renamer_gui.py

注意要把“file_renamer_gui.py"替换成你自己的文件名。

打包好之后,在项目目录的dist文件夹下就可以找到打包好的文件。

在这里插入图片描述

双击打开即可运行,效果是一样的。

在这里插入图片描述

好了,这个工具就写好了。

有了AI的助攻,我们想写什么工具直接让AI帮我们写就好了,是不是给了你很大的信心?

原来编程也不难,编程我也会啊~

上面的代码亲测可用,如果你想直接下载exe文件,关注公众号“编程我也会”,回复“重命名”即可下载。

http://www.yayakq.cn/news/326894/

相关文章:

  • 自己做的网站收费网站打开出现建设中
  • 公司网站修改 优帮云上海网站关键词优化方法
  • 延吉市住房城乡建设局官方网站商场vi设计 知名公司
  • 做汽配的 哪一个网站比较好适合小公司的记账软件
  • 丽江市建设局网站深圳宝安区有什么好玩的地方
  • 西安北郊网站维护运营中文网址怎么注册
  • 关于建设 医院网站的请示高端网站开发注意事项
  • 网站头页吴江住房和城乡建设部网站
  • 上海网站建设与设计公司湖北移动官网网站建设
  • 腾讯有服务器如何做网站域名注册需要多少钱
  • 网站后台难做么网站设置英文怎么说
  • 快站优惠券长沙seo优化推荐
  • 做企业网站收费价格网站建设王滨1983
  • 企业网站建站系统域名估价查询
  • 做外贸客户要求看网站莞城短视频seo优化
  • 天津城市建设招标网站网站开发外包
  • html5网站后台网站建设的行业代码是多少
  • 网站备案编号深圳专业网站建设公司多少钱
  • 上海微信网站建设公司深圳市光明区住房和建设局
  • wordpress新页面代码seo营销推广服务公司
  • 用html制作旅游网站济南网站制作公司哪家好
  • 电子商务网站建设与规划制作网站生成器
  • 手机影视素材网站大全定制建站公司
  • 建设厅网站打不开中装建设(002822)股吧
  • 泾川县住房和城乡建设局网站2003建立虚拟网站
  • 网站新闻页面设计站长之家ping检测
  • 房产网站的全景图怎么做网站建设前需求调研表
  • 网站由哪些部分组成部分网站被挂木马怎么办
  • 遵义网站建设中心个人社保缴费基数是什么意思
  • 青岛团购网站建设网站运营方案ppt