Python實現(xiàn)將HTML轉換成doc格式文件的方法示例
本文實例講述了Python實現(xiàn)將HTML轉換成doc格式文件的方法。分享給大家供大家參考,具體如下:
網(wǎng)頁上的一些文章,因為有格式的原因,它們在網(wǎng)頁上的源碼都是帶有html標簽的,用css來進行描述。本文利用HTML Parser 和docx兩個模塊,對網(wǎng)頁進行解析并存儲到word文檔中。轉換出來的格式相對還是有些粗糙,不喜勿噴。話不多說,直接上代碼。
class HTMLClient: #獲取html網(wǎng)頁源碼 def GetPage(self, url): #user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' user_agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/34.0.1847.116 Chrome/34.0.1847.116 Safari/537.36' headers = { 'User-Agent' : user_agent } req = urllib.request.Request(url, None, headers) try: res = urllib.request.urlopen(req) return res.read().decode("utf-8") except urllib.error.HTTPError as e: return None #獲取網(wǎng)絡圖片并保存在程序運行目錄下 def GetPic(self, url): user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-Agent' : user_agent } req = urllib.request.Request(url, None, headers) try: res = urllib.request.urlopen(req) return res.read() except urllib.error.HTTPError as e: return None
html到doc的轉換過程中,圖片保存和處理是比較麻煩的事情,因為可能涉及到圖片格式錯誤,因此為了保證圖片正常運行,應當修改圖片添加異常處理流程。
class MYHTMLParser(HTMLParser): def __init__(self, docfile): HTMLParser.__init__(self) self.docfile = docfile self.doc = Document(docfile) self.myclient = HTMLClient() self.text = '' self.title = False self.isdescription = False self.picList=[] #根據(jù)標簽頭類型決定標簽內(nèi)容的格式 def handle_starttag(self, tag, attrs): #print "Encountered the beginning of a %s tag" % tag self.title = False self.isdescription = False #<h1>標簽說明其中的內(nèi)容是標題 if re.match(r'h(\d)', tag): self.title = True #圖片的處理比較復雜,首先需要找到對應的圖片的url,然后下載并寫入doc中 #下載的圖片格式如果有問題,docx模塊會報錯,因此重新定義異常處理 #圖片名稱需要記錄下來,在文檔保存后要自動刪除 if tag == "img": if len(attrs) == 0: pass else: for (variable, value) in attrs: if variable == "src": #此處圖片url類型為[http://url/pic.img!200*200] #不同網(wǎng)站圖片類型不同,因此當作不同處理 picdata = self.myclient.GetPic(value.split('!')[0]) if picdata == None: pass else: pictmp = value.split('/')[-1].split('!')[0] picfix = value.split('/')[-1].split('!')[-1] with open(pictmp, 'wb') as pic: pic.write(bytes(picdata)) pic.close() try: if picfix[0:1] == 'c': self.doc.add_picture(pictmp, width=Inches(4.5)) else: self.doc.add_picture(pictmp)#, width=Inches(2.25)) except docx.image.exceptions.UnexpectedEndOfFileError as e: print(e) self.picList.append(pictmp) #javascript腳本 if tag == 'script': self.isdescription = True def handle_data(self, data): if self.title == True: if self.text != '': self.doc.add_paragraph(self.text) self.text = '' self.doc.add_heading(data, level=2) if self.isdescription == False: self.text += data def handle_endtag(self, tag): #if tag == 'br' or tag == 'p' or tag == 'div': if self.text != '': self.doc.add_paragraph(self.text) self.text = '' def complete(self, html): self.feed(html) self.doc.save(self.docfile) for item in self.picList: if os.path.exists(item): os.remove(item)
更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結》、《Python圖片操作技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python Socket編程技巧總結》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
相關文章
Python利用wxPython模塊打造ChatGPT式打字效果程序
這篇文章主要為大家介紹了如何利用Python和wxPython模塊打造一個ChatGPT式打字效果程序,從而增強用戶體驗或提高應用程序的可讀性,感興趣的可以了解一下2023-05-05