2012-10-18 582 views
2

有沒有一種方法可以高效地獲得Python文檔(.doc,.docx)的頁面數量?用Python編寫的word文檔的頁數

對於.odt文件?

我想在基於Linux上的Web2py的Web應用程序中使用它。

謝謝!

+2

對於DOCX,有可能使您可以訪問到Word文檔的XML一個Python模塊['docx'(https://github.com/mikemaccana/python-docx) 。這可能有也可能沒有頁數。 –

回答

2

可以在DOCX包讀取docProps/app.xml中值

<Properties> 
<Pages>CountValue</Pages> 

或ODT包

<office:document-meta> 
    <office:meta> 
     <meta:document-statistic meta:page-count="CountValue"> 

形式了meta.xml。

如果不存在這些值(它們是可選的),你必須使整個文檔的計算,實際上執行的渲染,更加困難

3

只對那些誰搜索此博客進入....

from win32com.client import Dispatch 
#open Word 
word = Dispatch('Word.Application') 
word.Visible = False 
word = word.Documents.Open(doc_path) 

#get number of sheets 
word.Repaginate() 
num_of_sheets = word.ComputeStatistics(2) 
+0

使用pywin32軟件包的優秀答案。爲我工作得非常好。謝謝。 – kstratis

+0

我嘗試了win32com解決方案,並且出現此錯誤:'''object has no attribute'Repaginate'' Does'Repaginate' get deprecated? – Jed