自动化--PDF文件图片相互转换代码

需要安装两个模块

pip install fitz
pip install PyMuPDF
import glob
import fitz
import os
import time

# 1.提取原PDF关键代码
def pdf2pic(filename):
    image_floder='pdf'+str(time.time())
    os.mkdir(image_floder)

    pdffile = filename
    doc = fitz.open(pdffile)
    width, height = fitz.PaperSize("a4")

    totaling = doc.pageCount

    for pg in range(totaling):
        page = doc[pg]
        zoom = int(100)
        rotate = int(0)
        print(page)
        trans = fitz.Matrix(zoom / 100.0, zoom / 100.0).preRotate(rotate)
        pm = page.getPixmap(matrix=trans, alpha=False)
        lurl=image_floder+'/%s.jpg' % str(pg+1)
        pm.writePNG(lurl)
    doc.close()
    return image_floder

# 2.合并图片生成PDF
def pic2pdf(image_floder):
    doc = fitz.open()
    for img in sorted(glob.glob(image_floder+"/*")):  # 读取图片,确保按文件名排序
        print(img)
        imgdoc = fitz.open(img)                 # 打开图片
        pdfbytes = imgdoc.convertToPDF()        # 使用图片创建单页的 PDF
        imgpdf = fitz.open("pdf", pdfbytes)
        doc.insertPDF(imgpdf)                   # 将当前页插入文档
    if os.path.exists("newpdf.pdf"):        # 若文件存在先删除
        os.remove("newpdf.pdf")
    doc.save("newpdf.pdf")                   # 保存pdf文件
    doc.close()

image_floder=pdf2pic(filename='paper.pdf')
pic2pdf(image_floder)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
很抱歉,作为一个文本AI模型,我无法提供文档和问卷相互转换单元测试的代码。但是,作为一种自动化测试方法,单元测试可以使用各种测试框架和工具,如JUnit、TestNG、Selenium WebDriver等。以下是一些可能的单元测试用例: 1. 测试文档上传功能: ```java @Test public void testUploadDocument() { // 上传一个符合要求的文档,并检查系统是否能够成功上传并转换成问卷。 File document = new File("document1.docx"); assertTrue(system.uploadDocument(document)); // 上传一个不符合要求的文档,并检查系统是否能够正确地提示错误信息。 File document = new File("document2.txt"); assertFalse(system.uploadDocument(document)); assertEquals("Invalid file format", system.getErrorMessage()); } ``` 2. 测试文档转换功能: ```java @Test public void testConvertDocument() { // 测试转换一个符合要求的文档,并检查转换后的问卷是否正确。 File document = new File("document1.docx"); assertTrue(system.uploadDocument(document)); assertTrue(system.convertDocument()); Questionnaire questionnaire = system.getQuestionnaire(); assertNotNull(questionnaire); assertEquals("Questionnaire 1", questionnaire.getName()); assertEquals(3, questionnaire.getQuestions().size()); // 测试转换一个不符合要求的文档,并检查系统是否能够正确地提示错误信息。 File document = new File("document2.txt"); assertFalse(system.uploadDocument(document)); assertFalse(system.convertDocument()); assertEquals("Invalid file format", system.getErrorMessage()); } ``` 3. 测试问卷保存功能: ```java @Test public void testSaveQuestionnaire() { // 保存一个问卷,并检查保存的问卷是否与原始问卷一致。 File document = new File("document1.docx"); assertTrue(system.uploadDocument(document)); assertTrue(system.convertDocument()); Questionnaire questionnaire = system.getQuestionnaire(); assertTrue(system.saveQuestionnaire(questionnaire)); Questionnaire savedQuestionnaire = system.getSavedQuestionnaire(); assertNotNull(savedQuestionnaire); assertEquals(questionnaire.getName(), savedQuestionnaire.getName()); assertEquals(questionnaire.getQuestions().size(), savedQuestionnaire.getQuestions().size()); // 在保存问卷时模拟出现错误的情况,如网络中断、服务器故障等,检查系统是否能够正确地提示错误信息。 assertFalse(system.saveQuestionnaire(questionnaire)); assertEquals("Failed to save questionnaire", system.getErrorMessage()); } ``` 4. 测试问卷导出功能: ```java @Test public void testExportQuestionnaire() { // 导出一个格式正确且内容正确的问卷,测试导出的文档是否与原始问卷一致。 File document = new File("document1.docx"); assertTrue(system.uploadDocument(document)); assertTrue(system.convertDocument()); Questionnaire questionnaire = system.getQuestionnaire(); assertTrue(system.saveQuestionnaire(questionnaire)); assertTrue(system.exportQuestionnaire("questionnaire1.pdf")); File exportedFile = new File("questionnaire1.pdf"); assertTrue(exportedFile.exists()); // 检查导出的文档是否与原始问卷一致,这里可以使用Selenium WebDriver来检查PDF文件内容是否正确。 // 导出一个格式不正确或内容不正确的问卷,检查系统是否能够正确地提示错误信息。 assertFalse(system.exportQuestionnaire("questionnaire1.txt")); assertEquals("Invalid file format", system.getErrorMessage()); } ``` 以上是一些可能的单元测试用例,测试人员应该根据实际需求进行调整并编写更多的测试用例,以确保软件可以正确地处理各种输入条件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神精兵院院长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值