docx python Python docx file read and write

Python docx file read and write

 

Python read and write word documents have an existing database can handle. I am here using python-docx. You can use pip install python-docx install it.

Here say, ppt and excel have similar library Oh, and read files directly inside the xml data. So doc format have to find another deal with other libraries, doc xml-based format is not.

Help Documentation: http://python-docx.readthedocs.org/en/latest/

1, or open a new file. This is relatively simple with docx Document class, if the specified path is to open the document; if no path is a New Document

  1. #coding:utf-8
  2. import docx
  3.  
  4. # New Document
  5. doc_new = docx.Document()
  6.  
  7. # Read the document
  8. doc = docx.Document( ur'C:\1.docx')

2. Save the file. There are open, there is saved. By the Document class save method, wherein the parameter is stored in the file path, or a stream file to be saved. Generally designated path.

doc.save(path_or_stream)

3, a collection of objects. python-docx word document contains a collection of related objects.

  1. doc.paragraphs     # Paragraph collection
  2. doc.tables         # table collection
  3. doc.sections       # Day collection
  4. doc.styles         # pattern set
  5. doc.inline_shapes  # built-in graphics and so on ...

4, insert a paragraph. One of the paragraph is the word most basic objects.

  1. doc.add_paragraph ( U 'first stage', style = None)  # inserts a paragraph, text "first section"
  2. # The default is not to apply a style, here you can not write the style parameter, or specify a paragraph style
  3. doc.add_paragraph ( U 'second section', style = 'the Heading 2')
  4.  
  5. # These styles are the default styles with a word, you can list out directly what paragraph styles
  6. print [s.name for s in doc.styles if s.type==1]

5, the new style. The help files which have not made myself carefully, but also in English. I do use the items on this on their own to figure out how to use, as follows.

  1. #coding:utf-8
  2. from docx import Document
  3. docx.shared from  Import RGBColor  # This is the color of class docx
  4.  
  5. # New Document
  6. doc = Document()
  7.  
  8. # Add style (style name is the first parameter, the second parameter is the style Type: represents paragraph; 2 representing the character; 3 form)
  9. style = doc.styles.add_style( 'style name 1', 2)
  10.  
  11. # Set a specific style (modified style font in blue, of course, you can also modify the other, we all try)
  12. style.font.color.rgb = RGBColor( 0x0, 0x0, 0xff)
6, apply a character style. NATURAL character is inside a paragraph, the following methods may be employed to append text paragraph style and the character set.
  1. # Insert a blank paragraph
  2. p = doc.add_paragraph( '')
  3. p.add_run( '123', style="Heading 1 Char")
  4. p.add_run( '456')
  5. p.add_run( '789', style="Heading 2 Char")
  6.  
  7. # Such a paragraph on the application of two character styles, the middle of "456" is no application form
  8. p.text Print   # output is u'123456789 'still continuous
7, set the font. Of course, you can not set the style by setting some words can also be set directly.
  1. p = doc.add_paragraph( '')
  2. r = p.add_run( '123')
  3. = r.font.bold  True     # bold
  4. = r.font.italic  True   # skewing, and so ...

8, operating table. Form an object-type is frequently used.

  1. # Create a table of 2x3, style can not write
  2. table=doc.add_table(rows= 2,cols=3,style=None)
  3.  
  4. # This form may be obtained by a table of rows and columns the number of rows and columns
  5. print len(table.rows)
  6. print len(table.columns)
  7.  
  8. # Traversing table
  9. for row in table.rows:
  10.     row.cells[ 0].text = '1'
  11.      #print row.cells[0].text
  12.  
  13. # Add rows or columns
  14. table.add_row()
  15. table.add_column()

Word is that these common operations almost. We can help documentation, you can also use dir and help view object properties and methods to help.

Python read and write word documents have an existing database can handle. I am here using python-docx. You can use pip install python-docx install it.

Here say, ppt and excel have similar library Oh, and read files directly inside the xml data. So doc format have to find another deal with other libraries, doc xml-based format is not.

Help Documentation: http://python-docx.readthedocs.org/en/latest/

1, or open a new file. This is relatively simple with docx Document class, if the specified path is to open the document; if no path is a New Document

  1. #coding:utf-8
  2. import docx
  3.  
  4. # New Document
  5. doc_new = docx.Document()
  6.  
  7. # Read the document
  8. doc = docx.Document( ur'C:\1.docx')

2. Save the file. There are open, there is saved. By the Document class save method, wherein the parameter is stored in the file path, or a stream file to be saved. Generally designated path.

doc.save(path_or_stream)

3, a collection of objects. python-docx word document contains a collection of related objects.

  1. doc.paragraphs     # Paragraph collection
  2. doc.tables         # table collection
  3. doc.sections       # Day collection
  4. doc.styles         # pattern set
  5. doc.inline_shapes  # built-in graphics and so on ...

4, insert a paragraph. One of the paragraph is the word most basic objects.

  1. doc.add_paragraph ( U 'first stage', style = None)  # inserts a paragraph, text "first section"
  2. # The default is not to apply a style, here you can not write the style parameter, or specify a paragraph style
  3. doc.add_paragraph ( U 'second section', style = 'the Heading 2')
  4.  
  5. # These styles are the default styles with a word, you can list out directly what paragraph styles
  6. print [s.name for s in doc.styles if s.type==1]

5, the new style. The help files which have not made myself carefully, but also in English. I do use the items on this on their own to figure out how to use, as follows.

  1. #coding:utf-8
  2. from docx import Document
  3. docx.shared from  Import RGBColor  # This is the color of class docx
  4.  
  5. # New Document
  6. doc = Document()
  7.  
  8. # Add style (style name is the first parameter, the second parameter is the style Type: represents paragraph; 2 representing the character; 3 form)
  9. style = doc.styles.add_style( 'style name 1', 2)
  10.  
  11. # Set a specific style (modified style font in blue, of course, you can also modify the other, we all try)
  12. style.font.color.rgb = RGBColor( 0x0, 0x0, 0xff)
6, apply a character style. NATURAL character is inside a paragraph, the following methods may be employed to append text paragraph style and the character set.
  1. # Insert a blank paragraph
  2. p = doc.add_paragraph( '')
  3. p.add_run( '123', style="Heading 1 Char")
  4. p.add_run( '456')
  5. p.add_run( '789', style="Heading 2 Char")
  6.  
  7. # Such a paragraph on the application of two character styles, the middle of "456" is no application form
  8. p.text Print   # output is u'123456789 'still continuous
7, set the font. Of course, you can not set the style by setting some words can also be set directly.
  1. p = doc.add_paragraph( '')
  2. r = p.add_run( '123')
  3. = r.font.bold  True     # bold
  4. = r.font.italic  True   # skewing, and so ...

8, operating table. Form an object-type is frequently used.

  1. # Create a table of 2x3, style can not write
  2. table=doc.add_table(rows= 2,cols=3,style=None)
  3.  
  4. # This form may be obtained by a table of rows and columns the number of rows and columns
  5. print len(table.rows)
  6. print len(table.columns)
  7.  
  8. # Traversing table
  9. for row in table.rows:
  10.     row.cells[ 0].text = '1'
  11.      #print row.cells[0].text
  12.  
  13. # Add rows or columns
  14. table.add_row()
  15. table.add_column()

Word is that these common operations almost. We can help documentation, you can also use dir and help view object properties and methods to help.

Guess you like

Origin www.cnblogs.com/xupanfeng/p/12355439.html
Recommended