{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 工具类\n", "## 生成随机数,用于图片文件名" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "G8hjNFEt.png\n" ] } ], "source": [ "import random\n", "import string\n", "\n", "def generate_random_filename(extension=\"png\", length=8):\n", " characters = string.ascii_letters + string.digits\n", " random_string = ''.join(random.choices(characters, k=length))\n", " return f\"{random_string}.{extension}\"\n", "\n", "# 示例使用\n", "print(generate_random_filename())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 生成 argon2 密码" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hashed password: $argon2id$v=19$m=65536,t=3,p=4$lFZ6NGOPLXGAjO7u3B+6kw$wvkNArNX7DGsmqHANJ+ah1ggPm/F8tdKURSYx3vaDwg\n", "Password is valid: True\n" ] } ], "source": [ "# 安装 argon2-cffi 库\n", "# 在终端中运行以下命令\n", "# pip install argon2-cffi\n", "\n", "from argon2 import PasswordHasher\n", "\n", "# 创建一个 PasswordHasher 实例\n", "ph = PasswordHasher()\n", "\n", "def hash_password(password):\n", " \"\"\"\n", " 使用 argon2 哈希密码\n", " :param password: 原始密码\n", " :return: 哈希后的密码\n", " \"\"\"\n", " return ph.hash(password)\n", "\n", "def verify_password(hashed_password, password):\n", " \"\"\"\n", " 验证密码是否匹配\n", " :param hashed_password: 哈希后的密码\n", " :param password: 原始密码\n", " :return: 是否匹配\n", " \"\"\"\n", " try:\n", " ph.verify(hashed_password, password)\n", " return True\n", " except:\n", " return False\n", "\n", "# 示例用法\n", "if __name__ == \"__main__\":\n", " original_password = \"my_secure_password\"\n", " hashed = hash_password(original_password)\n", " print(f\"Hashed password: {hashed}\")\n", "\n", " is_valid = verify_password(hashed, original_password)\n", " print(f\"Password is valid: {is_valid}\") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 将Word文字提取为Txt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'docx'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[2], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdocx\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Document\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mextract_text_from_docx\u001b[39m(docx_path, txt_path):\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# 打开Word文档\u001b[39;00m\n\u001b[0;32m 6\u001b[0m doc \u001b[38;5;241m=\u001b[39m Document(docx_path)\n", "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'docx'" ] } ], "source": [ "from docx import Document\n", "\n", "def extract_text_from_docx(docx_path, txt_path):\n", " # 打开Word文档\n", " \n", " doc = Document(docx_path)\n", " \n", " # 提取所有段落的文字\n", " full_text = []\n", " for para in doc.paragraphs:\n", " full_text.append(para.text)\n", " \n", " # 将文字写入txt文件\n", " with open(txt_path, 'w', encoding='utf-8') as txt_file:\n", " txt_file.write('\\n'.join(full_text))\n", "\n", "# 示例用法\n", "docx_path = 'example.docx' # 替换为你的Word文档路径\n", "txt_path = 'output.txt' # 替换为你想要保存的txt文件路径\n", "extract_text_from_docx(docx_path, txt_path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 将 Markdown 转为 Word" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: pypandoc in c:\\users\\wangq\\miniconda3\\envs\\local_p312\\lib\\site-packages (1.13)\n", "Note: you may need to restart the kernel to use updated packages.\n", "转换成功: C:\\Users\\wangq\\OneDrive\\Wantsong.Life\\Work\\生产管理系统\\数字化转型下的智慧电厂建设.docx\n" ] } ], "source": [ "%pip install pypandoc\n", "import pypandoc\n", "\n", "def convert_md_to_docx(md_file, output_file, css_file=None):\n", " extra_args = []\n", " if css_file:\n", " extra_args = ['--css', css_file]\n", " output = pypandoc.convert_file(md_file, 'docx', outputfile=output_file, extra_args=extra_args)\n", " assert output == \"\"\n", " print(f\"转换成功: {output_file}\")\n", "\n", "# 示例使用\n", "#convert_md_to_docx(r'C:\\Users\\wangq\\OneDrive\\Wantsong.Life\\Work\\生产管理系统\\数字化转型下的智慧电厂建设.md', r'C:\\Users\\wangq\\OneDrive\\Wantsong.Life\\Work\\生产管理系统\\数字化转型下的智慧电厂建设.docx')\n", "\n", "# 示例使用\n", "convert_md_to_docx(\n", " r'C:\\Users\\wangq\\OneDrive\\Wantsong.Life\\Work\\生产管理系统\\数字化转型下的智慧电厂建设.md',\n", " r'C:\\Users\\wangq\\OneDrive\\Wantsong.Life\\Work\\生产管理系统\\数字化转型下的智慧电厂建设.docx',\n", " r'C:\\Users\\wangq\\OneDrive\\Wantsong.Life\\Work\\md2word.css'\n", ")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Markdown 转为 PDF" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "转换成功: C:\\Users\\wangq\\OneDrive\\Wantsong.Life\\Work\\生产管理系统\\数字化转型下的智慧电厂建设.pdf\n" ] } ], "source": [ "import pypandoc\n", "\n", "def convert_md_to_pdf(md_file, output_file):\n", " output = pypandoc.convert_file(md_file, 'pdf', outputfile=output_file, extra_args=['--pdf-engine=xelatex'])\n", " assert output == \"\"\n", " print(f\"转换成功: {output_file}\")\n", "\n", "# 示例使用\n", "convert_md_to_pdf(r'C:\\Users\\wangq\\OneDrive\\Wantsong.Life\\Work\\生产管理系统\\数字化转型下的智慧电厂建设.md', r'C:\\Users\\wangq\\OneDrive\\Wantsong.Life\\Work\\生产管理系统\\数字化转型下的智慧电厂建设.pdf')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SVG 转为 PNG" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import cairosvg\n", "\n", "def convert_svg_to_png(input_svg_path, output_png_path):\n", " cairosvg.svg2png(url=input_svg_path, write_to=output_png_path)\n", "\n", "# 示例调用\n", "convert_svg_to_png(r'C:\\Users\\wangq\\OneDrive\\Wantsong.Life\\Note\\Prompts\\汉语新解\\教师.svg', r'C:\\Users\\wangq\\OneDrive\\Wantsong.Life\\Note\\Prompts\\汉语新解\\教师.png')\n" ] } ], "metadata": { "kernelspec": { "display_name": "local_p312", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }