2012-02-03 12 views
-3

的PDF我有一些Python代碼創建了HTML文件系統調用的函數...寫作,對於在python

filename = './resources/part2.pdf 

if not os.path.exists(filename): 

    cmd = 'pdftohtml %s' % filename 
    os.system(cmd) 

htmlFilename = './resources/part2s.html' 
htmlSource = open(htmlFilename, 'r') 

我有一個PDF文件,我想將它轉換爲HTML文件。首先我檢查它是否在 系統中退出。如果不是,我想通過使用已經在系統中退出的pdftohtml進行轉換。我如何編寫一個系統調用函數?另外,我想動態地生成文件名。歡迎任何答案。

+0

在什麼語言/框架下,這太模糊了,無法回答。 – 2012-02-03 14:23:21

+0

對不起,語言是python – Barbaros26 2012-02-03 14:35:42

+0

你問如何檢查文件的存在? – 2012-02-03 14:39:43

回答

1

那我們來看看......你的代碼沒有多大意義。 pdftohtml需要一個pdf作爲輸入並生成html文件。所以來你的代碼:

if not os.path.exists(filename): 
     cmd = 'pdftohtml %s' % filename 
     os.system(cmd) 

你是否看到上述問題?如果文件不存在,你怎麼能通過它作爲輸入?因此,如果系統中不存在pdf文件,則無法對此做任何處理。另外我建議使用subprocess.call而不是os.system。您的評論後,如果您要檢查如果HTML文件中存在前手你爲什麼要檢查的

import subprocess 

input_file = 'resources/part2.pdf' 
output_file = 'resources/part2s.html' 
if os.path.exists(input_file): 
    subprocess.call(['pdftohtml', input_file, output_file]) 

編輯

好:

我的代碼重寫爲類似pdf在你的條件?你可以做這樣的事情,如果我現在undestrand更精確的是你想要的:

import subprocess 

input_file = 'resources/part2.pdf' 
output_file = input_file.replace('.pdf', '.html') 
if os.path.exists(input_file) and not os.path.exists(output_file): 
    subprocess.call(['pdftohtml', input_file, output_file])  
+0

我明白了,但我已經有了pdf,我想檢查一下html文件是否在轉換之前。如果沒有html文件,那麼我想通過使用pdftohtml轉換它。 – Barbaros26 2012-02-03 15:05:12