Kivy:- 加载和保存不适用于 pdf 和 word 文档
Kivy:- Load & Save does not work for pdf & word docment
我正在阅读 kivy pdf 文件,并在其中找到了加载和保存代码...该代码适用于 .txt 文件和 .py 文件,但不适用于 .docx 和 .pdf 文件。 ..
main.py:---
import kivy
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.factory import Factory
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
import os
class LoadDialog(FloatLayout):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
class SaveDialog(FloatLayout):
save = ObjectProperty(None)
#text_input = ObjectProperty(None)
cancel = ObjectProperty(None)
class Root(FloatLayout):
loadfile = ObjectProperty(None)
savefile = ObjectProperty(None)
#text_input = ObjectProperty(None)
def dismiss_popup(self):
self._popup.dismiss()
def show_load(self):
content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
print(self.load)
self._popup = Popup(title="Load file", content=content, size_hint=(0.9, 0.9))
self._popup.open()
def show_save(self):
content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
print(self.save)
self._popup = Popup(title="Save file", content=content, size_hint=(0.9, 0.9))
self._popup.open()
def load(self, path, filename):
with open(os.path.join(path, filename[0])) as stream:
self.text = stream.read()
self.dismiss_popup()
self._popup = Popup(title="Load file", content=Label(text = 'File Loaded Successfully'), size_hint=(0.9, 0.9))
self._popup.open()
def save(self, path, filename):
with open(os.path.join(path, filename), 'w') as stream:
stream.write(self.text)
self.dismiss_popup()
self._popup = Popup(title="Save file", content=Label(text = 'File Saved Successfully'), size_hint=(0.9, 0.9))
self._popup.open()
class Editor(App):
pass
Factory.register('Root', cls=Root)
Factory.register('LoadDialog', cls=LoadDialog)
Factory.register('SaveDialog', cls=SaveDialog)
if __name__ == '__main__':
Editor().run()
editor.kv:---
#:kivy 1.8.0
Root:
#text_input: text_input
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: None
height: 100
Button:
text: 'Load'
on_release: root.show_load()
Button:
text: 'Save'
on_release: root.show_save()
Image:
source: 'myimage.png'
<LoadDialog>:
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
BoxLayout:
size_hint_y: None
height: 100
Button:
text: "Cancel"
on_release: root.cancel()
Button:
text: "Load"
on_release: root.load(filechooser.path, filechooser.selection)
<SaveDialog>:
text_input: text_input
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
on_selection: text_input.text = self.selection and self.selection[0] or ''
TextInput:
id: text_input
size_hint_y: None
height: 100
multiline: False
BoxLayout:
size_hint_y: None
height: 100
Button:
text: "Cancel"
on_release: root.cancel()
Button:
text: "Save"
on_release: root.save(filechooser.path, text_input.text)
我应该修改代码的哪一部分,以便它适用于所有类型的文件...????
.docx
和 .pdf
不是基于文本的格式。您可以加载 .txt
和 .py
文件,因为它们是基于文本的。您所做的本质上就像尝试将 .png
作为文本加载 - .png
是二进制格式,就像 .docx
和 .pdf
.
为了显示.docx
,您需要学习文件格式并编写自己的加载程序,或使用现有的加载程序。快速 Google 搜索结果 python-docx。一旦你加载了文件,如果你想显示它,那么你需要编写自己的代码来在 Kivy 中显示它,或者将它转换成 Kivy 已经支持的格式,比如 reStructuredText。
同样,对于 .pdf
,Google 会找到 pyPdf2。同样,您需要将其转换为 rST 或生成 Kivy widgets/instructions 以自己显示文档。
我正在阅读 kivy pdf 文件,并在其中找到了加载和保存代码...该代码适用于 .txt 文件和 .py 文件,但不适用于 .docx 和 .pdf 文件。 ..
main.py:---
import kivy
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.factory import Factory
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
import os
class LoadDialog(FloatLayout):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
class SaveDialog(FloatLayout):
save = ObjectProperty(None)
#text_input = ObjectProperty(None)
cancel = ObjectProperty(None)
class Root(FloatLayout):
loadfile = ObjectProperty(None)
savefile = ObjectProperty(None)
#text_input = ObjectProperty(None)
def dismiss_popup(self):
self._popup.dismiss()
def show_load(self):
content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
print(self.load)
self._popup = Popup(title="Load file", content=content, size_hint=(0.9, 0.9))
self._popup.open()
def show_save(self):
content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
print(self.save)
self._popup = Popup(title="Save file", content=content, size_hint=(0.9, 0.9))
self._popup.open()
def load(self, path, filename):
with open(os.path.join(path, filename[0])) as stream:
self.text = stream.read()
self.dismiss_popup()
self._popup = Popup(title="Load file", content=Label(text = 'File Loaded Successfully'), size_hint=(0.9, 0.9))
self._popup.open()
def save(self, path, filename):
with open(os.path.join(path, filename), 'w') as stream:
stream.write(self.text)
self.dismiss_popup()
self._popup = Popup(title="Save file", content=Label(text = 'File Saved Successfully'), size_hint=(0.9, 0.9))
self._popup.open()
class Editor(App):
pass
Factory.register('Root', cls=Root)
Factory.register('LoadDialog', cls=LoadDialog)
Factory.register('SaveDialog', cls=SaveDialog)
if __name__ == '__main__':
Editor().run()
editor.kv:---
#:kivy 1.8.0
Root:
#text_input: text_input
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: None
height: 100
Button:
text: 'Load'
on_release: root.show_load()
Button:
text: 'Save'
on_release: root.show_save()
Image:
source: 'myimage.png'
<LoadDialog>:
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
BoxLayout:
size_hint_y: None
height: 100
Button:
text: "Cancel"
on_release: root.cancel()
Button:
text: "Load"
on_release: root.load(filechooser.path, filechooser.selection)
<SaveDialog>:
text_input: text_input
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
on_selection: text_input.text = self.selection and self.selection[0] or ''
TextInput:
id: text_input
size_hint_y: None
height: 100
multiline: False
BoxLayout:
size_hint_y: None
height: 100
Button:
text: "Cancel"
on_release: root.cancel()
Button:
text: "Save"
on_release: root.save(filechooser.path, text_input.text)
我应该修改代码的哪一部分,以便它适用于所有类型的文件...????
.docx
和 .pdf
不是基于文本的格式。您可以加载 .txt
和 .py
文件,因为它们是基于文本的。您所做的本质上就像尝试将 .png
作为文本加载 - .png
是二进制格式,就像 .docx
和 .pdf
.
为了显示.docx
,您需要学习文件格式并编写自己的加载程序,或使用现有的加载程序。快速 Google 搜索结果 python-docx。一旦你加载了文件,如果你想显示它,那么你需要编写自己的代码来在 Kivy 中显示它,或者将它转换成 Kivy 已经支持的格式,比如 reStructuredText。
同样,对于 .pdf
,Google 会找到 pyPdf2。同样,您需要将其转换为 rST 或生成 Kivy widgets/instructions 以自己显示文档。