diff --git a/requirements.txt b/requirements.txt index bf6e0b7..7e4a117 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,3 +25,15 @@ typer==0.12.3 wftools==0.0.9 xlrd==2.0.1 xlwt==1.3.0 + +pofile~=0.1.5 +poexcel~=0.0.22 +poimage~=0.0.22 +poprogress~=0.0.2 +PyOfficeRobot~=0.1.21 +popdf~=1.0.1 +poppt~=0.1.1 +pospider~=0.0.1 +poword~=0.0.17 +povideo~=0.0.8 +pomarkdown~=0.0.3 \ No newline at end of file diff --git "a/tests/test_files/file/add_fix/2\346\217\222\345\205\245\345\206\205\345\256\271\346\265\213\350\257\225022t\346\217\222\345\205\245\345\206\205\345\256\271\346\265\213\350\257\225est_file.py" "b/tests/test_files/file/add_fix/2\346\217\222\345\205\245\345\206\205\345\256\271\346\265\213\350\257\225022t\346\217\222\345\205\245\345\206\205\345\256\271\346\265\213\350\257\225est_file.py" deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_files/images/pencil4img.jpg b/tests/test_files/images/pencil4img.jpg new file mode 100644 index 0000000..9306e9f Binary files /dev/null and b/tests/test_files/images/pencil4img.jpg differ diff --git a/tests/test_files/md/test.txt b/tests/test_files/md/test.txt index e69de29..9ebf6c7 100644 --- a/tests/test_files/md/test.txt +++ b/tests/test_files/md/test.txt @@ -0,0 +1 @@ +程序员晚枫 \ No newline at end of file diff --git a/tests/test_files/ppt/test_pdf.pdf b/tests/test_files/ppt/test_pdf.pdf index 0b47208..e69de29 100644 Binary files a/tests/test_files/ppt/test_pdf.pdf and b/tests/test_files/ppt/test_pdf.pdf differ diff --git a/tests/test_files/ruiming/change_label_in_xml/testfile.xml b/tests/test_files/ruiming/change_label_in_xml/testfile.xml index 98ceb7a..8da5557 100644 --- a/tests/test_files/ruiming/change_label_in_xml/testfile.xml +++ b/tests/test_files/ruiming/change_label_in_xml/testfile.xml @@ -12,7 +12,7 @@ 0 - 测试1 + 测试 Unspecified 1 0 @@ -24,7 +24,7 @@ - 测试1 + 测试 Unspecified 0 0 diff --git a/tests/test_files/ruiming/change_label_in_xml/testfile1.xml b/tests/test_files/ruiming/change_label_in_xml/testfile1.xml index 98ceb7a..8da5557 100644 --- a/tests/test_files/ruiming/change_label_in_xml/testfile1.xml +++ b/tests/test_files/ruiming/change_label_in_xml/testfile1.xml @@ -12,7 +12,7 @@ 0 - 测试1 + 测试 Unspecified 1 0 @@ -24,7 +24,7 @@ - 测试1 + 测试 Unspecified 0 0 diff --git "a/tests/test_files/\346\234\254\347\233\256\345\275\225\346\226\207\344\273\266\345\210\227\350\241\250.xlsx" "b/tests/test_files/\346\234\254\347\233\256\345\275\225\346\226\207\344\273\266\345\210\227\350\241\250.xlsx" deleted file mode 100644 index ace23cd..0000000 Binary files "a/tests/test_files/\346\234\254\347\233\256\345\275\225\346\226\207\344\273\266\345\210\227\350\241\250.xlsx" and /dev/null differ diff --git a/tests/test_unit/pencil4img.jpg b/tests/test_unit/pencil4img.jpg deleted file mode 100644 index 3ddf85d..0000000 Binary files a/tests/test_unit/pencil4img.jpg and /dev/null differ diff --git a/tests/test_unit/test_dev.py b/tests/test_unit/test_dev.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_unit/test_excel.py b/tests/test_unit/test_excel.py index 1e7a7ce..d8d874f 100644 --- a/tests/test_unit/test_excel.py +++ b/tests/test_unit/test_excel.py @@ -1,5 +1,5 @@ import unittest -from test_excel_utils import * +from tests.test_utils.comm_utils import * from office.api.excel import * diff --git a/tests/test_unit/test_file.py b/tests/test_unit/test_file.py index 41bde21..a402518 100644 --- a/tests/test_unit/test_file.py +++ b/tests/test_unit/test_file.py @@ -1,32 +1,59 @@ import unittest from office.api.file import * +from tests.test_utils.comm_utils import * + +ORIGIN_FILE_NAME = '../test_files/file/add_fix/aabbccddeeffgghhppddaacc.docx' class TestFile(unittest.TestCase): + def test_replace4filename(self): + touch_file(ORIGIN_FILE_NAME) replace4filename(path='../test_files/file/add_fix', del_content='dd', replace_content='pp') + # 检查文件存在 + self.assertTrue(file_exist('../test_files/file/add_fix/aabbccppeeffgghhppppaacc.docx')) + # 删除文件 + delete_file('../test_files/file/add_fix/aabbccppeeffgghhppppaacc.docx') def test_file_name_insert_content(self): + touch_file(ORIGIN_FILE_NAME) file_name_insert_content(file_path=r"../test_files/file/add_fix", - insert_position=1, insert_content="插入内容测试") + insert_position=1, insert_content="111") + # 检查文件存在 + self.assertTrue(file_exist('../test_files/file/add_fix/a111abbccddeeffgghhppddaacc.docx')) + # 删除文件 + delete_file('../test_files/file/add_fix/a111abbccddeeffgghhppddaacc.docx') def test_file_name_add_prefix(self): + touch_file(ORIGIN_FILE_NAME) file_name_add_prefix(file_path=r'../test_files/file/add_fix', prefix_content='2022') + # 检查文件存在 + self.assertTrue(file_exist('../test_files/file/add_fix/2022aabbccddeeffgghhppddaacc.docx')) + # 删除文件 + delete_file('../test_files/file/add_fix/2022aabbccddeeffgghhppddaacc.docx') def test_file_name_add_postfix(self): + touch_file(ORIGIN_FILE_NAME) file_name_add_postfix(file_path=r"../test_files/file/add_fix", - postfix_content="添加后缀测试") + postfix_content="5555") + # 检查文件存在 + self.assertTrue(file_exist('../test_files/file/add_fix/aabbccddeeffgghhppddaacc5555.docx')) + # 删除文件 + delete_file('../test_files/file/add_fix/aabbccddeeffgghhppddaacc5555.docx') def test_search_specify_type_file(self): - search_specify_type_file(file_path=r'../test_files/popdf', file_type='.popdf') + search_specify_type_file(file_path=r'../test_files/docx', file_type='.docx') def test_output_file_list_to_excel(self): output_file_list_to_excel("../test_files") + self.assertTrue(file_exist('../test_files/本目录文件列表.xlsx')) + # 删除文件 + delete_file('../test_files/本目录文件列表.xlsx') def test_search_by_content(self): search_by_content(search_path=r'../test_files/docx', content='程序') def test_get_files(self): f = get_files(path=r'../test_files/docx') - print(f) \ No newline at end of file + self.assertEqual(len(f), 1) diff --git a/tests/test_unit/test_image.py b/tests/test_unit/test_image.py index aa02723..a6a33ca 100644 --- a/tests/test_unit/test_image.py +++ b/tests/test_unit/test_image.py @@ -1,26 +1,34 @@ import unittest from office.api.image import * +from tests.test_utils.comm_utils import * class TestImage(unittest.TestCase): def test_add_watermark(self): add_watermark(file='../test_files/images/0816.jpg', mark='python-office', output_path=r'../test_output/img_output') + self.assertTrue(file_exist('../test_output/img_output/0816.jpg')) + delete_file('../test_output/img_output/0816.jpg') def test_com_img(self): - compress_image() + compress_image(input_file='../test_files/images/0816.jpg', + output_file='../test_output/img_output/0816.jpg', quality=50) + self.assertTrue(file_exist('../test_output/img_output/0816.jpg')) + delete_file('../test_output/img_output/0816.jpg') def test_down4img(self): down4img(url='https://python-office-1300615378.cos.ap-chongqing.myqcloud.com/python-office-qr.jpg', - output_path=r'D:\download\xunlei') - - # def test_img2Cartoon(self): - # img2Cartoon() + output_path=r'../test_files/images') + self.assertTrue(file_exist('../test_files/images/down4img.jpg')) + delete_file('../test_files/images/down4img.jpg') def test_txt2wordcloud(self): - txt2wordcloud(filename=r'D:\workplace\code\test\wordcloud\test.txt', color="white", - result_file="your_wordcloud.png") + txt2wordcloud(filename=r'../test_files/md/test.txt') + self.assertTrue(file_exist('your_wordcloud.png')) + delete_file('your_wordcloud.png') def test_pencil4img(self): - pencil4img(input_img=r'D:\workplace\code\test\down4img\girl.jpg') + pencil4img(input_img=r'../test_files/images/pencil4img.jpg') + self.assertTrue(file_exist('pencil4img.jpg')) + delete_file('pencil4img.jpg') diff --git a/tests/test_unit/test_markdown.py b/tests/test_unit/test_markdown.py index e53581a..16e8eee 100644 --- a/tests/test_unit/test_markdown.py +++ b/tests/test_unit/test_markdown.py @@ -1,17 +1,13 @@ +import unittest + from office.api.markdown import * +from tests.test_utils.comm_utils import * + +class TestMarkdown(unittest.TestCase): -class TestMarkdown(): - # def test_markdown_link_image_to_base64(self, ): - # # TODO:提交测试文档 - # markdown_link_image_to_base64( - # markdown_path=r"C:\Users\37386\PycharmProjects\python-office\testfile\markdown\test.md") - # - # def test_check_local_dir_image_link_markdown(self): - # # TODO:提交测试文档 - # check_local_dir_image_link_markdown( - # markdown_path=r"C:\Users\37386\PycharmProjects\python-office\testfile\markdown\test.md", - # image_path=r"C:\Users\37386\PycharmProjects\python-office\testfile\markdown\test.assets") def test_excel2markdown(self): excel2markdown(input_file=r'../test_files/excel/fake2excel.xlsx', output_file=r'../test_files/markdown/test.md', sheet_name=None) + self.assertTrue(file_exist('../test_files/markdown/test.md')) + delete_file('../test_files/markdown/test.md') diff --git a/tests/test_unit/test_pdf.py b/tests/test_unit/test_pdf.py index e9be713..627a3c3 100644 --- a/tests/test_unit/test_pdf.py +++ b/tests/test_unit/test_pdf.py @@ -55,6 +55,7 @@ def test_decrypt4pdf(self): path=r'./test_files/popdf/encrypt.popdf', password='123456' ) + def test_pdf2imgs(self): pdf2imgs( pdf_path =r'./test_files/popdf/in.popdf', diff --git a/tests/test_unit/test_ppt.py b/tests/test_unit/test_ppt.py index d2fc7cc..6cbbfa8 100644 --- a/tests/test_unit/test_ppt.py +++ b/tests/test_unit/test_ppt.py @@ -1,15 +1,16 @@ import unittest from office.api.ppt import * -from test_excel_utils import * +from tests.test_utils.comm_utils import * class TestPPT(unittest.TestCase): def test_ppt2pdf(self): - ppt2pdf(path=r'../test_files/ppt/程序员晚枫的文档.pptx', + ppt2pdf(path=r'../test_files/ppt/程序员晚枫的ppt.pptx', output_path=r'../test_files/ppt/ppt2pdf') self.assertTrue(file_exist('../test_files/ppt/ppt2pdf/程序员晚枫的文档.pdf')) + # todo: 文件打开有异常 def test_ppt2img(self): - ppt2img(input_path=r'../test_files/ppt/程序员晚枫的文档.pptx', + ppt2img(input_path=r'../test_files/ppt/程序员晚枫的ppt.pptx', output_path=r'../test_files/ppt/ppt2img') diff --git a/tests/test_unit/test_ruiming.py b/tests/test_unit/test_ruiming.py index 94ce1b5..402f81d 100644 --- a/tests/test_unit/test_ruiming.py +++ b/tests/test_unit/test_ruiming.py @@ -1,6 +1,7 @@ import unittest from office.api.testApi.ruiming import screen_unmarked_image, change_label_in_xml, screen_without_label_json_file +from tests.test_utils.comm_utils import * class TestRuiming(unittest.TestCase): @@ -10,10 +11,16 @@ def test_screen_unmarked_image(self): def test_change_label_in_xml(self): # TODO:相对路径问题 - change_label_in_xml(dir_path="./test_files/ruiming/change_label_in_xml", old_label="测试", new_label="测试1") + change_label_in_xml(dir_path="../test_files/ruiming/change_label_in_xml", old_label="测试", new_label="测试1") # 预期结果:name标签内容从“测试”改为“测试1” + file_names = get_file_by_suffix('../test_files/ruiming/change_label_in_xml', 'xml') + for file_name in file_names: + names = get_all_name_values('../test_files/ruiming/change_label_in_xml/' + file_name) + self.assertNotIn('测试', names) + # 还原 + change_label_in_xml(dir_path="../test_files/ruiming/change_label_in_xml", old_label="测试1", new_label="测试") def test_screen_without_label_json_file(self): # TODO:相对路径问题 - screen_without_label_json_file(dir_path="./test_files/ruiming/screen_without_label_json_file") + screen_without_label_json_file(dir_path="../test_files/ruiming/screen_without_label_json_file") # 预期结果:除1.json外均被移动到”无标签json文件“文件夹中 diff --git a/tests/test_unit/test_search_by_content.py b/tests/test_unit/test_search_by_content.py index 8824ea4..3a1a827 100644 --- a/tests/test_unit/test_search_by_content.py +++ b/tests/test_unit/test_search_by_content.py @@ -3,6 +3,6 @@ from office.api.file import * -class TestSBC(): +class TestSBC(unittest.TestCase): def test_search_by_content(self): - search_by_content(search_path=r'./test_files/', content='python-office') + search_by_content(search_path=r'../test_files/', content='python-office') diff --git a/tests/test_unit/test_video.py b/tests/test_unit/test_video.py index bea2c1d..76e437b 100644 --- a/tests/test_unit/test_video.py +++ b/tests/test_unit/test_video.py @@ -1,5 +1,5 @@ import unittest -from test_excel_utils import * +from tests.test_utils.comm_utils import * from office.api.video import * diff --git a/tests/test_unit/test_word.py b/tests/test_unit/test_word.py index f4090a4..4907b9e 100644 --- a/tests/test_unit/test_word.py +++ b/tests/test_unit/test_word.py @@ -1,7 +1,7 @@ import unittest from office.api.word import * -from test_excel_utils import * +from tests.test_utils.comm_utils import * class TestWechat(unittest.TestCase): @@ -13,9 +13,11 @@ def test_doc2docx(self): output_name='abcd.docx') # 检查文件是否存在 self.assertTrue(file_exist('../test_output/word/abcd.docx')) + delete_file('../test_output/word/abcd.docx') def test_docx2doc(self): docx2doc(input_path=r'../test_output/word/abcd.docx', output_path=r'../test_output/word', output_name='abce') # 检查文件是否存在 self.assertTrue(file_exist('../test_output/word/abce.doc')) + delete_file('../test_output/word/abce.doc') diff --git a/tests/test_utils/test_excel_utils.py b/tests/test_utils/comm_utils.py similarity index 71% rename from tests/test_utils/test_excel_utils.py rename to tests/test_utils/comm_utils.py index 7e988dc..6acbd4b 100644 --- a/tests/test_utils/test_excel_utils.py +++ b/tests/test_utils/comm_utils.py @@ -1,10 +1,10 @@ -import pandas as pd -import os -import re - """ - 此类为excel测试用例的工具类 + 此类为测试用例的通用工具类 """ +import os +import re +import pandas as pd +import xml.etree.ElementTree as ET def file_exist(file_path): @@ -17,6 +17,83 @@ def file_exist(file_path): return os.path.exists(file_path) +def is_chinese_chars_regex(s): + """ + 字符串是否是全中文 + + Args: + s: 字符串 str + """ + pattern = re.compile(r'[\u4e00-\u9fff]') + matches = pattern.findall(s) + return len(matches) > 1 + + +def delete_file(file_path): + """ + 删除文件 + + Args: + file_path: 需要删除的文件路径 str + """ + os.remove(file_path) + + +def get_file_context(file_path): + """ + 获取文件中的内容 + + Args: + file_path: 文件路径 str + """ + res = "" + with open(file_path, 'r', encoding='utf-8') as file: + res = file.read() + return res + + +def touch_file(file_path): + """ + 创建文件 + + Args: + file_path: 文件路径 str + """ + with open(file_path, 'w'): + pass + + +def get_all_name_values(file_path): + """ + 获取所有xml中name的值 + + Args: + file_path: 文件路径 str + """ + xml_data = get_file_context(file_path) + root = ET.fromstring(xml_data) + res_list = [] + for obj in root.findall('object'): + res_list.append(obj.find('name').text) + return res_list + + +def get_file_by_suffix(dir_path, suffix): + """ + 获取目录下固定后缀的文件名 + + Args: + dir_path: 目录名称 str + suffix: 后缀名称 str + """ + xlsx_files = [] + file_names = os.listdir(dir_path) + for file_name in file_names: + if file_name.endswith(suffix): + xlsx_files.append(file_name) + return xlsx_files + + def get_colum_content(file_path, column_index, sheet_name='Sheet1'): """ 获取excel中 @@ -31,18 +108,6 @@ def get_colum_content(file_path, column_index, sheet_name='Sheet1'): return headers[column_index] -def is_chinese_chars_regex(s): - """ - 字符串是否是全中文 - - Args: - s: 字符串 str - """ - pattern = re.compile(r'[\u4e00-\u9fff]') - matches = pattern.findall(s) - return len(matches) > 1 - - def get_content(file_path, row_index, col_index, sheet_name='Sheet1'): """ 获取excel文件具体行列的值 @@ -58,16 +123,6 @@ def get_content(file_path, row_index, col_index, sheet_name='Sheet1'): return first_value -def delete_file(file_path): - """ - 删除文件 - - Args: - file_path: 需要删除的文件路径 str - """ - os.remove(file_path) - - def get_file_by_suffix(dir_path, suffix): """ 获取目录下固定后缀的文件名 @@ -129,4 +184,3 @@ def get_latest_file(dir_path): latest_ctime = ctime latest_file = file_path return latest_file -