当前位置: 首页 > news >正文

python word 读取section.page_height参数错误

python  word 读取section.page_height参数错误

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

from docx import Document
from docx.shared import Cm, Twipsdoc = 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, Twipsdoc = 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, Twipsdoc = 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 themm. Provides convenience unit conversion methods in the form of read-onlyproperties. Immutable."""_EMUS_PER_INCH = 914400_EMUS_PER_CM = 360000_EMUS_PER_MM = 36000_EMUS_PER_PT = 12700_EMUS_PER_TWIP = 635
http://www.dtcms.com/a/210217.html

相关文章:

  • HarmonyOS:相机选择器
  • Halcon 单目相机标定测量
  • [python]opencv从源码编译python版本发现不生成cv2.pyd
  • 游戏引擎学习第309天:用于重叠检测的网格划分
  • 考研408《计算机组成原理》复习笔记,第二章(3)数值数据的运算和存储(定点数计算)
  • 实战教程:基于Vue.js与Django REST Framework的任务管理SPA开发全流程
  • WPF按钮Radius化
  • 如何选择支持AI接入的开发语言与框架
  • .NET ORM开发手册:基于SqlSugar的高效数据访问全攻略
  • 伴随矩阵 -- 代数余子式矩阵的转置
  • .NET外挂系列:8. harmony 的IL编织 Transpiler
  • Docker 镜像标签(Tag)规范与要求
  • AtCoder AT_abc407_d [ABC407D] Domino Covering XOR
  • JavaScript从入门到精通(一)
  • 深入理解Redis线程模型
  • stable diffusion论文解读
  • RabbitMQ 概述
  • MMaDA——开源首个多模态大扩散语言模型
  • 常见实验室器材采购渠道分享
  • 【软考向】Chapter 2 程序设计语言基础知识
  • DAY 35
  • 【Java Web】3.SpringBootWeb请求响应
  • 软件测试文档怎么写?软件渗透测试报告
  • 【R语言科研编程-散点图】
  • commonmark.js 源码阅读(二) - Inline Parser
  • R基于多元线性回归模型实现汽车燃油效率预测及SHAP值解释项目实战
  • 使用Spring Boot和Redis实现高效缓存机制
  • HarmonyOS:相机管理
  • Spring Boot微服务架构(四):微服务的划分原则
  • 工业智能网关建立烤漆设备故障预警及远程诊断系统