├── .github ├── ISSUE_TEMPLATE │ └── feature_request.md └── workflows │ └── codeql.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md └── script ├── FileEditor ├── line_merger.py └── replace_content.py ├── ImageEditor ├── overlay_images.py └── repeat_images.py └── README.md /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 功能建议 3 | about: 为这个项目提出一个想法 4 | title: '' 5 | labels: enhancement 6 | assignees: guobao2333 7 | 8 | --- 9 | 10 | **您的功能建议与问题相关吗? ** 11 | 对问题所在的清晰简洁的描述。 12 | 13 | **您想要请求功能的脚本?** 14 | 您想为当前项目提出建议和或请求的脚本路径。 15 | eg. script/xxx/A.py 16 | 17 | **描述您想要的解决方案** 18 | 清晰简洁地描述您想要实现的功能。 19 | 20 | **描述您考虑过的替代方案** 21 | 对您考虑过的任何替代解决方案或功能的清晰简洁的描述。 22 | 23 | **附加信息** 24 | 在此处添加有关功能请求的任何其他上下文或屏幕截图。 25 | -------------------------------------------------------------------------------- /.github/workflows/codeql.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: 17 | - 'script/**' 18 | pull_request: 19 | branches: [ "main" ] 20 | schedule: 21 | - cron: '18 10 * * 1' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze (${{ matrix.language }}) 26 | # Runner size impacts CodeQL analysis time. To learn more, please see: 27 | # - https://gh.io/recommended-hardware-resources-for-running-codeql 28 | # - https://gh.io/supported-runners-and-hardware-resources 29 | # - https://gh.io/using-larger-runners (GitHub.com only) 30 | # Consider using larger runners or machines with greater resources for possible analysis time improvements. 31 | runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} 32 | timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }} 33 | permissions: 34 | # required for all workflows 35 | security-events: write 36 | 37 | # required to fetch internal or private CodeQL packs 38 | packages: read 39 | 40 | # only required for workflows in private repositories 41 | actions: read 42 | contents: read 43 | 44 | strategy: 45 | fail-fast: false 46 | matrix: 47 | include: 48 | - language: python 49 | build-mode: none 50 | # CodeQL supports the following values keywords for 'language': 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift' 51 | # Use `c-cpp` to analyze code written in C, C++ or both 52 | # Use 'java-kotlin' to analyze code written in Java, Kotlin or both 53 | # Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both 54 | # To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, 55 | # see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. 56 | # If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how 57 | # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages 58 | steps: 59 | - name: Checkout repository 60 | uses: actions/checkout@v4 61 | 62 | # Initializes the CodeQL tools for scanning. 63 | - name: Initialize CodeQL 64 | uses: github/codeql-action/init@v3 65 | with: 66 | languages: ${{ matrix.language }} 67 | build-mode: ${{ matrix.build-mode }} 68 | # If you wish to specify custom queries, you can do so here or in a config file. 69 | # By default, queries listed here will override any specified in a config file. 70 | # Prefix the list here with "+" to use these queries and those in the config file. 71 | 72 | # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs 73 | # queries: security-extended,security-and-quality 74 | 75 | # If the analyze step fails for one of the languages you are analyzing with 76 | # "We were unable to automatically build your code", modify the matrix above 77 | # to set the build mode to "manual" for that language. Then modify this step 78 | # to build your code. 79 | # ℹ️ Command-line programs to run using the OS shell. 80 | # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun 81 | - if: matrix.build-mode == 'manual' 82 | run: | 83 | echo 'If you are using a "manual" build mode for one or more of the' \ 84 | 'languages you are analyzing, replace this with the commands to build' \ 85 | 'your code, for example:' 86 | echo ' make bootstrap' 87 | echo ' make release' 88 | exit 1 89 | 90 | - name: Perform CodeQL Analysis 91 | uses: github/codeql-action/analyze@v3 92 | with: 93 | category: "/language:${{matrix.language}}" 94 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Log/OS Files 2 | *.log 3 | 4 | # Backup Files 5 | *.bak 6 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog | 变更日志 2 | 3 | 这里是本项目文件的变更日志,不过由于本项目仓库并不是仅发布某个单独的发行版项目,所以与[通用变更日志](https://common-changelog.org)的规范会有所不同。 4 | 5 | > [!IMPORTANT] 6 | > 特殊规范: 7 | > 版本号前增加文件名作为标题: `日期 - 文件名 - 版本号` 8 | > 如果某一天进行了多次更新,且为同一类型,则合并至同一个二/三级标题下,其下属标题顺延增加。 9 | 10 | ## replace_content - [1.5.0](https://github.com/guobao2333/Python-HandyTool/commit/b0c2c76) - 2024-07-29 11 | ### Fixed | 修复 12 | 13 | 1. 修复文件夹拼写错误 14 | - `FileEditer` -> `FileEditor` 15 | - `ImageEditer` -> `ImageEditor` 16 | 2. 修复选择覆盖时`ctrl+c`退出出现的报错 17 | 18 | ### Changed | 变更 19 | 20 | * 现在默认不显示详细输出信息 21 | * 优化部分提示信息 22 | * 缩减函数名 23 | * 去除函数多余默认参数 24 | 25 | ### Added | 新增 26 | 27 | + 新增可选参数 `替换次数` 可用于指定在每个文件中的替换次数 28 | + 新增可选参数 `-e` or `--coding` 可用于指定编码 29 | + 新增可选参数 `-v` or `--verbose` 可用于显示详细输出信息 30 | 31 | --- 32 | ## 2024-07-27 33 | ### line_merger - [1.0.0](https://github.com/guobao2333/Python-HandyTool/commit/433b5bd) 34 | #### New | 新脚本 35 | 36 | + 新增python脚本:[逐行合并内容](script/FileEditor/line_merger.py) 37 | 作用:将`文件2`中的内容逐行合并到`文件1` 38 | 39 | ### replace_content - [1.4.0](https://github.com/guobao2333/Python-HandyTool/commit/433b5bd) 40 | #### Changed | 变更 41 | 42 | * `path`现在是不可空的必选参数 43 | * 优化部分代码逻辑 44 | * 优化部分提示信息 45 | 46 | --- 47 | ## 2024-07-10 - replace_content - [1.3.0](https://github.com/guobao2333/Python-HandyTool/commit/4065ada) 48 | ### Added | 新增 49 | 50 | + 为不覆盖时新建的文件名称增加后缀配置 51 | + 为操作文件时新增编码格式配置 52 | 53 | --- 54 | ## 2024-05-11 - replace_content - [1.2.1](https://github.com/guobao2333/Python-HandyTool/commit/39497da) 55 | ### Changed | 变更 56 | 57 | * **将仓库由 `python_handy-tool` 更名为 `Python-HandyTool`** 58 | 59 | * 调整脚本部分提示内容 60 | * 优化和完善文档 61 | 62 | ## 2024-05-08 - replace_content - [1.2.0](https://github.com/guobao2333/Python-HandyTool/commit/56cff86) 63 | ### Added | 新增 64 | 65 | + 新增`跳过匹配失败的文件`的功能 66 | 67 | --- 68 | ## 2024-04-21 69 | ### New | 新脚本 70 | 71 | + 新增python脚本:[合并图片](script/ImageEditor/overlay_images.py) overlay_images.py - [1.0.0](https://github.com/guobao2333/Python-HandyTool/commit/b929ef7) 72 | 作用:将`图片2`合并至`图片1`之上 73 | + 新增python脚本:[重复拼接图片](script/ImageEditor/repeat_images.py) repeat_images.py - [1.0.0](https://github.com/guobao2333/Python-HandyTool/commit/b929ef7) 74 | 作用:在同个方向上重复拼接图片 75 | 76 | ### Changed | 变更 77 | 78 | * **调整存放所有脚本的目录为`script`** 79 | * 优化和完善文档 80 | 81 | ## replace_content - [1.1.0](https://github.com/guobao2333/Python-HandyTool/commit/1e862b8) 82 | ### Fixed | 修复 83 | 84 | 1. 修复了批量操作文件时,**仅执行一次**的bug 85 | 2. 修复了批量操作文件时,错误传递`目录`参数用于操作文件的bug 86 | 87 | ### Changed | 变更 88 | 89 | * 增加了一些注释 90 | * 微调了提示信息 91 | 92 | * 说明文档修改了亿点点措辞~ 93 | * 问题模板也修改了亿点点措辞~ 94 | 95 | ### Added | 新增 96 | 97 | * 新增变更日志 98 | * 新增`.gitignore`文件 99 | 100 | --- 101 | ## 2024-04-19 - replace_content - [1.0.0](https://github.com/guobao2333/Python-HandyTool/commit/c0c63d5) 102 | **创建了此项目仓库**👍🏻 103 | ### New | 新脚本 104 | 105 | + 新增python脚本:[批量替换文件内容](script/FileEditor/replace_content.py) 106 | 作用:可使用正则表达式替换文件内的指定内容 107 | 108 | + 新增许可证 109 | + 新增`README.md`说明文档 110 | + 新增`ISSUE_TEMPLATE`问题模板 111 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2024 @shiguobaona 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 方便的py小工具 2 | 使用python编写的一些方便小工具 3 | 4 | [![Repo Link](https://img.shields.io/badge/Github-Repo-brightgreen?style=flat&logo=github)](https://github.com/guobao2333/Python-HandyTool) 5 | [![Repo License](https://img.shields.io/github/license/guobao2333/Python-HandyTool?logo=data:image/svg%2bxml;base64,PHN2ZyBhcmlhLWhpZGRlbj0idHJ1ZSIgaGVpZ2h0PSIxNiIgdmlld0JveD0iMCAwIDE2IDE2IiB2ZXJzaW9uPSIxLjEiIHdpZHRoPSIxNiIgZGF0YS12aWV3LWNvbXBvbmVudD0idHJ1ZSIgY2xhc3M9Im9jdGljb24gb2N0aWNvbi1sYXcgbXItMiIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiBzdHlsZT0idXNlci1zZWxlY3Q6IGF1dG87Ij4KPHBhdGggZmlsbD0iI2Y1ZjVmNSIgZD0iTTguNzUuNzVWMmguOTg1Yy4zMDQgMCAuNjAzLjA4Ljg2Ny4yMzFsMS4yOS43MzZjLjAzOC4wMjIuMDguMDMzLjEyNC4wMzNoMi4yMzRhLjc1Ljc1IDAgMCAxIDAgMS41aC0uNDI3bDIuMTExIDQuNjkyYS43NS43NSAwIDAgMS0uMTU0LjgzOGwtLjUzLS41My41MjkuNTMxLS4wMDEuMDAyLS4wMDIuMDAyLS4wMDYuMDA2LS4wMDYuMDA1LS4wMS4wMS0uMDQ1LjA0Yy0uMjEuMTc2LS40NDEuMzI3LS42ODYuNDVDMTQuNTU2IDEwLjc4IDEzLjg4IDExIDEzIDExYTQuNDk4IDQuNDk4IDAgMCAxLTIuMDIzLS40NTQgMy41NDQgMy41NDQgMCAwIDEtLjY4Ni0uNDVsLS4wNDUtLjA0LS4wMTYtLjAxNS0uMDA2LS4wMDYtLjAwNC0uMDA0di0uMDAxYS43NS43NSAwIDAgMS0uMTU0LS44MzhMMTIuMTc4IDQuNWgtLjE2MmMtLjMwNSAwLS42MDQtLjA3OS0uODY4LS4yMzFsLTEuMjktLjczNmEuMjQ1LjI0NSAwIDAgMC0uMTI0LS4wMzNIOC43NVYxM2gyLjVhLjc1Ljc1IDAgMCAxIDAgMS41aC02LjVhLjc1Ljc1IDAgMCAxIDAtMS41aDIuNVYzLjVoLS45ODRhLjI0NS4yNDUgMCAwIDAtLjEyNC4wMzNsLTEuMjg5LjczN2MtLjI2NS4xNS0uNTY0LjIzLS44NjkuMjNoLS4xNjJsMi4xMTIgNC42OTJhLjc1Ljc1IDAgMCAxLS4xNTQuODM4bC0uNTMtLjUzLjUyOS41MzEtLjAwMS4wMDItLjAwMi4wMDItLjAwNi4wMDYtLjAxNi4wMTUtLjA0NS4wNGMtLjIxLjE3Ni0uNDQxLjMyNy0uNjg2LjQ1QzQuNTU2IDEwLjc4IDMuODggMTEgMyAxMWE0LjQ5OCA0LjQ5OCAwIDAgMS0yLjAyMy0uNDU0IDMuNTQ0IDMuNTQ0IDAgMCAxLS42ODYtLjQ1bC0uMDQ1LS4wNC0uMDE2LS4wMTUtLjAwNi0uMDA2LS4wMDQtLjAwNHYtLjAwMWEuNzUuNzUgMCAwIDEtLjE1NC0uODM4TDIuMTc4IDQuNUgxLjc1YS43NS43NSAwIDAgMSAwLTEuNWgyLjIzNGEuMjQ5LjI0OSAwIDAgMCAuMTI1LS4wMzNsMS4yODgtLjczN2MuMjY1LS4xNS41NjQtLjIzLjg2OS0uMjNoLjk4NFYuNzVhLjc1Ljc1IDAgMCAxIDEuNSAwWm0yLjk0NSA4LjQ3N2MuMjg1LjEzNS43MTguMjczIDEuMzA1LjI3M3MxLjAyLS4xMzggMS4zMDUtLjI3M0wxMyA2LjMyN1ptLTEwIDBjLjI4NS4xMzUuNzE4LjI3MyAxLjMwNS4yNzNzMS4wMi0uMTM4IDEuMzA1LS4yNzNMMyA2LjMyN1oiIHN0eWxlPSJ1c2VyLXNlbGVjdDogYXV0bzsiPjwvcGF0aD4KPC9zdmc%2B&style=flat&label=Licence)](https://github.com/guobao2333/Python-HandyTool/blob/main/LICENSE) 6 | [![Common Changelog](https://common-changelog.org/badge.svg)](https://common-changelog.org) 7 | 8 | 最初只是为了某些小功能随手写的自动化脚本。 9 | 10 | 虽然说好像我实现的这些功能可能已经有人做了,甚至已经封装成了一个库,可能比我的更好用,但我的本意只是为了丰富自己的编程技术,就是写着玩(: 11 | 12 | 如果本项目对你有帮助,欢迎Star⭐ 13 | 14 | ## Change Log | 变更日志 15 | ### replace_content - [1.5.0](https://github.com/guobao2333/Python-HandyTool/commit/b0c2c76) - 2024-07-29 16 | #### Added | 新增 17 | 18 | + 新增可选参数 `替换次数` 可用于指定在每个文件中的替换次数 19 | + 新增可选参数 `-e` or `--coding` 可用于指定编码 20 | + 新增可选参数 `-v` or `--verbose` 可用于显示详细输出信息 21 | 22 | #### Changed | 变更 23 | 24 | * 现在默认不显示详细输出信息 25 | 26 | > 仅展示最新版本的部分变化,更多版本细节请[查看完整更新日志](CHANGELOG.md) 27 | 28 | --- 29 | ## Usage | 使用 30 | 需要安装python环境,还有pip用于安装依赖库。 31 | > 部分系统/包管理器安装的python可能不会自带pip,所以还需要自行安装,并且可能还需要设置系统环境变量。_**但这并不是本项目关注的重点,所以请自行解决。**_ 32 | 33 | 你可以在[script/README.md](script/README.md)中查看所有脚本。 34 | 35 | ### Usage Example | 用法示例 36 | > [!IMPORTANT] 37 | > 请注意!由于`ImageEditor/`目录下的脚本写的比较早,无法使用`-h`或`--help`参数获取帮助,请直接运行脚本获取帮助。 38 | > 目前没有太多时间精力去翻新,暂时就这样。 39 | 40 | 本仓库的参数解析使用到了`argparse`这个库,所以你需要先行安装: 41 | ```bash 42 | pip install argparse 43 | ``` 44 | 45 | 如果你还没有安装python和pip,请阅读`安装运行环境`这一章节。 46 | #### Clone Repo | 克隆仓库 47 | ```bash 48 | git clone https://github.com/guobao2333/Python-HandyTool.git 49 | cd Python-HandyTool 50 | python script/FileEditor/replace_content.py -h 51 | ``` 52 | 53 | 如果你不想因为仓库的频繁更新而克隆仓库,请复制下方示例代码直接运行。 54 | #### Run Now | 直接运行 55 | ```bash 56 | curl -s "https://raw.githubusercontent.com/guobao2333/Python-HandyTool/main/script/FileEditor/replace_content.py" | python - -h 57 | ``` 58 | 59 | 如果需要增加其他参数,应使用空格分隔:`- -v -r` 60 | ********** 61 | 62 | 如果你还没有安装python,请继续阅读以下内容通过CLI(终端命令行)来安装运行环境。 63 | 64 | ### Install Runtime Environment | 安装运行环境 65 | 如果你已经安装python则可以跳过这部分。 66 | 67 |
68 | 点击查看 69 | 70 | > [!NOTE] 71 | > 现在几乎所有系统安装python3都会自带pip,所以你只需要安装python即可使用pip。 72 | > 如果你无法使用pip命令,你才需要另外安装。 73 | 74 | #### Linux 75 | - Ubuntu or Debian 76 | ```bash 77 | sudo apt-get install python3 78 | sudo apt-get install python-pip 79 | ``` 80 | 81 | - Android Termux 82 | ```bash 83 | pkg install python3 84 | pkg install python-pip 85 | ``` 86 | 87 | - CentOS 88 | ```bash 89 | sudo yum install python3 90 | ``` 91 | #### Other OS | 其他系统 92 | 请使用Google、Bing、Baidu、yandex等搜索引擎自行寻找答案。 93 | 94 | 95 | #### Check Installed | 验证安装 96 | ```bash 97 | python --version 98 | pip --version 99 | ``` 100 | 101 | 如果到这里没有报错,并输出了版本号的话, 102 | 恭喜你!已经安装成功了,享受代码吧! 103 |
104 | 105 | ## Contribute | 贡献 106 | 1. 点击上方`fork`仓库后,修改或添加你的代码 107 | 2. 点击`Pull requests`创建新的拉取请求后根据提示进行操作。 108 | 3. 提交合并请求后,接下来请等待代码审查,如果审查结束将会合并代码。 109 | 110 | 如果合并完成,恭喜你🎉您完成了对本项目的贡献!我们由衷的感谢为每个开源项目做出贡献的人,无论贡献多少。 111 | 112 | ## License | 许可证 113 | [Apache-2.0 license](./LICENSE) 114 | 115 | Copyright 2024 @shiguobaona 116 | 117 | Licensed under the Apache License, Version 2.0 (the "License"); 118 | you may not use this file except in compliance with the License. 119 | You may obtain a copy of the License at 120 | 121 | http://www.apache.org/licenses/LICENSE-2.0 122 | 123 | Unless required by applicable law or agreed to in writing, software 124 | distributed under the License is distributed on an "AS IS" BASIS, 125 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 126 | See the License for the specific language governing permissions and 127 | limitations under the License. 128 | 129 | ## Disclaimer | 免责声明 130 | 本仓库中的 Python 脚本仅供参考和学习使用,除 Apache-2.0 许可证外,本仓库**明确**不保证其***完整性、准确性、可靠性和安全性***。使用者应自行评估其**适用性**,并承担使用过程中产生的**任何风险和责任**。 131 | 132 | 作者不对因使用或无法使用本仓库中的任何脚本而导致的任何损失或损害负责。 133 | 134 | 使用者在使用本仓库中的任何脚本之前,请务必仔细阅读并理解本免责声明,以及仔细检查源代码中是否存在可能对您造成损失的功能。 135 | 136 | **特别提醒:** 137 | 138 | - 本仓库中的脚本可能包含**安全漏洞**,但您可以为此打开一个`issues`来报告此安全漏洞。 139 | - 本仓库中的脚本可能与特定环境或系统不兼容,请确保您的环境满足使用要求。 140 | - 本仓库中的脚本可能包含第三方库,您将需要自行安装。 141 | 142 | 如果您有任何疑问或建议,请打开一个新的`issues`。 -------------------------------------------------------------------------------- /script/FileEditor/line_merger.py: -------------------------------------------------------------------------------- 1 | ################# LICENCE START ################ 2 | # Copyright 2024 shiguobaona(https://github.com/guobao2333) 3 | 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ################# LICENCE END ################## 16 | 17 | import os,argparse 18 | 19 | def is_valid_path(parser, arg, file_or_folder=None): 20 | if file_or_folder == 'file': 21 | if not os.path.isfile(arg): 22 | parser.error(f"文件不存在:'{arg}'") 23 | elif file_or_folder == 'folder': 24 | if not os.path.isdir(arg): 25 | parser.error(f"目录无效:'{arg}'") 26 | else: 27 | if not os.path.exists(arg): 28 | parser.error(f"路径无效:'{arg}'") 29 | return arg 30 | 31 | def merge_files(file1, file2, output, direction): 32 | f1_n = os.path.basename(file1) 33 | f2_n = os.path.basename(file2) 34 | with open(file1, 'r') as f1, open(output, 'w') as f3: 35 | with open(file2, 'r') as f2: 36 | if direction == 'left': 37 | print(f"合并方向:{f2_n} + {f1_n}") 38 | for line1, line2 in zip(f1, f2): 39 | merged_line = line2.rstrip('\n') + line1 40 | f3.write(merged_line) 41 | elif direction == 'right': 42 | print(f"合并方向:{f1_n} + {f2_n}") 43 | for line1, line2 in zip(f1, f2): 44 | merged_line = line1.rstrip('\n') + line2 45 | f3.write(merged_line) 46 | path = os.path.abspath(output) 47 | print(f"合并完成!已保存到:{path}") 48 | 49 | parser = argparse.ArgumentParser(description='将 file2 中的内容逐行合并到 file1 并输出到新文件。') 50 | parser.add_argument('file1', type=lambda x: is_valid_path(parser, x, 'file'), help='文件1的路径') 51 | parser.add_argument('file2', type=lambda x: is_valid_path(parser, x, 'file'), help='文件2的路径') 52 | parser.add_argument('output', help='合并后的文件保存路径') 53 | parser.add_argument('-d', '--direction', choices=['left', 'right'], default='right', help='合并方向(左或右)默认为右(将file2的内容放到file1后面)') 54 | 55 | args = parser.parse_args() 56 | 57 | merge_files(args.file1, args.file2, args.output, args.direction) 58 | -------------------------------------------------------------------------------- /script/FileEditor/replace_content.py: -------------------------------------------------------------------------------- 1 | ################# LICENCE START ################ 2 | # Copyright 2024 shiguobaona(https://github.com/guobao2333) 3 | 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ################# LICENCE END ################## 16 | 17 | import os,re,sys,argparse,inquirer 18 | 19 | ################# CONFIG START ################# 20 | 21 | # 读取和写入时使用的编码 22 | read_encoding = 'utf-8' #默认utf-8 23 | write_encoding = 'utf-8' #默认utf-8 24 | 25 | # 默认覆盖文件 26 | overwrite_default = False #默认False 27 | 28 | # 提示是否覆盖 29 | # 如果为False,则在命令行未提供-o参数时提示 30 | overwrite_prompt = True #默认True 31 | 32 | # 不覆盖时为新创建的文件添加文件名后缀 33 | new_file_suffix = '_new' #file_new.txt 34 | 35 | # 默认替换次数 36 | replace_count = 0 #默认0为全部替换 37 | 38 | ################# CONFIG END ################### 39 | 40 | 41 | def in_file(file, match_content, replace_content, use_regex=False, overwrite=overwrite_default, replace_count=None): 42 | global match_num 43 | global no_match_num 44 | # 读取文件 45 | with open(file, 'r', encoding=read_encoding, errors='ignore') as f: 46 | #try: 47 | content = f.read() 48 | #except UnicodeDecodeError: 49 | #sys.exit(f"\033[31m编码错误\033[0m:你的文件与默认编码'{read_encoding}'不一致,请检查你的文件编码!\n 可以使用\033[33m-e '编码'\033[0m 参数指定编码。\n\n使用 '-h' 参数获得更多帮助。") 50 | # 狗屎编码判断😡💢有一个字符不是这个编码就要报错,我就草🤬这下只能忽略了……啊?unicode?哦那没事了…… 51 | 52 | # 使用正则表达式进行匹配和替换 53 | if use_regex: 54 | result = re.sub(match_content, replace_content, content, count=replace_count) 55 | else: 56 | result = content.replace(match_content, replace_content, replace_count) 57 | 58 | if result == content: 59 | if verbose: 60 | print(f"\033[33m未匹配到目标内容。\033[0m已跳过此文件:{file}") 61 | else: 62 | no_match_num += 1 63 | else: 64 | content = result 65 | 66 | # 覆盖操作 67 | if not overwrite: 68 | # 不覆盖创建新文件 69 | file_name_without_ext, ext = os.path.splitext(file) 70 | new_file = f"{file_name_without_ext}{new_file_suffix}{ext}" 71 | with open(new_file, 'w', encoding=write_encoding, errors='ignore') as f: 72 | f.write(content) 73 | if verbose: 74 | print(f"\033[32m替换完成!\033[0m新文件已保存到:{new_file}") 75 | else: 76 | match_num += 1 77 | else: 78 | # 直接覆盖 79 | with open(file, 'w', encoding=write_encoding, errors='ignore') as f: 80 | f.write(content) 81 | if verbose: 82 | print(f"\033[32m替换完成!\033[0m已替换的文件路径:{file}") 83 | else: 84 | match_num += 1 85 | 86 | 87 | def in_dir(dir, match_content, replace_content, regex, overwrite, replace_count): 88 | # 批量操作目录下的所有文件 89 | for root, dirs, files in os.walk(dir): 90 | for file in files: 91 | file_path = os.path.join(root, file) 92 | in_file(file_path, match_content, replace_content, regex, overwrite, replace_count) 93 | if not verbose: 94 | print(f"\033[32m已替换文件数\033[0m:{match_num}") 95 | print(f"\033[33m未匹配文件数\033[0m:{no_match_num}") 96 | 97 | 98 | # 命令行参数解析 99 | parser = argparse.ArgumentParser(description='替换指定文件或目录下的所有文件中的指定内容。') 100 | parser.add_argument('path', metavar='目录|文件', type=str, help='要替换的目录或文件的路径') 101 | parser.add_argument('match_content', metavar='匹配内容', type=str, help='要匹配的内容。开启正则表达式需使用引号包裹,否则可能会导致您的命令行出现异常') 102 | parser.add_argument('replace_content', metavar='替换内容', type=str, help="替换后的内容。建议使用引号包裹,匹配组应使用'\\x'代替原生正则的'$x'") 103 | parser.add_argument('replace_count', metavar='替换次数', nargs = '?', type=int, help='匹配内容在一个文件中的替换次数') 104 | parser.add_argument('-r', '--regex', action='store_true', help='在匹配内容中使用正则表达式进行匹配') 105 | parser.add_argument('-o', '--overwrite', action='store_true', help='直接覆盖旧文件而不是新建文件且不会提示') 106 | parser.add_argument('-v', '--verbose', action='store_true', help='输出冗长的详细提示信息') 107 | parser.add_argument('-e', '--encoding', action='store', help='改变读写文件的编码格式(默认UTF-8)') 108 | 109 | args = parser.parse_args() 110 | overwrite = args.overwrite 111 | path = args.path 112 | encoding = args.encoding 113 | match_content = rf"{args.match_content}" 114 | replace_content = rf"{args.replace_content}" 115 | 116 | if args.replace_count != None: 117 | replace_count = args.replace_count 118 | 119 | if encoding: 120 | # 哎呀懒得分了,自己改前面配置分开设置 121 | read_encoding = encoding 122 | write_encoding = encoding 123 | 124 | # 输出详细信息 125 | if args.verbose: 126 | verbose = True 127 | else: 128 | # 狗屎自增需要初始化😡💢 129 | verbose = False 130 | match_num = 0 131 | no_match_num = 0 132 | 133 | # 判断路径存在 134 | if not os.path.exists(path): 135 | sys.exit(f"\033[91m\033[1mERR:\033[0m 此路径\033[1m不存在\033[0m:\033[33m\"{path}\"\033[0m") 136 | # 使用绝对路径 137 | if not os.path.isabs(path): 138 | path = os.path.abspath(path) 139 | #print(f"绝对路径:{path}") 140 | 141 | # 提示是否覆盖旧文件 142 | if overwrite_prompt: 143 | if not args.overwrite: 144 | overwrite_question = [inquirer.List('overwrite', message="\033[33m是否覆盖旧文件而不是创建新文件?\033[0m", choices=[('是\033[31m(危险)\033[0m', True), ('否', False)], default=False)] 145 | overwrite_answer = inquirer.prompt(overwrite_question) 146 | try: 147 | overwrite = overwrite_answer['overwrite'] 148 | except TypeError: 149 | sys.exit() 150 | 151 | 152 | # 判断文件操作方式 153 | replace_fn = in_file if os.path.isfile(path) else in_dir 154 | replace_fn(path, match_content, replace_content, args.regex, overwrite, replace_count) 155 | -------------------------------------------------------------------------------- /script/ImageEditor/overlay_images.py: -------------------------------------------------------------------------------- 1 | ################# LICENCE START ################ 2 | # Copyright 2024 shiguobaona(https://github.com/guobao2333) 3 | 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ################# LICENCE END ################## 16 | 17 | import os, sys 18 | from PIL import Image 19 | 20 | ################# CONFIG START ################# 21 | 22 | # 默认输出路径 23 | default_output_path = './' 24 | 25 | # 输出文件名后缀 26 | new_file_suffix = '_合并' 27 | 28 | ################# CONFIG END ################### 29 | 30 | 31 | def overlay_images(img1_path, img2_path, output_path=None): 32 | img1 = Image.open(img1_path) 33 | img2 = Image.open(img2_path) 34 | 35 | # 将图片2覆盖到图片1上 36 | img1.paste(img2, (0, 0), img2) 37 | 38 | # 使用默认路径 39 | if not output_path: 40 | # 获取图片1的文件名和后缀 41 | img1_filename = os.path.splitext(os.path.basename(img1_path))[0] 42 | img1_ext = os.path.splitext(os.path.basename(img1_path))[1] 43 | 44 | output_filename = f"{img1_filename}{new_file_suffix}{img1_ext}" 45 | img1_dir = os.path.dirname(img1_path) 46 | output_path = os.path.join(img1_dir, output_filename) 47 | img1.save(output_path) 48 | print("图片合并完成,已保存至:{output_path}") 49 | 50 | img1_path = sys.argv[1] 51 | img2_path = sys.argv[2] 52 | output_path = default_output_path if not len(sys.argv) > 3 else sys.argv[3] 53 | 54 | overlay_images(img1_path, img2_path, output_path) -------------------------------------------------------------------------------- /script/ImageEditor/repeat_images.py: -------------------------------------------------------------------------------- 1 | ################# LICENCE START ################ 2 | # Copyright 2024 shiguobaona(https://github.com/guobao2333) 3 | 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | ################# LICENCE END ################## 16 | 17 | import os, sys 18 | from PIL import Image 19 | 20 | ################# CONFIG START ################# 21 | 22 | # 默认输出路径 23 | output_path = './' 24 | 25 | # 是否横向拼接 26 | horizontal = False 27 | # 默认拼接方向 28 | #axis = 0|1 29 | 30 | ################# CONFIG END ################### 31 | 32 | 33 | def join_images(img_path, repeat_times, output_path=None, horizontal=False): 34 | # 打开输入图片 35 | with Image.open(img_path).convert("RGBA") as img: 36 | width, height = img.size 37 | if horizontal: 38 | new_width = width * repeat_times 39 | new_height = height 40 | else: 41 | new_width = width 42 | new_height = height * repeat_times 43 | 44 | # 创建新的空白图像 45 | new_image = Image.new('RGBA', (new_width, new_height)) 46 | 47 | # 拼接图片 48 | for i in range(repeat_times): 49 | if horizontal: 50 | new_image.paste(img, (i * width, 0)) 51 | else: 52 | new_image.paste(img, (0, i * height)) 53 | 54 | # 确定保存路径和文件名 55 | if output_path is None: 56 | output_dir = os.path.dirname(img_path) # 默认输出图片路径 57 | output_filename = os.path.splitext(os.path.basename(img_path))[0] + '_拼接' + os.path.splitext(img_path)[1] 58 | output_path = os.path.join(output_dir, output_filename) 59 | output_dirname = os.path.dirname(output_path) 60 | # 检查输出路径是否存在 61 | elif not os.path.exists(output_dirname): 62 | sys.exit("输出路径不存在") 63 | 64 | # 保存结果图片 65 | new_image.save(output_path) 66 | print("图片拼接完成,已保存至:" + output_path) 67 | 68 | if __name__ == '__main__': 69 | # 获取命令行参数 70 | if len(sys.argv) < 3: 71 | print("缺少参数!请参考下方命令格式:") 72 | print("python 脚本.py <图片路径> <重复次数> [输出路径] [横向 | 纵向(默认):]") 73 | else: 74 | img_path = sys.argv[1] 75 | if not os.path.isfile(img_path): 76 | sys.exit("目标图片不存在") 77 | 78 | # 检查重复次数是否为整数 79 | if not sys.argv[2].isdigit(): 80 | sys.exit("重复次数不是正整数") 81 | else: 82 | repeat_times = int(sys.argv[2]) 83 | # 判断拼接方向参数 84 | if len(sys.argv) > 3: 85 | if sys.argv[3].lower() in ['true', 'false']: 86 | horizontal = True if sys.argv[3].lower() == 'true' else False 87 | else: 88 | output_path = sys.argv[3] 89 | join_images(img_path, repeat_times, output_path, horizontal) -------------------------------------------------------------------------------- /script/README.md: -------------------------------------------------------------------------------- 1 | # List | 所有脚本 2 | ## Files Editor | 通用文件编辑 3 | - 正则批量替换内容:[查看](FileEditor/replace_content.py) 4 | - 按行合并内容:[查看](FileEditor/line_merger.py) 5 | 6 | ## Images Editor | 图片编辑 7 | - 覆盖图片:[查看](ImageEditor/overlay_images.py) 8 | - 重复拼接图片:[查看](ImageEditor/repeat_images.py) 9 | --------------------------------------------------------------------------------