python --读rtf文件(转excel格式)
读rtf文件
pip install pywin32
from win32com.client import Dispatch, DispatchEx
# word = Dispatch('Word.Application') # 打开word应用程序
word = DispatchEx('Word.Application') # 启动独立的进程
word.Visible = False # 后台运行,不显示
word.DisplayAlerts = False # 不警告
path = r'C:\Users\EDY\Desktop\GD0101-06.rtf' # 写绝对路径,相对路径会拨错
doc = word.Documents.Open(FileName=path, Encoding='gbk')
for para in doc.paragraphs:
print(para.Range.Text)
doc.Close()
word.Quit()
转excel格式
import os
import win32com.client as win32
def save_as_xlsx(fname):
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(fname)
wb.SaveAs(fname+"x", FileFormat = 51) #FileFormat = 51 is for .xlsx extension
wb.Close() #FileFormat = 56 is for .xls extension
excel.Application.Quit()
if __name__ == "__main__":
package = "E:\\你的xls所在的文件夹\\"
files = os.listdir(package)
for fname in files:
if fname.endswith(".xls"):
print(fname + "正在进行格式转换,请稍后~")
save_as_xlsx(package + fname)
print(fname + "格式转换完成O(∩_∩)O哈哈~")
else:
print("跳过非xls文件:"+fname)