python word 读取section.page_height参数错误

发布于:2025-05-25 ⋅ 阅读:(20) ⋅ 点赞:(0)

python  word 读取section.page_height参数错误

最近在做报告自动化,发现调用word模板,或程序新建模板,只要调用section.page_height获得的页数据就不正常,

from docx import Document
from docx.shared import Cm, Twips

doc = Document()
section = doc.sections[0]

print("Page Height (Twips):", section.page_height)  # 正常应为 15840
print("Page Height (Cm):", Twips(section.page_height).cm)  # 直接转换为厘米
D:\od172406\venv\Scripts\python.exe D:\od172406\常用文件\调试\sdfdsfaa.py 
Page Height (Twips): 10058400
Page Height (Cm): 17741.9

进程已结束,退出代码为 0

(venv) D:\od172406 git:[master]
pip install --upgrade python-docx
Requirement already satisfied: python-docx in d:\od172406\venv\lib\site-packages (1.0.1)
Collecting python-docx
  Using cached python_docx-1.1.2-py3-none-any.whl.metadata (2.0 kB)
Requirement already satisfied: lxml>=3.1.0 in d:\od172406\venv\lib\site-packages (from python-docx) (5.4.0)
Requirement already satisfied: typing-extensions>=4.9.0 in d:\od172406\venv\lib\site-packages (from python-docx) (4.13.2)
Using cached python_docx-1.1.2-py3-none-any.whl (244 kB)
Installing collected packages: python-docx
  Attempting uninstall: python-docx
    Found existing installation: python-docx 1.0.1
    Uninstalling python-docx-1.0.1:
      Successfully uninstalled python-docx-1.0.1
Successfully installed python-docx-1.1.2

换了几个版本的python_docx,都是如此结果,是什么变了呢

最后只有换思路实现课任务,在此记录,有时间好好研究一下。

from docx import Document
from docx.shared import Cm, Twips

doc = Document()
section = doc.sections[0]
print(section)
from docx.shared import Cm, Pt
# 设置页面高度为29.7厘米(A4纸的高度)
section.page_height = Cm(29.7)
print("Page Height (Twips):", section.page_height)  # 正常应为 15840
print("Page Height (Cm):", Twips(section.page_height).cm)  # 直接转换为厘米

结果:

D:\od172406\venv\Scripts\python.exe D:\od172406\常用文件\调试\sdfdsfaa.py 
<docx.section.Section object at 0x0000021D756B9D30>
Page Height (Twips): 10692130
Page Height (Cm): 18859.729305555556

进程已结束,退出代码为 0

有空研究,查清了原因,版本升级后单位变了

from docx import Document
from docx.shared import Cm, Twips

doc = Document()
section = doc.sections[0]
print(section)
from docx.shared import Cm, Pt
# 设置页面高度为29.7厘米(A4纸的高度)
print("Page Height (Emus):", section.page_height)  # 正常应为 15840
section.page_height = Cm(29.7)
print("Page Height Emus):", section.page_height)  # 正常应为 15840
print("Page Height (Twips):", section.page_height/635)  # 正常应为 15840
print("Page Height (Cm):", section.page_height/Cm(1.0))  # 直接转换为厘米
print('cm',Cm(1.0))
print('ccc,',section.page_height/Cm(1.0))

<docx.section.Section object at 0x0000017D519C1AF0>
Page Height (Emus): 10058400
Page Height Emus): 10692130
Page Height (Twips): 16838.0
Page Height (Cm): 29.70036111111111
cm 360000
ccc, 29.70036111111111
Page Height (Cm): 29.70036111111111
 

查看文件原码:

class Length(int):
    """Base class for length constructor classes Inches, Cm, Mm, Px, and Emu.

    Behaves as an int count of English Metric Units, 914,400 to the inch, 36,000 to the
    mm. Provides convenience unit conversion methods in the form of read-only
    properties. Immutable.
    """

    _EMUS_PER_INCH = 914400
    _EMUS_PER_CM = 360000
    _EMUS_PER_MM = 36000
    _EMUS_PER_PT = 12700
    _EMUS_PER_TWIP = 635

网站公告

今日签到

点亮在社区的每一天
去签到