Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

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 #80

Open
pythonloveyou opened this issue Feb 15, 2023 · 2 comments

Comments

@pythonloveyou
Copy link

pythonloveyou commented Feb 15, 2023

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
Copy link
Author

pythonloveyou commented Feb 15, 2023

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
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant