我目前正在使用 python-docx 庫(版本 0.8.1.1)在 PyCharm(社區版 2021.3.3)上撰寫一些 python(版本 3.10.4)代碼,它允許檢查 Word 檔案中使用的紙張是否為 A4。該檔案包含通過 2x Section Break(下一頁)創建的三個部分。
代碼(如下所示)的目的是根據檔案中各個部分的頁面寬度和頁面高度列印特定的陳述句。代碼僅部分功能來實作此目的。
當整個檔案不是 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
uj5u.com熱心網友回復:
請注意,這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.height, section.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
# check if all sections are A4
sections_a4 = [*map(isA4, sections)]
if not any(sections_a4):
print("Whole Doc. not A4")
else:
# loop over section num and whether it is A4
for i, b in enumerate(sections_a4): # iterable (1, True) ...
if not b: #section is not A4
print(f"Section {i} is not A4!")
# you could break here if you want to only print once
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/466204.html
