简体   繁体   中英

Manipulating Existing Excel Workbook Using Python

I have code that will allow me to open an excel workbook and also write into existing excel sheets.

Currently I am using wb = copy(temp) which copies an existing template that has tables, graphs, formulas within some cells, and Names. After I copy the workbook and save under a new name, I lose all of the previously noted objects.

My question is: Is it possible to copy an excel workbook with all of the pre-existing tables, graphs, etc. and save the file after adding data to selected cells?

  • Output of Python code producing excel sheet without correct formatting
  • Excel sample sheet , this is what the output should look like

You can do this with the openpyxl package. Here's a simple example:

import openpyxl

# Grab first worksheet of existing excel file
wb = openpyxl.load_workbook('sample.xlsx')
ws = wb.worksheets[0]

# Write some cell values
ws['A1'] = 18
ws['A2'] = 39

# Save changes to excel file
wb.save("sample.xlsx")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM