Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a new document by word operation in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to operate word in python to build a new document, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The win32com module mainly provides Python with the function of calling the operation of the underlying com components of windows. COM components are a set of interface specifications developed by Microsoft for the software production of the computer industry to be more in line with human behavior. Many large-scale software use COM to provide API, but it is only used on windows at present. A COM component can be a Dll (in-process component) or an EXE (out-of-process component). A COM component can contain multiple COM objects, and a COM object can have multiple interfaces.

MS Office and WPS can be called through the underlying com components. Compared with python-docx, pydocx or modifying the xml file of docx, win32com can directly use the relevant interfaces of VBA, which is more flexible and applicable.

First, install pip install pypiwin32

2. Create a new document import os

From win32com.client import Dispatch, DispatchEx# to get the current path

Pwd = os.getcwd ()

# app = DispatchEx ('Word.Application') # MS word

App = DispatchEx ("Kwps.Application") # WPS# New word document

Doc = app.Documents.Add ()

# Open the document

# doc = app.Documents.Open ('document path')

# display the update interface

App.Visible = True

App.ScreenUpdating = True

# insert content at the beginning of the document

Doc_range1 = doc.Range (0,0)

Doc_range1.InsertBefore ('hello python word docking')

# insert content at the end of the document

Doc_range2 = doc.Range ()

Doc_range2.InsertAfter ('word end')

# add content to the specified location of the document

I = 5

Doc_range3 = doc.Range (0, I)

Doc_range3.InsertAfter ("insert position i")

# must be saved and saved using an absolute path

# doc.Save () # if the document has not been saved before, the Save as dialog box prompts the user to type a file name.

Doc.SaveAs (os.path.join (pwd, 'python_word.docx'))

# close the word document

Doc.Close ()

# turn off the software

App.Quit ()

At this point, a word file named python_word.docx is created under the current path, which contains: hello insert position i python word docking word end

3. Pictures of word related objects

Application: represents a Word application. The Application object contains properties and methods that return the top-level object, including menu bars, toolbars, commands, and all documents.

There are two ways to get the Application object:

Dispatch: create a new instance

DispatchEx: start a separate process

Documents: a document object that can store multiple Document, which is equivalent to word opening multiple documents.

There are two ways to get document objects:

# create a new document

Doc = app.Documents.Add ()

# Open the document

Doc = app.Documents.Open ('Word file path')

* * Range** (Start, End) method to specify the specific range of the document. The position of the first character of the document is 0, the position of the last character is the total number of characters in the document, and all the contents are selected when no parameters are provided.

Selection: selection, document selection area, or insertion point (cursor flashing position). Only one selection can be activated for the entire document at the same time, and the default insertion point is at the beginning of the document.

S = app.Selection

Font: contains the font properties of the object (such as font name, font size, color, and so on).

# get selection or area font object

Font = s.Font

# or

Font = r.Font

Set font

# the font is set to imitate Song, and it must be installed on the computer.

Font.Name = 'imitating Song'

# font size is set to number three

Font.Size = 16

PageSetup: represents the page setup object. All page settings properties that contain documents, such as the left margin, bottom margin, and paper size, as properties for the PageSetup object.

# get the page setup object

Ps = doc.PageSetup

# the top margin is 79 pounds

Ps.TopMargin = 130

# Page size. A3 and A4 are 6 and 7 respectively.

Ps.PaperSize = 6

This is the end of the new document on how to operate word in python. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report