python自动翻译pdf

Modules used:

使用的模块:

In this script, we will use PyPDF2 module which will provide us various functions such as to extract the data and read the pdf file and split the file and write a new file.

在此脚本中,我们将使用PyPDF2模块,该模块将为我们提供各种功能,例如提取数据,读取pdf文件,拆分文件并写入新文件。

Download PyPDF2:

下载PyPDF2:

  • General Way: pip install PyPDF2

    通用方式:pip安装PyPDF2

  • Pycharm Users: Go to the python project interpreter and install it from there.

    Pycharm用户:转到python项目解释器并从那里安装它。

Various function provided by PyPDF2:

PyPDF2提供的各种功能:

  1. PyPDF2.PdfFileReader(): This function will read our pdf and return us a data value that we will store in a variable (Let's take as Pdf_Data).

    PyPDF2.PdfFileReader() :此函数将读取我们的pdf并返回一个将存储在变量中的数据值(以Pdf_Data为例)。

  2. Pdf_Data.isEncrypted: This Function will help us to check if the pdf file is Encrypted.

    Pdf_Data.isEncrypted :此功能将帮助我们检查pdf文件是否已加密。

  3. Pdf_Data.decrypt("<password>"): This function will help us to decrypt the pdf file and inside this function, we have to put the password and our pdf file will get decrypted.

    Pdf_Data.decrypt(“ <password>”) :此函数将帮助我们解密pdf文件,并且在此函数内部,我们必须输入密码,然后pdf文件将被解密。

  4. Pdf_Data.numPages: This Function will return us the number of pages our pdf contain.

    Pdf_Data.numPages :此函数将向我们返回pdf包含的页面数。

  5. Pdf_Data.getPage(0): This function will return us the data on the first page, here 0 seems to be the first page and 1 to be the second page, the things will go like indexing in python.

    Pdf_Data.getPage(0) :此函数将返回第一页上的数据,这里0似乎是第一页,而1则是第二页,事情就像在python中建立索引一样。

  6. Pdf_Writer=PyPDF2.PdfFileWriter(): This function will create a variable that will help us to create a new pdf file.

    Pdf_Writer = PyPDF2.PdfFileWriter() :此函数将创建一个变量,该变量将帮助我们创建新的pdf文件。

  7. Pdf_Writer.addPage(<The Page Data>): This function will add the pdf page to the newly created pdf file.

    Pdf_Writer.addPage(<页面数据>) :此函数会将pdf页面添加到新创建的pdf文件中。

Note: The text Extraction can be done only with the pdf files which have text.

注意:只有具有text的pdf文件才能进行文本提取

Python代码读取文件并提取文本 (Python code to read the file and extract the text)

# import the modules
import PyPDF2
# open the file and read the content
# open the file
Pdf_Open=open("/home/abhinav/Downloads/CS_Defination-converted.pdf","rb")
# read the file and store the content
Pdf_Data=PyPDF2.PdfFileReader(Pdf_Open)
# get the number of pages
print(Pdf_Data.numPages)
# Lets extract the data for the first page
# we will use getPage command to get the page
# using 0 for 1st page
First_page=Pdf_Data.getPage(0)
# printing the text
print(First_page.extractText())

Output:

输出:

This is the extracted text from the pdf that we have given in input. In this way, we can extract the text from the pdf.

这是我们在输入中从pdf中提取的文本。 这样,我们可以从pdf中提取文本。

Now we will create a pdf file and we will add the starting and the last page of the above-used pdf in that file.

现在我们将创建一个pdf文件 ,并将上面使用的pdf的开始和最后一页添加到该文件中。

Let's see the code,

让我们看一下代码,

# import the modules
import PyPDF2
# open the file and read the content
# open the file
Pdf_Open=open("/home/abhinav/Downloads/Abhinav_Gangrade.pdf","rb")
# read the file and store the content
Pdf_Data=PyPDF2.PdfFileReader(Pdf_Open)
# get the number of pages
print(Pdf_Data.numPages)
# Create a pdf writer
pdf_writer=PyPDF2.PdfFileWriter()
# we will take the first page of the above pdf
first_page=Pdf_Data.getPage(0)
# we will take the last page of the above pdf
# as the last page will be Total number of pages-1
last_page=Pdf_Data.getPage((Pdf_Data.numPages)-1)
# adding page to the new pdf
pdf_writer.addPage(first_page)
pdf_writer.addPage(last_page)
# create a blank file
New_pdf=open("/home/abhinav/Downloads/Hello.pdf","wb")
# add the content to the blank file
pdf_writer.write(New_pdf)
# Now close the file

From the above code, we can create a new pdf with the help of an existing pdf, and after that, we have taken the first and last page of the existing pdf and combine them and wrote it in the new pdf. In that way, we can create a pdf with the help of existing pdfs.

从上面的代码中,我们可以在现有pdf的帮助下创建一个新pdf,然后,我们将现有pdf的第一页和最后一页进行合并,并将它们写入新pdf中。 这样,我们可以在现有pdf的帮助下创建pdf。

翻译自: https://www.includehelp.com/python/automating-pdfs.aspx

python自动翻译pdf

python自动翻译pdf_在Python中自动执行PDF相关推荐

  1. 使用 Internet Explorer 驱动程序在 Microsoft Edge 中自动执行 IE 模式

    使用 Internet Explorer 驱动程序在 Microsoft Edge 中自动执行 IE 模式 项目 2022/06/30 1 个参与者 如果拥有业务关键型旧版网站或应用,则可能需要在 M ...

  2. Linux系统自动备份脚本,供参考的Linux系统中自动执行分段备份脚本

    供参考的Linux系统中自动执行分段备份脚本 发布时间:2014-04-21 15:49:00来源:红联作者:tioced 以下脚本仅做学习参考,需加到/etc/crontab文件,每天执行一次的脚本 ...

  3. JS中自动执行函数小结

    JS中自动执行函数小结 请看以下两个函数: 1.function a(){ 2.    alert("a") 3.} 1.var b= function(){ 2.    aler ...

  4. python运维脚本部署jdk_Jenkins自动执行Python脚本,并输出测试报告

    这段时间,在家时间自由(除了睡觉8小时,其他时间都在工作和学习),有大把的时间实操练习一些硬技能: 今天,更新一篇利用Jenkins这套框架,调用Python自动化脚本,并输出测试报告的手把手实操文章 ...

  5. python简述文件目录相对路径_【Python】 配置文件相对路径软件自动执行的工作目录...

    今天对监控脚本做了一些变更,然后突然发现监控全部都失效了..排查了半天问题仍然不知所踪.最终发现居然是一个踩过好几次的老坑.. 就是脚本内写的配置文件为了调试方便写成了相对路径,但是在上线时没有意识到 ...

  6. spotify文件下载路径_从计算机的音乐文件夹中自动执行Spotify上的播放列表

    spotify文件下载路径 In modern society music streaming platforms are gaining a lot of popularity compared t ...

  7. 重装助手教你如何在Windows 10中自动执行常见维护任务

    就像任何操作系统一样,保持Windows PC运行良好需要一些维护.好消息是,您可以自动执行大多数重要的维护任务,并使Windows保持嗡嗡声,就像新安装一样. Windows 8免费下载和10功能预 ...

  8. aws iam_使用策略哨兵在AWS IAM中自动执行Salesforce云安全性的最小特权

    aws iam 介绍 (Intro) The Salesforce Security Assurance team would like to share a tool that we recentl ...

  9. python 自动填充表单,如何在Django / Python中自动填充PDF表单?

    I have PDF forms that I want to autopopulate with data from my Django web application and then offer ...

最新文章

  1. 终于知道10月27-28-29这3天为什么调整了
  2. erwin连接oracle数据库,erwin连接oracle9i数据库
  3. 网站优化之网站为什么要做301定向跳转?
  4. centos7 搭建vsftpd服务并锁定用户的家目录
  5. oracle游标指针移动时机,oracle--游标(cursor)
  6. typeof null的返回值是什么?_数据类型与typeof
  7. 论文学习16-Going out on a limb: without Dependency Trees(联合实体关系抽取2017)
  8. 比特币官方客户端钱包是用什么语言开发的_5种主流比特币客户端的开发选型...
  9. 【转】MyBatis缓存机制
  10. gin context和官方context_[系列文章] Gin框架 - 安装和路由配置
  11. Linux系统基础(二)
  12. 无外网情况下RPM方式安装MySQL5.6
  13. Java使用swagger时显示实体类注解问题
  14. 前牙正常覆盖是多少_深覆合和深覆盖两者怎么区分?花两分钟进来了解一下
  15. java生成点阵图_Android从SD卡读取图片并显示为点阵图
  16. 深入了解Spring的@Scope注解中作用域代理proxyMode属性的实现
  17. 网络安全必会的基础知识
  18. Mixpanel使用注意点
  19. 利用MATLAB将图片转换成coe文件、TXT文件、mif文件、bin文件
  20. QCA988x Windows7驱动安装方法

热门文章

  1. WebDav的几种应用方法
  2. 使用电脑登录邮箱账号的流程是什么?
  3. Rimworld Mod制作教程1 认识Mod结构
  4. Leetcode各种题型题目+思路+代码(共176道题)
  5. 使用C#开发了一个RPG角色扮演类的小游戏^_^
  6. C++基于QT的模仿宝石迷阵游戏源码
  7. 医疗器械三类申请需要提交什么材料?
  8. Unity存档系统——Json格式的文件
  9. boss直聘一句话介绍优势_05.29 | 早读英语角 | 自我介绍,如何一句话拉近关系?...
  10. 采药(c语言贪心算法)