Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
So, we cannot work with these documents using normal text editors. But, we can manipulate these word documents in python using the python-docx module.
Bàn luận The first step is to install this third-party module python-docx. You can use pip “pip install python-docx” or download the tarball from here. Here’s the Github repository.
Tài liệu Word chứa văn bản được định dạng trong ba cấp đối tượng. Các đối tượng cấp cấp thấp nhất, các đối tượng đoạn trung bình và đối tượng tài liệu cấp cao nhất. Vì vậy, chúng ta không thể làm việc với các tài liệu này bằng cách sử dụng các trình chỉnh sửa văn bản thông thường. Nhưng, chúng ta có thể điều khiển các tài liệu từ này trong Python bằng mô-đun Python-DOCX. After installation import “docx” NOT “python-docx”.
3. Use “docx.Document” class to start working with the word document.
1. Bước đầu tiên là cài đặt mô-đun bên thứ ba này Python-docx. Bạn có thể sử dụng PIP PIP PIP Cài đặt Python-Docx, hoặc tải xuống tarball từ đây. Ở đây, kho lưu trữ GitHub.
2. Sau khi cài đặt nhập, DOCX, không phải là Python-docx .3. Sử dụng lớp docx.document của lớp để bắt đầu làm việc với tài liệu từ.
Mã số 1:
doc.add_heading('Heading for the document'DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 0DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 1DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 2
import docx
doc = docx.Document()
DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 8 UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 74: character maps to <undefined> 4DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 2
DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 3= DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 5DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 6DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 2
import1
doc.add_heading(import3DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 0import5DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 2
import7import8DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 2
docx0docx1DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 2
Output:
DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 8DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 9 UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 74: character maps to <undefined> 0= UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 74: character maps to <undefined> 2
Code #2: Now, to open a word document, create an instance along with passing the path to the document.
DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 8 UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 74: character maps to <undefined> 7 UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 74: character maps to <undefined> 8= UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 74: character maps to <undefined> 2
Lưu ý rằng ngắt trang trong trang thứ hai.
doc 2doc 3doc 4DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 2
doc 2doc 7
doc 2doc 3=0DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 2
doc 2=3DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 1=5
doc 2doc 3=8DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 2
doc 2=3DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 1docx.Document()3
doc 2doc 3docx.Document()6DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 2
docx3 docx4import docx6
doc.add_heading(2doc 2doc.add_heading(4
Output:
List of paragraph objects:->>> [<docx.text.paragraph.Paragraph object at 0x7f45b22dc128>, <docx.text.paragraph.Paragraph object at 0x7f45b22dc5c0>, <docx.text.paragraph.Paragraph object at 0x7f45b22dc0b8>, <docx.text.paragraph.Paragraph object at 0x7f45b22dc198>, <docx.text.paragraph.Paragraph object at 0x7f45b22dc0f0>] List of runs objects in 1st paragraph:->>> [<docx.text.run.Run object at 0x7f45b22dc198>] Text in the 1st paragraph:->>> Heading for the document The whole content of the document:->>> Heading for the document Your paragraph goes here, hey there, bold here, and these words are italic Heading level 2doc = docx9doc 0DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close() 2//python-docx.readthedocs.io/en/latest/#user-guide.
Tôi đang cố gắng tự động mở một tài liệu từ trong Python. Tôi rất mới để lập trình và tôi nghe trang này đã giúp những người gặp rắc rối với nó.
Tôi đã xem xét các câu hỏi khác nhau và đã tìm thấy điều này:
DummyFile = path_to_docx with open(DummyFile) as f: source_stream = io(f.read()) document = doc(source_stream) source_stream.close()Nhưng khi tôi chạy nó, tôi nhận được:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 74: character maps to <undefined>Có vẻ như mã này không phải là những gì tôi muốn. Tôi tin rằng nó đang cố gắng đọc tài liệu của tôi và đưa nó vào một biến. Không phải những gì tôi muốn. Tôi muốn lệnh, khi được thực thi, sẽ mở tài liệu từ trong Microsoft Wordin Microsoft Word
Kết quả dự kiến:
Tài liệu Word mở ra trong Microsoft Word, như có thể thấy ở đây: