Python operation Excel and Word

As an interpreted language, Python is also a dynamically typed language, and its flexibility is very suitable for writing daily scripts.
Some do not pay attention to the daily needs of efficiency can Pythonbe achieved. Moreover, Pythonthere are enough open source dependency packages for our use.
In this paper, through the Pythonrealization of the operation of Excel and Word, as well as pit possible language.

Several options

Python's choices for Excel and Word are not many. There are two main categories.

  • Win32Com realizes the operation of Word, Excel, PowerPoint by calling Win32Api
  • python-docx (word read and write), python-excel (excel operation), realize the operation of Word and Excel on Windo32Api, the document is complete and rich

Win32Com

Windows will provide a special Com package for developers, such as Excel, Word, PowerPoint and other applications. Win32Com is a simple package of packages, and the interface layer is basically unchanged.
In many blogs, I have a headache because I don't know much about the interface of Win32Com and I can't develop it. In fact, Windows official provides detailed interface documentation, which is basically the same as the VBA language interface.
Therefore, you can refer to the implementation of the following documents:

for example:

In Excel, Sheet has two forms, Charts or Worksheet. The following example obtains a picture from Chart Sheet and exports it to the local.

import win32com.client as win32

# 从Excel excel_name 的sheet_name 中导出图片保存至picture_name 中
def export_picture(excel_name, picture_name, sheet_name):
    # 获取Excel api
    excel = win32.gencache.EnsureDispatch("Excel.Application")
    
    # 打开Excel 文档, wb 为文件句柄
    wb = excel.Workbooks.Open("excel_name.xlsx")
    
    # 导出图片
    wb.Sheets("sheet_name").Export("picture_name.jpg")
    
    wb.Close()

python-docx package

There are some inconveniences when using win32com operation, you can use the docx library. The use of docx library is more user-friendly.

doc is divided into paragraphs, heading, etc. according to the carriage return character. So if you need to insert a carriage return, you need to insert a paragraph.

for example:


import docx

def edit_doc(doc_name, text):
    doc = docx.Document(doc_name)

    # 添加文字,并居中
    # 此处可直接添加文字,add_paragraph 默认会调用 add_run
    doc.add_paragraph(text).paragraph_format.aligenment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER

    # 添加空段落
    doc.add_paragraph()


    # 添加 10x10 的表格
    rows = 10
    cols = 10
    table = doc.add_table( rows, cols, style="Table Grid")

    # 对表格内容进行赋值
    for x,y in [(x,y) for x in range(0,9) for y in range (0, 9)]:
        table.cell(x,y).text = str(x * y)
        table.cell(x,y).paragraphs[0].paragraph_format.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER
        # 设置 cell 的宽度
        table.cell(x,y).width = 25600 * 30

    # 第一行表格的合并
    table.cell(0,0).merge(table.cell(0,9))

    # 插入分页符
    doc.add_page_break()

    # 插入一张图片
    # 对word 的编辑,需要通过 add_run() 来实现
    doc.add_paragraph().add_run().add_picture("pciture_name.jpg", 25600 * 200, 25600 * 200)

    # 保存文件
    doc.save("output.docx")
    

Use of python-excel

There are two packages for excel operation, xlrd is used for excel reading, and xlwt is used for excel write operation. Here is only a brief introduction to excel reading.


# 获取表格的数据 [0, rows] x [0, cols]
def get_table_data(excel_name, sheet_name, rows, cols):
    result = []

    book = xlrd.open_workbook(excel_name)
    sheet = book.sheet_by_name(sheet_name)
    for row in range (0, rows):
        line = []
        for col in range (0, cols):
            line.append(sheet.cell_value(row, col))
        result.append(line)

    return result

A problem encountered:
how to judge the table content is empty:


if sheet.cell_type(x,y) is 0:
    print "is empty"

Technical summary

There are plenty of packages for operating Word and Excel. The above are the ones that are used more.
For interfaces that are not implemented in xlrd, xlwt, docx, you can use win32com.
If win32com cannot be implemented, you can consider whether the application does not provide the corresponding interface service.

Guess you like

Origin www.cnblogs.com/-lee/p/12690967.html