根据 python 中集合的内容打印语句时出错

Error in printing statements based on the contents of a set in python

我目前正在使用 python-docx 库(0.8 版)在 PyCharm(社区版 2021.3.3)上编写一些 python(3.10.4 版)代码。 1.1),可以检查Word文档中使用的纸张是否为A4。该文档包含通过 2x 分节符(下一页)创建的三个部分。

该代码(如下图)的目的是根据版块的页宽和页高来打印特定的语句document.The代码只实现了部分功能

当整个文档不是A4时,应该打印“整个文档不包含A4尺寸的纸张”。但它并没有这样做,而是打印“第 1 节不包含 A4 尺寸的纸张”。当第 1 节和第 2 节不是 A4 时(其中打印“第 1 节不包含 A4 尺寸的纸张”),当第 1 节和第 3 节不是 A4 时(其中奇怪地打印“整个文档不包含不包含 A4 尺寸的纸张。”) 并且当第 2 部分和第 3 部分不是 A4 时(其中打印“第 2 部分不包含 A4 尺寸的纸张。”)。

我想知道如何解决这些问题以确保代码打印出正确的语句。我相信问题可能出在 if-elif 块中。然而,我基本上没有找到问题的根源。

我们将不胜感激任何形式的帮助。代码有什么问题欢迎追问

from docx import Document

# access Word document file
WordFile = Document("Report_T3.docx")

# the number in the square brackets refer to the section
# page section starts at 0 as that is what indexing begins at in python
section1 = WordFile.sections[0]  # 1st section (Title page)
section2 = WordFile.sections[1]  # 2nd section (Preliminary pages)
section3 = WordFile.sections[2]  # 3rd section (Introduction - End of Doc)

section1_pgheight = set()  # store section 1 page height in the set section1_pgheight
section1_pgwidth = set()   # store section 1 page width in the set section1_pgwidth
section2_pgheight = set()  # store section 2 page height in the set section2_pgheight
section2_pgwidth = set()   # store section 2 page width in the set section2_pgwidth
section3_pgheight = set()  # store section 3 page height in the set section3_pgheight
section3_pgwidth = set()   # store section 3 page width in the set section3_pgwidth

# loop through all three sections in the document
for section in WordFile.sections:
    # add section 1 page height into the set section1_pgheight
    section1_pgheight.add(section1.page_height)
    # add section 1 page width into the set section1_pgwidth
    section1_pgwidth.add(section1.page_width)
    # add section 2 page height into the set section2_pgheight
    section2_pgheight.add(section2.page_height)
    # add section 2 page width into the set section2_pgwidth
    section2_pgwidth.add(section2.page_width)
    # add section 3 page height into the set section3_pgheight
    section3_pgheight.add(section3.page_height)
    # add section 3 page width into the set section3_pgwidth
    section3_pgwidth.add(section3.page_width)

# check if all pages in all sections are A4; for A4 height == 10692130, width == 7560310
if {10692130} == section1_pgheight == section2_pgheight == section3_pgheight and {7560310} == section1_pgwidth == section2_pgwidth == section3_pgwidth:
    print("Whole document contains A4 size paper.")  # works

# check if all pages in all sections are not A4; for A4 height == 10692130, width == 7560310
elif {10692130} != section1_pgheight != section2_pgheight != section3_pgheight and {7560310} != section1_pgwidth != section2_pgwidth != section3_pgwidth:
    print("Whole document does not contain A4 size paper.")  # not working

# check if page in section 1 is not A4; for A4 height == 10692130, width == 7560310
elif {10692130} != section1_pgheight and {7560310} != section1_pgwidth:
    print("Section 1 does not contain A4 size paper.")  # works

# check if pages in section 2 are not A4; for A4 height == 10692130, width == 7560310
elif {10692130} != section2_pgheight and {7560310} != section2_pgwidth:
    print("Section 2 does not contain A4 size paper.")  # works

# check if pages in section 3 are not A4; for A4 height == 10692130, width == 7560310
elif {10692130} != section3_pgheight and {7560310} != section3_pgwidth:
    print("Section 3 does not contain A4 size paper.")  # works

# check if pages in sections 1 and 2 are not A4; for A4 height == 10692130, width == 7560310
elif {10692130} != section1_pgheight != section2_pgheight and {7560310} != section1_pgwidth and {7560310} != section2_pgwidth:
    print("Sections 1 and 2 do not contain A4 size paper.")  # not working

# check if pages in sections 1 and 3 are not A4; for A4 height == 10692130, width == 7560310
elif {10692130} != section1_pgheight and {10692130} != section3_pgheight and {7560310} != section1_pgwidth and {7560310} != section3_pgwidth:
    print("Sections 1 and 3 do not contain A4 size paper.")  # not working

# check if pages in sections 2 and 3 are not A4; for A4 height == 10692130, width == 7560310
elif {10692130} != section2_pgheight and {10692130} != section3_pgheight and {7560310} != section2_pgwidth and {7560310} != section3_pgwidth:
    print("Sections 2 and 3 do not contain A4 size paper.")  # not working

请注意,A4 != A3 != A4 的计算结果为 True,因此您不是在检查是否所有内容都不是 A4,而是检查第 1 节是否不是 a4 以及第 2 节是否与第 1 节不同,依此类推。 ..

相反,您必须这样做:

elif section1_pgheight != {10692130} and section2_pgheight != {10692130} and section3_pgheight != {10692130} and section1_pgwidth != {7560310} and section2_pgwidth != {7560310} and section3_pgwidth != {7560310}:
    print("Whole document does not contain A4 size paper.")

我不明白使用集合的意义,因为你只能检查是否相等,另外,避免重复自己,更好的代码是:

sections = [] #use a list to perserve order
# original loop was not doing anything so remove it
for section in WordFile.sections:
    sections.append((section.page_height, section.page_width)) #add dimension tuple

def isA4(dimensions): 
    A4_WIDTH = 7560310 #avoid magic numbers
    A4_HEIGHT = 10692130
    height, width = dimensions #unpack dimesnsion tuple
    # define helper which would remove a lot of the repetetive checking
    return height == A4_HEIGHT and width == A4_WIDTH

#get the number of sections that are not a4, also turn them into strings
not_a4 = [str(i + 1) for i, section in enumerate(sections) if not isA4(section)]

if len(not_a4) == len(sections):
    print("Whole doc. is not A4!")
elif len(not_a4) == 1:
    #list has one element e.g. [1]
    print(f"Section: {not_a4[0]} is not A4!") 
elif 0 < len(not_a4) < len(sections):
    print(f"Sections {', '.join(not_a4)} are not A4!")