PyQt5读写⽂件
⼀、读写txt⽂件,指定⽂件数据类型
def my_open(self):
my_file_path = OpenFileName(None, '选择⽂件', r'C:\Users\Administrator\Desktop','Excel files(*.txt)')
with open(my_file_path[0]) as f:
my_file = f.read()
def my_save(self):
my_data = PlainText()
my_file = SaveFileName(None,'⽂件保存',r'C:\Users\Administrator\Desktop','Excel files(*.txt)')
fs = open(my_file[0],'a+')
fs.write(my_data)
fs.close()
⼆、读取Word⽂件:
def my_open(self):
from win32com import client as wc
word = wc.Dispatch('Word.Application')
# 不打开word程序
word.Visible = 0
getsavefilename
my_file_path = OpenFileName(None, '选择⽂件', r'C:\Users\Administrator\Desktop',"word⽂件(*.docx , *.doc))")        myworddoc = word.Documents.Open(my_file_path[0])
#获取有多少段落
my_count = myworddoc.Paragraphs.Count
#循环取出段落,显⽰到显⽰框中
for i in range(my_count):
my_pr = myworddoc.Paragraphs[i].Range
myworddoc.Close()
  或者是:
def my_open(self):
import docx
my_file_path = OpenFileName(None, '选择⽂件', r'C:\Users\Administrator\Desktop',"
word⽂件(*.docx , *.doc))")        doc = docx.Document(my_file_path[0])
for i in doc.paragraphs:
三、读取Ecxle⽂件⼀样的操作,⽅法⽐较多如xlrd  pandas等等的,不做记录

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。