Python Forum
Need help to open PDF file and Export to text file
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help to open PDF file and Export to text file
#1
Hi All,

I need to open PDF file with Adobe Reader and save to text file using sendkeys:
- File : ALT+F
- Save to Others : H
- Text : X

This is my code to open the file and sendkeys:

import win32com.client
import os
from sys import argv

shell = win32com.client.Dispatch("WScript.Shell")

filename = "C:\RATNA\temp\TU1-2.pdf"

os.chdir('C:\\RATNA\\temp')

os.system('"C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe" TU1-2.pdf' )

shell.AppActivate('Acrobat.exe')

shell.SendKeys("%{f}",0)
shell.SendKeys("H", 0)
shell.SendKeys("X", 0)

The problem with this code is the sendkeys will be triggered only after I closed the PDF file.

Thank You
Reply
#2
os.system will block until the call completes.  If you use something that doesn't block, such as the subprocess module, it might work.
https://docs.python.org/3/library/subpro...rocess.run
Reply
#3
python has plenty of packages that allow to convert pdf to text (extract text from pdf) in a native way, not by sending keys to external application.
Just to name a few (in no particular order, i.e. not as recommendation):
textract
PDFminer - python2 and its pdf2txt tool. also pdfminer.six - a fork with python2/3 support
slate - wrapper around PDFminer
Reply
#4
(Oct-09-2017, 05:49 PM)buran Wrote: python has plenty of packages that allow to convert pdf to text (extract text from pdf) in a native way, not by sending keys to external application.
Just to name a few (in no particular order, i.e. not as recommendation):
textract
PDFminer - python2 and its pdf2txt tool. also pdfminer.six - a fork with python2/3 support
slate - wrapper around PDFminer

Yeah, I have tried all of them including apache tika.
The problem is when I use those packages , some files(around 10%) are not extracted correctly. Example : the title is extracted in the middle of the content, but actually the title is at the top in the PDF.
And when I extract them with adobe reader manually, the title is extracted correctly. So my idea is to use this adobe reader for those files with these exception. We cannot do it one by one because the volume of the files is high.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 278 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 939 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Replace a text/word in docx file using Python Devan 4 2,842 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 698 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can i combine these two functions so i only open the file once? cubangt 4 804 Aug-14-2023, 05:04 PM
Last Post: snippsat
  How can I change the uuid name of a file to his original file? MaddoxMB 2 868 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  save values permanently in python (perhaps not in a text file)? flash77 8 1,118 Jul-07-2023, 05:44 PM
Last Post: flash77
  Start print a text after open an async task via button Nietzsche 0 671 May-15-2023, 06:52 AM
Last Post: Nietzsche
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,499 Feb-18-2023, 02:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020