如何使用python正确保存Excel文件? [英] How to properly save excel file using python?
问题描述
当我尝试保存并比用python读取excel文件时遇到问题.这是我的功能:
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)
我正在更改单元格 D18
,保存文件,然后尝试读取其他具有公式但没有任何内容的单元格(也没有正确读取公式的单元格).但是,如果我手动打开文件并将其保存在Excel中,则代码行可以正确读取这些单元格.
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.
问题是此行 wb.save('8de69ccb60047ce5.xls')
.它会保存文件中的更改,但不会正确保存文件(我不知道如何对其进行描述).在python中更改文件后如何读取带有公式的单元格?
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?
推荐答案
使用保存功能将文件另存为 sample_book.xlsx .
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
这篇关于如何使用python正确保存Excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!