Python脚本,打印目录中的所有Word文件,按顺序打印文档。

huangapple go评论60阅读模式
英文:

Python script that prints all word files in directory prints the documents out of order

问题

这是您的Python脚本的翻译部分:

我有一个简短的Python脚本它查看我指定的目录并打印其中的所有文件Microsoft Word .docx格式)。

整个脚本如下

import os
from pathlib import Path

# 设置要打印文件的路径
my_path = Path(__file__).parent / 'OUTPUT'
string_path = str(my_path)

# 列出所选文件夹中的所有文件
list_files = os.listdir(my_path)

# 将文件列表转换为路径列表
list_paths = [os.path.join(str(my_path), file) for file in list_files]

# 此函数使用路径打印文件夹中的所有内容
def print_all():
    for item in list_paths:
        os.startfile(item, "print")

# 运行打印函数
print_all()

它可以工作也就是将所有文件发送到默认网络打印机并打印出来但是当它们从打印机上出来时文件的顺序是无序的尽管它们在目录中按文件名排序如下所示

01_firstdoc.docx
02_seconddoc.docx
03_thirddoc.docx
...以此类推...
20_twentiethdoc.docx

我阅读了以下帖子有关无序打印的帖子另一个帖子),其中一个没有回应另一个第一个帖子有一些评论建议添加等待时间然而我不确定如何实现这一点或者是否在我的情况下这确实是解决这个问题的方法虽然我理解延迟概念的原因但我也不确定评论中提到的竞态条件

运行Windows 10 Home和Python 3.10.5

编辑按照下面的建议将time.sleep()添加到for循环中已经解决了这个问题

def print_all():
    for item in list_paths:
        os.startfile(item, "print")
        time.sleep(10)

请注意,我只提供了脚本的翻译部分,没有包含问题或其他内容。如果需要更多信息或有其他问题,请随时提出。

英文:

I have a short Python script that looks in the directory I point it to and prints all of the files in it (Microsoft Word .docx format).

The entire script is below:

import os
from pathlib import Path


#set path where the files to print are
my_path = Path(__file__).parent / 'OUTPUT'
string_path = str(my_path)

#list all files in chosen folder
list_files = os.listdir(my_path)

#make the list of files into a list of paths
list_paths = [os.path.join(str(my_path), file) for file in list_files]

#this function prints everything in the folder using the paths
def print_all():
    for item in list_paths:
        os.startfile(item, "print")

#run the print function
print_all()

It works, as in it sends all the files to the default network printer and they do print, but the files are not in order when they come of the printer, although they are ordered in the directory by filename as such:

01_firstdoc.docx <br/>
02_seconddoc.docx <br/>
03_thirddoc.docx <br/>
..etc.. <br/>
20_twentiethdoc.docx <br/>

I read the following posts (post on out of order printing; another post), where one had no response, and another (first post) had some comments suggesting to add a wait time. However, I am not sure how to implement that, or if that is indeed the solution to this problem in my case. And while I understand the delay concept reasoning, I am also not sure about the race condition mentioned in the comments.

Running Windows 10 Home, and Python 3.10.5

Edit: Adding time.sleep() to the for loop has fixed the issue, as suggested below.

def print_all():
    for item in list_paths:
        os.startfile(item, &quot;print&quot;)
        time.sleep(10)

答案1

得分: 1

在目录中的文件不会按自然顺序排序;当您使用ls查看它们时,排序是由ls执行的,而不是由操作系统或文件系统执行的。(您可以使用选项更改它们的排序方式。)

只需在您的Python程序中对数组进行排序(例如,for item in sorted(list_paths):)。

英文:

Files in a directory are not sorted in any order naturally; when you look at them with ls, the sorting is done by ls, not the OS or file system. (And you can change how it sorts them with options.)

Just sort the array in your Python program (e.g. for item in sorted(list_paths):.

答案2

得分: 1

要实现延迟,只需导入time,然后在你的代码中在os.startfile(item, &quot;print&quot;)后面调用time.sleep(10)(数字代表秒数)。

检查它是否对你有用。它似乎帮助了你提到的第二个线程的人。

至于顺序,请确保你实际上对路径进行了排序并执行:for item in sorted(list_paths):

英文:

To implement the delay, you just need to import time

then call time.sleep(10) (the number represents the seconds) in your code, right behind os.startfile(item, &quot;print&quot;)

Check if it works to you. It seemed to help the person of the 2nd thread you mention.

And about the order, make sure you are actually ordering the paths and do: for item in sorted(list_paths):

huangapple
  • 本文由 发表于 2023年5月26日 01:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定