Linux環境下Python操作word

2021-07-25 15:48:41 字數 1461 閱讀 7315

1、建立word模板(使用$標識key)

2、將word模板另存為*.xml

#res中是需要在word中顯示的內容

res={}

res['test01']=u"測試1"

res['test02']=u"測試2"

#word模板讀入

input_file = file("/home/ubuntu/workspace/test.xml", 'r')

#生成word位置

output_file = file("/home/ubuntu/workspace/result.doc", 'w')

#將res中的內容替換word中的標籤並輸出

#實際上就是將xml以字串讀入,然後替換掉指定識別符號

s = template(input_file.read().decode("utf-8"))

#使用安全替換,即模板中的key不存在時不報錯

output = s.safe_substitute(res)

output_file.write(output.encode("utf-8"))

output_file.close()

4、結果

使用$時,需要將識別符號及內容一次性輸入,否則生成xml中$會斷成幾節,如下

解決辦法:使用word開啟生成的xml檔案,選中片段,ctrl+x ctrl+v剪下之後貼上進去,再ctrl+s即可

解決方法:

在**中加入sys.setdefaultencoding('utf8')或在中文字元後加.encode("utf8")

linux環境下kingbase的簡單操作命令

1 切換使用kingbase su kingbase 2 檢視版本資訊 kingbase v 3 檢視54321埠使用情況 netstat an grep 54321 4 檢視kingbase的程序 ps aux grep kingbase ps ef grep kingbase 5 啟動資料庫 在...

python虛擬環境下使用opencv操作攝像頭

使用攝像頭例項 coding utf 8 import cv2 defvideo capture camera cv2.videocapture 0 while true ret,frame camera.read if ret true image cv2.cvtcolor frame,cv2.c...

Linux下Python環境搭建

linux 下python環境搭建 系統環境 os oracle linux enterprise 5.6 python 位址 版本 python 2.7.3.tgz setuptools 版本 setuptools 0.6c11.tar.gz md5 wget 2.安裝python 檢視系統自帶的...