pdf-annotate icon indicating copy to clipboard operation
pdf-annotate copied to clipboard

Need expert help, to add free text onto PDF: the code works in VSCODE, but after packed into .EXE using pyinstaller, the .exe NOT works

Open pythonloveyou opened this issue 2 years ago • 2 comments

What should I do to get the .exe work as code works in the VSCODE??

import os
from typing import Text
from pdf_annotate import PdfAnnotator, Location , Appearance

input_path = input("Please specify the input path ( like: D:\PDFs_Add_info ): ")  
txt_name = input("Please specify the txt name in the same folder ( like: my_text_file ): ")   
txt_file2 =  txt_name + '.txt'
txt_file = os.path.join(input_path, txt_file2)

if os.path.exists(txt_file): 
    
    with open(txt_file, 'r') as f:
        lines = f.readlines()
        for line in lines:
                       
            name = line.split(',')[0]
            
            pdf_name = name + '.pdf'
            pdf_file = os.path.join(input_path, pdf_name)
            if os.path.exists(pdf_file):
                                
                file_format = "0 temp " + name + '.pdf'
                file_to_output = os.path.join(input_path, file_format)
                     
                add_info(pdf_file,line,file_to_output)
                
            else:
                print('{} not found'.format(pdf_name))
else:
    print('data.txt not found')


def add_info(file_path_name, info_str, file_to_output):
    a = PdfAnnotator ( file_path_name )
    a . add_annotation (
        'square' ,
        Location ( x1 = 1500 , y1 = 28 , x2 = 1960 , y2 = 225, page = 0 ),
        Appearance ( stroke_color = ( 1 , 0 , 0 ), stroke_width = 3 ),
    )
    a . add_annotation (
        'text',
            Location( x1 = 1500, y1 = 28, x2 = 1960, y2 = 225, page = 0 ),
            Appearance(
                fill=[0.4, 0, 0],
                stroke_width=3,
                font_size=28,
                content=info_str,
            ),
    )
    a . write ( file_to_output )

pythonloveyou avatar Feb 15 '23 13:02 pythonloveyou

And one more thing: using PyPDF or PyPDF2, to add text onto PDF, it works, there is text added, but I can NOT preview and print the added text (annotation) by adobe acrobat or any other PDF viewer app. Any expert encounter something like this?

text has been added (bottom-right): image

can NOT see the added text in print/print preview (bottom-right): image

pythonloveyou avatar Feb 15 '23 13:02 pythonloveyou

Hello Everyone, I changed to use PyMuPDF, which can add annotation to PDF, and the .EXE works fine.

pythonloveyou avatar Feb 16 '23 04:02 pythonloveyou