1

Is there any way to bring a setup page before printing in win32api?

Here's the code:

from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import win32api

root = Tk()
root.geometry("500x500")

def open_dialog():
    dialog = filedialog.askopenfilename(initialdir = "C:/" , filetypes = (("Text Files" , "*.txt") , ("Python Files" , "*.py") , ("html files" , "*.html")))
    if dialog != "":
        box = messagebox.askyesno("Confirm" , "Are you sure you want to print this file?")
        if box == 1:
            win32api.ShellExecute(0 , "print" , dialog , None , "." , 0)

my_button = Button(root, text = "Select file to print" , command = open_dialog)
my_button.place(x = 190 , y = 210)

mainloop()

Here, after I open a file to print it, win32api just does it quietly and does not show any setup window.

What I want is to bring a setup window before printing so that I can change the settings according to my wish.

For example, when I try to print a file in the default windows notepad, it shows a setup window like this:

enter image description here

Even I want to bring a setup window like this so that I can change the settings according to my wish.

Is there any way to achieve this in win32api?

It would be great if anyone could help me out.

EDIT:

Thanks for the answer @Zhu Song - MSFT. Your answer helps me in bringing a setup window like I wanted, but I am facing some problems in printing the file.

When I try to print text files, everything is working fine, but the path of the file that I'm printing and the current page number is also being printed along with the text as a header and footer.

Here's an image:

enter image description here

As you can see here, there is some unnecessary text at the top and the bottom which I don't want to print.

Also when I try to print python files, the text inside the file is not being printed at all:

enter image description here

Is there any way to fix these issues?

Lenovo 360
  • 569
  • 1
  • 7
  • 27
  • If you want to have control over printing, you need to print properly. ShellExecute isn't meant for this. Your entire approach is doomed to failure. I said this at your last question too. – David Heffernan Jan 26 '21 at 22:58

1 Answers1

1

Refer to ShellExecute, “Print”.

As David Heffernan said:

You are relying on the shell's associations to print the file, but that's a terribly brittle approach. If you right click on the file and select Print you'll observe the same behaviour as your call to ShellExecute.

And you can try to use the following code :

    if box == 1:
        filepath = "C:\\windows\\system32\\mshtml.dll,PrintHTML " + "\"" + dialog + "\"" 
        win32api.ShellExecute(0 , None , "rundll32.exe" , filepath , None , 1)

More reference : What parameters to pass to mshtml.dll?

Zeus
  • 3,703
  • 3
  • 7
  • 20
  • Thanks for your answer, this brings a setup window like I wanted, but when I try to print the file, only the path of the file is getting printed and it is not printing the text inside the file. Is there any way to fix this problem? – Lenovo 360 Jan 26 '21 at 08:55
  • @Lenovo360 This code works fine for me, do you test normal `.txt` or `.html` files. – Zeus Jan 26 '21 at 09:00
  • I'm trying to print a python file (`.py`) – Lenovo 360 Jan 26 '21 at 09:02
  • I tested these three file types and the code all worked fine for me. Maybe you need to confirm whether the selected printer can work normally. – Zeus Jan 26 '21 at 09:06
  • If you don't mind, should I edit my question and insert an image describing the issue I am facing? – Lenovo 360 Jan 26 '21 at 09:14
  • Yes, you can edit the question to describe your problem in detail – Zeus Jan 26 '21 at 09:15
  • Sorry for the late reply. It took me some time to capture the images. I have edited my question and described my issue, could you please help me out? – Lenovo 360 Jan 26 '21 at 09:46
  • This is the result of printing via `PrintHTML`, you can refer to [this thread](https://stackoverflow.com/questions/2573603/removing-page-title-and-date-when-printing-web-page-with-css/18960845). You cannot remove it. So if you need to print other types of files, also jump out of the setting window. According to the link in the original answer, you need to make sure that the machine's associations are configured to handle the type. This is cumbersome, so this is some limitation achieved by `ShellExecute`. – Zeus Jan 26 '21 at 09:54
  • @Lenovo360 I can print `.py` files normally. It is similar to normal text, can you provide more information? – Zeus Jan 26 '21 at 10:00
  • 1
    You can try using `Microsoft Print to PDF` to see if the `.py` file can be printed normally. If so, maybe your printing device does not support this feature. – Zeus Jan 27 '21 at 05:54