From 163962367513bbe16294a2f60ebe0c578f6627c4 Mon Sep 17 00:00:00 2001 From: CoderWanFeng <1957875073@qq.com> Date: Thu, 13 Oct 2022 20:37:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1=E8=A1=8C=E5=91=BD=E4=BB=A4=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=9B=BE=E7=89=87=F0=9F=91=89https://mp.weixin.qq.com?= =?UTF-8?q?/s/P6pRm1VX8bGYepC8O4Bt4Q?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- office/api/image.py | 4 +- office/core/ExcelType.py | 147 ----------------------------- office/core/FileType.py | 140 --------------------------- office/core/ImageType.py | 140 --------------------------- office/core/MarkdownType.py | 98 ------------------- office/core/PDFType.py | 142 ---------------------------- office/core/PPTType.py | 33 ------- office/core/SearchByContentType.py | 22 ----- office/core/ToolsType.py | 95 ------------------- office/core/VideoType.py | 20 ---- office/core/WeChatType.py | 9 -- office/core/WordType.py | 40 -------- setup.cfg | 2 +- tests/test_unit/test_image.py | 2 +- 14 files changed, 4 insertions(+), 890 deletions(-) delete mode 100644 office/core/ExcelType.py delete mode 100644 office/core/FileType.py delete mode 100644 office/core/ImageType.py delete mode 100644 office/core/MarkdownType.py delete mode 100644 office/core/PDFType.py delete mode 100644 office/core/PPTType.py delete mode 100644 office/core/SearchByContentType.py delete mode 100644 office/core/ToolsType.py delete mode 100644 office/core/VideoType.py delete mode 100644 office/core/WeChatType.py delete mode 100644 office/core/WordType.py diff --git a/office/api/image.py b/office/api/image.py index 903fc90..6b2bdfd 100644 --- a/office/api/image.py +++ b/office/api/image.py @@ -36,8 +36,8 @@ def img2Cartoon(path, client_api='OVALewIvPyLmiNITnceIhrYf', client_secret='rpBQ # @except_dec() -def down4img(url, output_name='down4img', type='jpg'): - poimage.down4img(url, output_name, type) +def down4img(url, output_path='.', output_name='down4img', type='jpg'): + poimage.down4img(url, output_path, output_name, type) # mainImage.down4img(url, output_name, type) diff --git a/office/core/ExcelType.py b/office/core/ExcelType.py deleted file mode 100644 index 1679853..0000000 --- a/office/core/ExcelType.py +++ /dev/null @@ -1,147 +0,0 @@ -# import search4file -# from faker import Faker -# import pandas as pd -# from alive_progress import alive_bar -# -# from office.lib.excel import SplitExcel -# from office.lib.utils import pandas_mem -# import os -# from pathlib import Path -# from openpyxl import load_workbook -# import warnings -# from tqdm import tqdm -# -# # 忽略waring警告 -# warnings.filterwarnings("ignore") -# -# -# class MainExcel(): -# -# def fake2excel(self, columns, rows, path, language): -# """ -# @Author & Date : CoderWanFeng 2022/5/13 0:12 -# @Desc : columns:list,每列的数据名称,默认是名称 -# rows:多少行,默认是1 -# language:什么语言,可以填english,默认是中文 -# path:输出excel的位置,有默认值 -# """ -# # 可以选择英语 -# if language.lower() == 'english': -# language = 'en_US' -# else: -# language = 'zh_CN' -# # 开始造数 -# fake = Faker(language) -# excel_dict = {} -# with alive_bar(len(columns) * rows) as bar: -# for column in columns: -# excel_dict[column] = list() -# # excel_dict[column] = map(lambda x: eval('fake.{func}()'.format(func=x)), [column] * rows) # 使用map,会报错 -# while len(excel_dict[column]) < rows: -# excel_dict[column].append(eval('fake.{func}()'.format(func=column))) -# bar() -# # 用pandas,将模拟数据,写进excel里面 -# writer = pd.ExcelWriter(path) -# data = pd.DataFrame(excel_dict) -# data = pandas_mem.reduce_pandas_mem_usage(data) -# data.to_excel(writer, index=False) -# # writer.save() -# writer.close() -# -# def merge2excel(self, dir_path, output_file, xlsxSuffix=".xlsx"): -# """ -# :param dir_path: 存放excel文件的位置 -# :param output_file: 输出合并后excel文件的位置 -# :return: 没有返回值 -# """ -# if not output_file.endswith(xlsxSuffix): -# raise Exception(f'您自定义的输出文件名,不是以{xlsxSuffix}结尾的') -# file_path_dict = self.getfile(dir_path) # excel文件所在的文件夹 -# try: -# writer = pd.ExcelWriter(output_file) # 合并后的excel名称 -# except PermissionError: -# raise Exception(f'小可爱,你的输出文件,是不是上次打开了没关闭呀?这是你自己指定的输出文件名称:{output_file}') -# for file, path in file_path_dict.items(): -# if file.endswith("xlsx"): -# df = pd.read_excel(path) -# if file.endswith("csv"): -# df = pd.read_csv(path) -# df.to_excel(writer, sheet_name=file.split('.')[0], index=False) -# print(f'您指定的Excel文件已经合并完毕,合并后的文件名是{output_file}') -# writer.save() -# -# def getfile(self, dirpath): -# path = Path(dirpath) -# file_path_dict = {} -# for root, dirs, files in os.walk(dirpath): -# for file in files: -# if file.endswith("xlsx") or file.endswith("csv"): -# file_path_dict[file] = (path / file) -# return file_path_dict -# -# def sheet2excel(self, file_path): -# # 先读取一次文件,获取sheet表的名称 -# -# origin_excel = load_workbook(filename=file_path) # 读取原excel文件 -# origin_sheet_names = origin_excel.sheetnames # 获取sheet的名称 -# print(f'一共有{len(origin_sheet_names)}个sheet,名称分别为:{origin_sheet_names}') -# print('拆分开始') -# -# if len(origin_sheet_names) > 1: # 如果sheetnames小于1,报错:该文件不需要拆分 -# -# for j in tqdm(range(len(origin_sheet_names))): -# -# wb = load_workbook(filename=file_path) # 再读取一次文件,由于每次删除后需要保存一次,所以不能与上一次一样 -# sheet = wb[origin_sheet_names[j]] -# wb.copy_worksheet(sheet) -# -# new_filename = origin_sheet_names[j] + '.xlsx' # 新建一个sheet命名的excel文件 -# -# for i in tqdm(range(len(origin_sheet_names))): -# sheet1 = wb[origin_sheet_names[i]] -# wb.remove(sheet1) -# -# wb.save(filename=new_filename) -# -# # 由于使用copy_worksheet后,sheet表名有copy字段,这里做个调整 -# -# new = load_workbook(filename=new_filename) -# news = new.active -# news.title = origin_sheet_names[j] -# new.save(filename=new_filename) -# print('拆分结束') -# else: -# raise Exception(f"你的文件只有一个sheet,难道还要拆分吗?我做不到啊~~~,你的文件名{file_path}") -# -# def merge2sheet(self, dir_path, output_sheet_name: str, output_excel_name): -# # 多个excel文件,合并到一个sheet里面 -# for root, dirs, files in os.walk(dir_path): -# path = Path(dir_path) -# print(f'正在合并的文件有:{files}') -# print(f'合并后的文件名是:{output_excel_name}') -# print(f'合并后的sheet名是:{output_sheet_name}') -# df_list = [] -# for file in files: -# if file.endswith("xlsx") or file.endswith("xls"): -# excel_path = (path / file) -# df_list.append(pd.read_excel(excel_path)) -# res = pd.concat(df_list) -# res.to_excel( -# (path / (output_excel_name + '.xlsx')), -# sheet_name=output_sheet_name, -# index=False # 不保留index -# ) -# -# pass -# -# def find_excel_data(self, search_key, target_dir): -# """ -# 检索指定目录下的excel文件和过滤 -# 参数: -# search_key:检索的关键词 -# target_dir:目标文件夹 -# """ -# search4file.find_excel_data(search_key, target_dir) -# -# def split_excel_by_column(self, filepath, column, worksheet_name): -# SplitExcel.split_excel_by_column(filepath, column, worksheet_name) diff --git a/office/core/FileType.py b/office/core/FileType.py deleted file mode 100644 index 246f9cb..0000000 --- a/office/core/FileType.py +++ /dev/null @@ -1,140 +0,0 @@ -# import os -# from alive_progress import alive_bar -# -# import pathlib -# import openpyxl -# -# from office.lib.utils.time_utils import time_count_dec -# -# -# class MainFile(): -# -# def replace4filename(self, path, del_content, replace_content): -# """ -# :param path: 需要替换的文件夹路径 -# :param del_content: 需要删除/替换的内容 -# :param replace_content: 替换后的内容,可以不填 = 直接删除 -# :return: -# """ -# # 获取该目录下所有文件,存入列表中;不包含子文件夹 -# fileList = os.listdir(path) -# work_count = 0 -# with alive_bar(len(fileList)) as bar: -# for old_file_name in fileList: # 依次读取该路径下的文件名 -# bar() # 进度条 -# if del_content in old_file_name: -# -# if replace_content: -# new_file_name = old_file_name.replace(del_content, replace_content) -# else: -# new_file_name = old_file_name.replace(del_content, '') -# os.rename(path + os.sep + old_file_name, path + os.sep + new_file_name) -# work_count = work_count + 1 -# print("当前目录下,共有{}个文件/文件夹,本次运行共进行了{}个文件/文件夹的重命名".format(len(fileList), work_count)) -# -# @time_count_dec -# def file_name_insert_content(self, file_path, insert_position: int, insert_content: str): -# """ -# -# :param file_path: 文件存放路径 -# :param insert_position: 插入位置(内容将插入在此之后,如果输入位置大于文件主名长度将插入在末尾) -# :param insert_content: 插入内容 -# """ -# Path = pathlib.Path(file_path).resolve() -# if Path.is_dir(): -# FileNameList = list(Path.glob("*")) # 获取该路径下的文件列表 -# for FileName in FileNameList: -# if FileName.is_file(): # 判断是否为文件,只对文件进行操作 -# FileNameExtension = "".join(list(FileName.suffixes)) -# FileNameRoot = FileName.name.replace(FileNameExtension, "") -# # 分离文件主名和扩展名,防止对扩展名进行操作 -# FileNameFormer = FileNameRoot[:insert_position:] -# FileNameLatter = FileNameRoot[insert_position::] -# # 拆分文件主名 -# NewFileName = FileNameFormer + insert_content + FileNameLatter + FileNameExtension # 合并文件名 -# if not Path.joinpath(NewFileName).is_file(): -# FileName.rename(Path.joinpath(NewFileName)) -# else: -# print(f"该目录下已存在名为{NewFileName}的文件,请检查!") -# else: -# print("请输入文件夹路径") -# -# @time_count_dec -# def file_name_add_prefix(self, file_path, prefix_content: str): -# """ -# -# :param file_path: 文件存放路径 -# :param prefix_content: 前缀内容 -# """ -# Path = pathlib.Path(file_path).resolve() -# if Path.is_dir(): -# FileNameList = list(Path.glob("*")) # 获取该路径下的文件列表 -# for FileName in FileNameList: -# if FileName.is_file(): # 判断是否为文件,只对文件进行操作 -# NewFileName = prefix_content + FileName.name # 合并文件名 -# if not Path.joinpath(NewFileName).is_file(): -# FileName.rename(Path.joinpath(NewFileName)) -# else: -# print(f"该目录下已存在名为{NewFileName}的文件,请检查!") -# else: -# print("请输入文件夹路径") -# -# @time_count_dec -# def file_name_add_postfix(self, file_path, postfix_content: str): -# """ -# -# :param file_path: 文件存放路径 -# :param postfix_content: 后缀内容 -# """ -# Path = pathlib.Path(file_path).resolve() -# if Path.is_dir(): -# FileNameList = list(Path.glob("*")) # 获取该路径下的文件列表 -# for FileName in FileNameList: -# if FileName.is_file(): # 判断是否为文件,只对文件进行操作 -# FileNameExtension = "".join(list(FileName.suffixes)) -# FileNameRoot = FileName.name.replace(FileNameExtension, "") -# # 分离文件主名和扩展名 -# NewFileName = FileNameRoot + postfix_content + FileNameExtension # 合并文件名 -# if not Path.joinpath(NewFileName).is_file(): -# FileName.rename(Path.joinpath(NewFileName)) -# else: -# print(f"该目录下已存在名为{NewFileName}的文件,请检查!") -# else: -# print("请输入文件夹路径") -# -# def search_specify_type_file(self, file_path, file_type: str): -# ''' -# -# :param file_path:目标路径 -# :param file_type:需要查找的文件类型 -# ''' -# print("开始查找") -# i = 0 # 计数变量 -# file_path = pathlib.Path(file_path).resolve() -# if file_path.is_dir(): -# file_name_list = list(file_path.glob("**/*")) # 获取该路径下的文件列表 -# for file_name in file_name_list: -# file_name_extension = "".join(list(file_name.suffixes)) -# if file_name_extension == file_type: -# print(f"{file_name.name},{file_name.parent}") -# i = i + 1 -# print(f"查找完成,共找到{i}个文件") -# else: -# print("请输入文件夹路径") -# -# def output_file_list_to_excel(self,dir_path: str): -# """ -# :param dir_path: 需要生成文件列表的目录 -# """ -# dir_path = pathlib.Path(dir_path).resolve() -# if dir_path.is_dir(): -# dir_path_list = list(dir_path.glob("**/*")) -# output_excel = openpyxl.Workbook() -# output_excel_sheet = output_excel.active -# output_excel_sheet.append(["完整路径", "文件所在路径", "文件名"]) -# for file_path in dir_path_list: -# if file_path.is_file(): -# output_excel_sheet.append([str(file_path), str(file_path.parent), str(file_path.name)]) -# output_excel.save(dir_path.joinpath("本目录文件列表.xlsx")) -# else: -# print("请输入正确的文件路径!") \ No newline at end of file diff --git a/office/core/ImageType.py b/office/core/ImageType.py deleted file mode 100644 index 51d364d..0000000 --- a/office/core/ImageType.py +++ /dev/null @@ -1,140 +0,0 @@ -# import os -# # from lib.image import add_watermark_service -# # 生成词云需要使用的类库 -# from PIL import Image -# from alive_progress import alive_bar -# -# from office.lib.image import add_watermark_service -# import base64 -# import requests -# -# -# class MainImage(): -# -# #TODO:自动生成gif -# def image2gif(self): -# im = Image.open("1.jpg") -# images = [] -# images.append(Image.open('2.jpg')) -# images.append(Image.open('3.jpg')) -# im.save('gif.gif', save_all=True, append_images=images, loop=1, duration=1, comment=b"aaabb") -# -# # from wordcloud import WordCloud -# # import jieba -# -# # def txt2wordcloud(filename, color="white", result_file="your_wordcloud.png"): -# # """ -# # @Author & Date : CoderWanFeng 2022/4/28 9:26 -# # @Desc : 生成词云的代码,可以添加更多个性化功能 -# # @Return : -# # """ -# # with open(filename, encoding='utf8') as fp: -# # text = fp.read() -# # # 将读取的中文文档进行分词 -# # # 接收分词的字符串 -# # word_list = jieba.cut(text) -# # # 分词后在单独个体之间加上空格 -# # cloud_text = " ".join(word_list) -# # -# # # 生成wordcloud对象 -# # wc = WordCloud(background_color=color, -# # max_words=200, -# # min_font_size=15, -# # max_font_size=50, -# # width=400, -# # font_path="msyh.ttc", # 默认的简体中文字体,没有会报错 -# # ) -# # wc.generate(cloud_text) -# # wc.to_file(result_file) -# -# def add_watermark(self, file, mark, out="output", color="#8B8B1B", size=30, opacity=0.15, space=75, angle=30): -# """ -# @Author & Date : demo 2022/5/6 14:33 -# @Desc : 给图片添加水印 -# @Return : 添加了水印的图片,输出到out指定的文件夹 -# """ -# if os.path.isdir(file): -# names = os.listdir(file) -# with alive_bar(len(names)) as bar: -# for name in names: -# bar() -# image_file = os.path.join(file, name) -# add_watermark_service.add_mark2file(image_file, mark, out, color, size, opacity, space, angle) -# else: -# add_watermark_service.add_mark2file(file, mark, out, color, size, opacity, space, angle) -# -# def get_access_token(self, client_api, client_secret): -# -# # 获取token的API -# url = 'https://aip.baidubce.com/oauth/2.0/token' -# # 获取access_token需要的参数 -# params = { -# # 固定参数 -# 'grant_type': 'client_credentials', -# # 必选参数,传入你的API Key -# 'client_id': client_api, -# # 必选参数,传入你的Secret Key -# 'client_secret': client_secret -# } -# # 发送请求,获取响应数据 -# -# response = requests.post(url, params) -# # 将响应的数据转成字典类型,然后取出access_token -# access_token = eval(response.text)['access_token'] -# # 将access_token返回 -# return access_token -# -# def img2Cartoon(self, path, client_api, client_secret): -# print('=' * 30) -# print('正在进行动漫头像的转换') -# print('本仓库的视频教程:http://t.cn/A6aAvu47') -# print('这个接口调用的是百度AI平台的免费试用接口(200次),如果代码报错,大概率是试用次数没有了') -# print('获取免费使用次数的教程,我整理在这个文档里了:https://python-office.com/office/image.html') -# print('=' * 30) -# -# # 头像动漫化的API -# url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime' -# # 以二进制的方式读取原始图片 -# origin_im = open(path, 'rb') -# # 将图片进行base64编码 -# path = base64.b64encode(origin_im.read()) -# # 关闭原图片 -# origin_im.close() -# # 请求的headers信息,固定写法 -# headers = {'content-type': 'application/x-www-form-urlencoded'} -# # 请求的参数 -# params = { -# # 开始获取的access_token -# 'access_token': self.get_access_token(client_api, client_secret), -# # 图片的base64编码 -# 'image': path, -# } -# # 发送请求 -# response = requests.post(url, data=params, headers=headers) -# # 对响应结果进行处理 -# if response: -# # 打开一个文件 -# f = open('result.jpg', 'wb') -# try: -# # 获取动漫头像 -# anime = response.json()['image'] -# except: -# raise Exception('你没有开通百度AI账号,错误原因以及【免费】开通方式,见:https://mp.weixin.qq.com/s/5Eyk2j20jzSaVcr1DTsfvw') -# # 对返回的头像进行解码 -# anime = base64.b64decode(anime) -# # 将头像写入文件当中 -# f.write(anime) -# f.close() -# print('*' * 20 + "{}".format('动漫头像名称:result.jpg') + '*' * 20) -# print('*' * 20 + "{}".format('您的动漫头像转换完毕,请在本代码运行的文档里查看') + '*' * 20) -# -# def down4img(self, url, output_name, type): -# """ -# 下载指定url的一张图片,支持所有格式:jpg\png\gif .etc -# """ -# response = requests.get(url, stream=True) -# with open('.'.join((output_name, type)), 'wb') as output_img: -# for chunk in response: -# output_img.write(chunk) -# output_img.close() -# print(f"下载成功,图片名称:{'.'.join((output_name, type))}") diff --git a/office/core/MarkdownType.py b/office/core/MarkdownType.py deleted file mode 100644 index c9fdb6a..0000000 --- a/office/core/MarkdownType.py +++ /dev/null @@ -1,98 +0,0 @@ -# import base64 -# import pathlib -# -# -# class MainMarkdown(): -# -# def markdown_link_image_to_base64(self, markdown_path): -# markdown_path = pathlib.Path(markdown_path) -# if markdown_path.is_file() and markdown_path.suffix == ".md": -# i = 0 # 图片计数变量 -# image_base64_list = [] # 图片base64编码列表 -# markdown_file = open(markdown_path, encoding="utf-8") -# new_markdown_file = open(markdown_path.parent.joinpath(markdown_path.name[:-3:] + "(base64).md"), "x", -# encoding="utf-8") -# markdown_file = markdown_file.readlines() -# for line in markdown_file: -# # 按行遍历文件 -# if line[:2:] == "![": -# # 如果该行有链接图片 -# i = i + 1 -# this_image_path = pathlib.Path(line[line.rfind("(") + 1:line.rfind(")"):]) -# if not this_image_path.is_absolute(): -# # Python文件和markdown文件通常不在同一路径,相对路径基准不同,故需要判断链接图片路径是否为绝对路径。 -# this_image_path = markdown_path.parent.joinpath(this_image_path).resolve() -# this_image_file = open(this_image_path, "rb") -# this_image_base64_data = base64.b64encode(this_image_file.read()) -# # 对链接的图片进行base64编码 -# line = line[:line.index("("):] + f"[image{str(i)}]" + "\n" -# image_base64_list.append( -# f"[image{str(i)}]: " + "data:image/" + this_image_path.suffix + ";base64," + str( -# this_image_base64_data)[2:-1:] + "\n") -# new_markdown_file.writelines(line) -# for i in image_base64_list: -# # 遍历存放base64编码的列表 -# if markdown_file[-1] != "\n": -# new_markdown_file.writelines("\n") -# # 判断最后一行是否为空行,如果不是则写入换行符 -# new_markdown_file.writelines(i) -# # 将图片的base64编码写入新文件 -# elif not markdown_path.is_file(): -# print("markdown文件路径输入有误,请检查!") -# elif markdown_path.suffix != ".md": -# print("请输入.md文件的路径!") -# -# def check_local_dir_image_link_markdown(self, markdown_path, image_path): -# """ -# :param markdown_path: markdown文件路径 -# :param image_path: 本地图片存放路径 -# """ -# markdown_path = pathlib.Path(markdown_path) -# image_path = pathlib.Path(image_path) -# # 转换路径为对象 -# if markdown_path.is_file() and image_path.is_dir(): -# print("检查中……") -# markdown_image_list = [] # markdown文件中链接的图片列表 -# markdown_file = open(markdown_path, encoding="utf-8") -# markdown_file = markdown_file.readlines() -# for line in markdown_file: -# # 按行遍历文件 -# if line[:2:] == "![": -# # 如果该行有链接图片 -# this_image_path = pathlib.Path(line[line.rfind("(") + 1:line.rfind(")"):]) -# # 提取链接图片路径 -# if this_image_path.is_absolute(): -# # Python文件和markdown文件通常不在同一路径,相对路径基准不同,故需要判断链接图片路径是否为绝对路径。如果是则直接添加进列表,如果不是则要拼接上markdown所在的路径 -# markdown_image_list.append(this_image_path) -# else: -# this_image_path = markdown_path.parent.joinpath(this_image_path).resolve() -# markdown_image_list.append(this_image_path) -# local_dir_image_list = [] -# for i in list(image_path.glob("**/*")): -# local_dir_image_list.append(i.resolve()) -# markdown_image_set = set(markdown_image_list) -# local_dir_image_set = set(local_dir_image_list) -# if local_dir_image_set ^ markdown_image_set != ( -# local_dir_image_set ^ markdown_image_set) & local_dir_image_set: -# """ -# 如果有文件夹中有图片未被链接到markdown中或markdown中链接了其他路径的图片,则local_dir_image_set ^ markdown_image_set不为空 -# (local_dir_image_set ^ markdown_image_set) & local_dir_image_set为未被链接到markdown中的图片 -# 如果两个集合不相等,则代表markdown中链接了其他路径的图片 -# """ -# print("文档中有链接其他路径的图片,请检查! ") -# other_dir_image_list = list((local_dir_image_set ^ markdown_image_set) & markdown_image_set) -# print("其他路径图片列表:") -# for i in other_dir_image_list: -# print(str(i)) -# print() -# if (local_dir_image_set ^ markdown_image_set) & local_dir_image_set != set(): -# print("未被链接图片列表:") -# for i in local_dir_image_set ^ markdown_image_set & local_dir_image_set: -# # 打印未被链接图片名列表 -# print(i) -# else: -# print(f"{image_path.resolve()}内无未被链接的图片") -# elif not markdown_path.is_file(): -# print("markdown文件路径输入有误,请检查!") -# elif not image_path.is_dir(): -# print("图片存放路径输入有误,请检查!") diff --git a/office/core/PDFType.py b/office/core/PDFType.py deleted file mode 100644 index 35959a0..0000000 --- a/office/core/PDFType.py +++ /dev/null @@ -1,142 +0,0 @@ -# from fpdf import FPDF -# from office.lib.pdf import add_watermark_service -# import pikepdf -# from PyPDF2 import PdfFileReader, PdfFileWriter, PdfReader, PdfWriter -# from pdf2docx import Converter -# import os -# from pathlib import Path -# import fitz # fitz就是pip install PyMuPDF -# -# from tqdm import tqdm -# -# -# class MainPDF(): -# -# def add_watermark(self): -# pdf_file_in = input("请输入需要添加水印的文件位置:") # 需要添加水印的文件 -# Watermark_Str = input("请输入需要添加的水印内容:") -# print('=' * 20) -# print('正在按要求,给你的PDF文件添加水印,请让程序飞一会儿~') -# print('=' * 20) -# pdf_file_mark = 'watermark.pdf' # 水印文件 -# add_watermark_service.create_watermark(str(Watermark_Str)) -# pdf_file_out = '添加了水印的文件.pdf' # 添加PDF水印后的文件 -# add_watermark_service.pdf_add_watermark(pdf_file_in, pdf_file_mark, pdf_file_out) -# print("水印添加结束,请打开电脑上的这个位置,查看结果文件:{path}".format(path=os.getcwd())) -# -# def add_watermark_by_parameters(self, pdf_file, mark_str, output_file_name): -# """ -# 给pdf添加水印,需要参数的版本 -# """ -# # pdf_file_in = input("请输入需要添加水印的文件位置:") # 需要添加水印的文件 -# # Watermark_Str = input("请输入需要添加的水印内容:") -# print('=' * 20) -# print('正在按要求,给你的PDF文件添加水印,请让程序飞一会儿~') -# print('=' * 20) -# pdf_file_mark = 'watermark.pdf' # 水印文件 -# add_watermark_service.create_watermark(str(mark_str)) -# pdf_file_out = output_file_name # '添加了水印的文件.pdf' # 添加PDF水印后的文件 -# add_watermark_service.pdf_add_watermark(pdf_file, pdf_file_mark, pdf_file_out) -# print("水印添加结束,请打开电脑上的这个位置,查看结果文件:{path}".format(path=os.getcwd())) -# -# def file2pdf(self, file_type, path, res_pdf='file2pdf.pdf'): -# if file_type == 'txt': -# pdf = FPDF() -# pdf.add_page() # Add a page -# pdf.set_font("Arial", size=15) # set style and size of font -# f = open(path, "r") # open the text file in read mode -# # insert the texts in pdf -# for x in f: -# pdf.cell(50, 5, txt=x, ln=1, align='C') -# # pdf.output("path where you want to store pdf file\\file_name.pdf") -# pdf.output(res_pdf) -# -# def pdf2docx(self, file_path, output_path): -# word_name = os.path.basename(file_path)[:-4] + '.docx' -# word_path = Path(output_path) / word_name -# cv = Converter(file_path) -# cv.convert(word_path) -# cv.close() -# -# # 合并pdf -# def merge2pdf(self, one_by_one, output): -# """ -# @Author & Date : CoderWanFeng 2022/5/16 23:33 -# @Desc : merge_pdfs(paths=['开篇词.pdf', '中国元宇宙白皮书 (送审稿).pdf'], output='merge.pdf') -# """ -# pdf_writer = PdfWriter() -# -# for path in one_by_one: -# pdf_reader = PdfReader(path) -# # for page in tqdm(range(pdf_reader.getNumPages())): -# for page in tqdm(range(len(pdf_reader.pages))): -# # 把每张PDF页面加入到这个可读取对象中 -# # pdf_writer.addPage(pdf_reader.getPage(page)) -# pdf_writer.add_page(pdf_reader.pages[page]) -# -# # 把这个已合并了的PDF文档存储起来 -# with open(output, 'wb') as out: -# pdf_writer.write(out) -# -# # PDF加密 -# def encrypt4pdf(self, path, password, res_pdf='encrypt.pdf'): -# """ -# @Author & Date : CoderWanFeng 2022/5/9 18:27 -# @Desc : path: 存放文件的路径 -# password: 你的密码 -# res_pdf: 结果文件的名称 ,可以为空,默认是:encrypt.pdf -# """ -# pdf = pikepdf.open(path) -# pdf.save(res_pdf, encryption=pikepdf.Encryption(owner=password, user=password, R=4)) -# pdf.close() -# -# # PDF解密 -# def decrypt4pdf(self, path, password, res_pdf='decrypt.pdf'): -# pdf = pikepdf.open(path, password=password) -# pdf.save(res_pdf) -# pdf.close() -# -# # def pdf2imgs(self, pdf_path: str, out_dir=".") -> None: -# # print('PDF开始转换,你可以加入交流群唠唠嗑:http://www.python4office.cn/wechat-group/') -# # pdfDoc = fitz.open(pdf_path) -# # if pdfDoc.pageCount > 50: -# # print('少年,你的PDF页数有点多哟,请耐心等待~') -# # for pg in range(pdfDoc.pageCount): -# # page = pdfDoc[pg] -# # rotate = int(0) -# # # 每个尺寸的缩放系数为1.3,这将为我们生成分辨率提高2.6的图像。 -# # # 此处若是不做设置,默认图片大小为:792X612, dpi=96 -# # zoom_x = 1.33333333 # (1.33333333-->1056x816) (2-->1584x1224) -# # zoom_y = 1.33333333 -# # mat = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate) -# # pix = page.getPixmap(matrix=mat, alpha=False) -# -# # if not os.path.exists(out_dir): # 判断存放图片的文件夹是否存在 -# # os.makedirs(out_dir) # 若图片文件夹不存在就创建 -# -# # pix.writePNG(out_dir + '/' + 'images_%s.png' % pg) # 将图片写入指定的文件夹内 -# # print(f'PDF转换Image完成,图片在你指定的output文件夹{out_dir},如果没有指定,默认是PDF同一个文件夹') -# -# def pdf2imgs(self, pdf_path: str, out_dir=".") -> None: -# print('PDF开始转换,你可以加入交流群唠唠嗑:http://www.python4office.cn/wechat-group/') -# pdfDoc = fitz.open(pdf_path) -# if pdfDoc.page_count > 50: -# print('少年,你的PDF页数有点多哟,请耐心等待~') -# for pg in tqdm(range(pdfDoc.page_count)): -# page = pdfDoc[pg] -# rotate = int(0) -# # 每个尺寸的缩放系数为1.3,这将为我们生成分辨率提高2.6的图像。 -# # 此处若是不做设置,默认图片大小为:792X612, dpi=96 -# zoom_x = 1.33333333 # (1.33333333-->1056x816) (2-->1584x1224) -# zoom_y = 1.33333333 -# mat = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate) -# pix = page.get_pixmap(matrix=mat, alpha=False) -# -# if not os.path.exists(out_dir): # 判断存放图片的文件夹是否存在 -# os.makedirs(out_dir) # 若图片文件夹不存在就创建 -# -# pix.save(out_dir + '/' + 'images_%s.png' % pg) # 将图片写入指定的文件夹内 -# print(f'PDF转换Image完成,图片在你指定的output文件夹{out_dir},如果没有指定,默认是PDF同一个文件夹') -# -# def add_img_watermark(self, pdf_file_in, pdf_file_mark, pdf_file_out): -# add_watermark_service.pdf_add_watermark(pdf_file_in, pdf_file_mark, pdf_file_out) diff --git a/office/core/PPTType.py b/office/core/PPTType.py deleted file mode 100644 index a3419b8..0000000 --- a/office/core/PPTType.py +++ /dev/null @@ -1,33 +0,0 @@ -# import os -# import time -# -# from office.lib.ppt.ppt2pdf_service import ppt2pdf_single -# -# -# class MainPPT(): -# -# def ppt2pdf(self, path): -# """ -# @Author & Date : CoderWanFeng 2022/5/9 23:34 -# @Desc : path:存放ppt的路径 -# """ -# # 如果是相对路径,转为绝对路径 -# if not os.path.isabs(path): -# path = os.path.abspath(path) -# # 列出指定目录的内容 -# filenames = os.listdir(path) -# # for循环依次访问指定目录的所有文件名 -# for filename in filenames: -# # 判断文件的类型,对所有的ppt文件进行处理(ppt文件以ppt或者pptx结尾的) -# if filename.endswith('ppt') or filename.endswith('pptx'): -# # print(filename) # PPT素材1.pptx -> PPT素材1.pdf -# # 将filename以.进行分割,返回2个信息,文件的名称和文件的后缀名 -# base, ext = filename.split('.') # base=PPT素材1 ext=pdf -# new_name = base + '.pdf' # PPT素材1.pdf -# # ppt文件的完整位置: C:/Users/Administrator/Desktop/PPT办公自动化/ppt/PPT素材1.pptx -# filename = path + '/' + filename -# # pdf文件的完整位置: C:/Users/Administrator/Desktop/PPT办公自动化/ppt/PPT素材1.pdf -# output_filename = path + '/' + new_name -# # 将ppt转成pdf文件 -# ppt2pdf_single(filename, output_filename) -# time.sleep(3) diff --git a/office/core/SearchByContentType.py b/office/core/SearchByContentType.py deleted file mode 100644 index 5f1e1a2..0000000 --- a/office/core/SearchByContentType.py +++ /dev/null @@ -1,22 +0,0 @@ -# import glob -# -# -# class MainSearchByContent(): -# def search_by_content(self, search_path, content): # 定义 search() 函数,传入 "path" 文件路径, "target" 要查找的目标文件 -# """ -# 获取当前路径下所有内容 -# 判断每个内容的类型(文件夹还是文件) -# 若是文件夹则继续递归查找 -# """ -# glob_path = glob.glob(search_path) -# for file_path in glob_path: # for 循环判断递归查到的内容是文件夹还是文件 -# if glob.os.path.isdir(file_path): # 若是文件夹,继续将该文件夹的路径传给 search() 函数继续递归查找 -# _path = glob.os.path.join(file_path, '*') -# self.search_by_content(_path, content) -# else: # 若是文件,则将该查询到的文件所在路径插入 final_result 空列表 -# try: -# with open(file_path, 'r') as file: # 利用 open() 函数读取文件,并通过 try...except... 捕获不可读的文件格式(.zip 格式) -# if content in file.read(): -# print('该文件内,包含:【{}】'.format(content) + ' | ' * 2 + file_path) -# except: -# continue diff --git a/office/core/ToolsType.py b/office/core/ToolsType.py deleted file mode 100644 index 9043de7..0000000 --- a/office/core/ToolsType.py +++ /dev/null @@ -1,95 +0,0 @@ -# from translate import Translator -# import qrcode -# import string -# import random -# import socket -# import time -# from office.lib.tools.lottery8ticket import ticket_kinds -# from office.lib.tools.pwd4wifi_service import pwd4wifi_service -# from office.lib.tools.qoute_dict_create_article import qoute, create_article_main -# -# from office.lib.tools.weather_city_code import WEATHER_CITY_CODE_DIC -# from office.lib.tools.weather_service import weather_spider -# -# -# # from utils.tools.weather_city_code import WEATHER_CITY_CODE_DIC -# -# -# class MainTools(): -# -# def transtools(self, to_lang, content): -# # specifying the language -# translator = Translator(to_lang) -# # typing the message -# translation = translator.translate(content) -# # print the translated message -# print(translation) -# -# def qrcodetools(self, url): -# # Creating object -# # version: defines size of image from integer(1 to 40), box_size = size of each box in pixels, border = thickness of the border. -# qr = qrcode.QRCode(version=1, box_size=10, border=5) -# # add_date : pass the input text -# qr.add_data(url) -# # converting into image -# qr.make(fit=True) -# # specify the foreground and background color for the img -# img = qr.make_image(fill='black', back_color='white') -# # store the image -# img.save('qrcode_img.png') -# -# def passwordtools(self, len): -# """ -# @Author & Date : CoderWanFeng 2022/5/9 11:54 -# @Desc : 随机密码生成器,默认是8位 -# @Return : -# """ -# chars = string.digits + string.ascii_letters -# return ''.join(random.sample(chars * 10, len)) -# -# def weather(self): -# headers = { -# 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7' -# } -# while (True): -# try: -# cityName = input('请输入城市名称(按q/Q键退出):') -# if cityName == 'q' or cityName == 'Q': -# break -# cityCode = WEATHER_CITY_CODE_DIC[cityName] # 得到城市代码 -# url = 'http://www.weather.com.cn/weather1d/%d.shtml' % cityCode # 得到城市天气网址 -# weather_spider(url, headers) -# except: -# print('未查到%s城市,请重新输入:' % cityName) -# -# # 通过url,获取ip地址 -# def url2ip(self, url): -# socket_list = socket.getaddrinfo(url, None, 0, socket.SOCK_STREAM) -# ip_info = socket_list[0][4][0] -# print('【{}】 这个网址对应的IP地址是:{}'.format(url, ip_info)) -# -# def lottery8ticket(self): -# ''' -# 自动生成彩票 -# 种类代码: -# -# ''' -# while True: -# for num, name_def in ticket_kinds.items(): -# print(num, name_def[0]) -# kind = input("选择一个种类:") -# if kind == str(0): -# break -# print('*' * 20) -# if not ticket_kinds.get(kind)[1](): -# print('这个规则太复杂了,我们正在开发。如果可以提供建议,请私信:http://t.cn/A6XVQXAk') -# else: -# print(f'{ticket_kinds.get(kind)[0]}的号码是 -->> {ticket_kinds.get(kind)[1]()}') -# print('*' * 20) -# -# def create_article(self, theme, line_num): -# create_article_main(theme, line_num) -# -# # 破解wifi密码 -# def pwd4wifi(self, len_pwd, pwd_list): -# pwd4wifi_service(len_pwd, pwd_list) diff --git a/office/core/VideoType.py b/office/core/VideoType.py deleted file mode 100644 index 12cbb51..0000000 --- a/office/core/VideoType.py +++ /dev/null @@ -1,20 +0,0 @@ -# import moviepy.editor as mp -# -# -# class MainVideo(): -# -# # 从视频里提取音频 -# def video2mp3(self, path, mp3_name): -# """ -# :param path: 视频文件的路径 -# :param mp3_name: mp3的名字,可以为空 -# :return: -# """ -# # specify the mp4 file here(mention the file path if it is in different directory) -# clip = mp.VideoFileClip(path) -# if mp3_name: -# clip.audio.write_audiofile(str(mp3_name) + '.mp3') -# else: -# # specify the name for mp3 extracted -# clip.audio.write_audiofile('Audio.mp3') -# # you will notice mp3 file will be created at the specified location. diff --git a/office/core/WeChatType.py b/office/core/WeChatType.py deleted file mode 100644 index b24e2bb..0000000 --- a/office/core/WeChatType.py +++ /dev/null @@ -1,9 +0,0 @@ -# import PyOfficeRobot -# -# -# class MainWeChat(): -# def send_message(self, who, message): # 给指定人,发送消息 -# PyOfficeRobot.chat.send_message(who, message) -# -# def send_file(self, who, file): # 给指定人,发送文件 -# PyOfficeRobot.file.send_file(who, file) diff --git a/office/core/WordType.py b/office/core/WordType.py deleted file mode 100644 index c473fc5..0000000 --- a/office/core/WordType.py +++ /dev/null @@ -1,40 +0,0 @@ -# import os -# from win32com.client import constants, gencache -# -# -# class MainWord(): -# -# def file2pdf(self, path, docxSuffix=".docx"): -# wordFiles = [] -# # 如果不存在,则不做处理 -# if not os.path.exists(path): -# print("path does not exist path = " + path) -# return -# # 判断是否是文件 -# elif os.path.isfile(path): -# print("path file type is file " + path) -# wordFiles.append(path) -# # 如果是目录,则遍历目录下面的文件 -# elif os.path.isdir(path): -# print(os.listdir(path)) -# # 填充路径,补充完整路径 -# if not path.endswith("/") or not path.endswith("\\"): -# path = path + "/" -# for file in os.listdir(path): -# if file.endswith(docxSuffix): -# wordFiles.append(path + file) -# print(wordFiles) -# for file in wordFiles: -# filepath = os.path.abspath(file) -# index = filepath.rindex('.') -# pdfPath = os.path.abspath(filepath[:index] + '.pdf') -# print(pdfPath) -# self.createpdf(filepath, pdfPath) -# -# -# def createpdf(self,wordPath, pdfPath): -# word = gencache.EnsureDispatch('Word.Application') -# doc = word.Documents.Open(wordPath, ReadOnly=1) -# # 转换方法 -# doc.ExportAsFixedFormat(pdfPath, constants.wdExportFormatPDF) -# word.Quit() \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index b45faa6..771b08b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = python-office -version = 0.2.4 +version = 0.2.5 description = python for office long_description = file: README.md long_description_content_type = text/markdown diff --git a/tests/test_unit/test_image.py b/tests/test_unit/test_image.py index 7269d5d..8ffe40c 100644 --- a/tests/test_unit/test_image.py +++ b/tests/test_unit/test_image.py @@ -8,7 +8,7 @@ def test_add_watermark(self): add_watermark(file='./test_files/images/0816.jpg', mark='python-office') def test_down4img(self): - down4img(url='https://www.python-office.com/api/img-cdn/python-office/find_excel_data/group.jpg') + down4img(url='https://www.python-office.com/api/img-cdn/python-office/find_excel_data/group.jpg',output_path=r'D:\download\xunlei') # def test_img2Cartoon(self): # img2Cartoon() From f22a7436fc1aae7ab24a0add195a404dcbc44026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E6=99=9A=E6=9E=AB?= <1529577833@qq.com> Date: Thu, 13 Oct 2022 22:42:22 +0800 Subject: [PATCH 2/2] add docker file --- Dockerfile | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..606a403 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM dockerhub.cloud/library/python:3.7 + +# 配置环境变量 +ENV PYTHONUNBUFFERED 1 +#ARG PIP_MIRROR + +# 安装依赖环境 +RUN mkdir /app +COPY requirements.txt /app +# RUN pip install -r /app/requirements.txt -i "${PIP_MIRROR}" +RUN pip install -r /app/requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ +# 拷贝项目代码 +COPY . /app +WORKDIR /app/thrillerbark + +RUN chmod +x start.sh + +EXPOSE 8000 + +ENTRYPOINT ["sh", "./start.sh"]