How to properly save excel file using python?

孤者浪人 提交于 2021-01-29 11:13:53

问题


I have a problem when I'm trying to save and than read excel file in python. So this is my function:

import openpyxl
import xlrd
from xlutils.copy import copy
import pandas as pd

def write_excel():
  wb = openpyxl.load_workbook('8de69ccb60047ce5.xlsx')
  sheet = wb.active

  sheet['D18'] = 3

  wb.save('8de69ccb60047ce5.xls')

  df1 = pd.read_excel('8de69ccb60047ce5.xls', sheet_name='Лист1', header=None, skiprows=1, usecols="H,I")
  print(df1)

  workbook = xlrd.open_workbook('8de69ccb60047ce5.xls')
  worksheet = workbook.sheet_by_index(0)
  print(worksheet.cell(17, 8).value)
  print(worksheet.cell(18, 8).value)

I'm changing cell D18, saving file and than trying to read other cells that has formulas but I get nothing (also cell without formulas read correctly). But if I open file manually and save it in Excel that lines of code read those cells correctly.

The problem is this line wb.save('8de69ccb60047ce5.xls'). It saves changes in file but it doesn't saves file correctly (I don't know how to discribe it). How can I read cell with formula after changing the file in python?


回答1:


Save a file as sample_book.xlsx with save function.

wb.save(filename = 'sample_book.xlsx')

For more info check out this link: https://www.soudegesu.com/en/post/python/create-excel-with-openpyxl/#save-file



来源:https://stackoverflow.com/questions/57049679/how-to-properly-save-excel-file-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!