openpyxl

How to format cell with datetime object of the form 'yyyy-mm-dd hh:mm:ss' in Excel using openpyxl

拟墨画扇 提交于 2019-11-30 23:37:08
So, given: dttm = datetime.datetime.strptime("2014-06-23 13:56:30", "%Y-%m-%d %H:%M:%S") ws['A1'] = dttm The result in excel is that the correct date-time is written to the cell (you can see it where you'd input formulas). BUT, the cell display format is only MM/DD/YYYY. I need the cell to display like "6/23/2014 13:56" instead of just "6/23/2014". How can I explicitly format the cell to accomplish this? Thanks! Edit @alecxe This solution works and is exactly what I asked for. I would like to be able to save styles like the solution by @Woodham. Unfortunately it raises a typeError (see comment

python3-常用模块之openpyxl(1)

梦想与她 提交于 2019-11-30 22:52:50
1、创建工作簿 from openpyxl import Workbook # 创建excel对象 wb = Workbook() # 获取第一个sheet = wb.active # 单元格写入内容 ws.append(['单元1','单元2','钉钉']) ws['A1'] = 'A1' ws['A2'] = '你好'+'A2' # 新行写入多个单元格,默认会在新的一行里写入内容 ws.append(['单元11','单元22','钉钉1']) # 插入自定义时间对象 import time ws['B2'] = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) # 保存表格 wb.save("sample_demo.xlsx")  2、创建sheet from openpyxl import Workbook wb = Workbook() #创建sheet,如果已经存在多个sheet,默认在最后位置插入 ws1 = wb.create_sheet("sheet1") #设置sheet的名字,注意'sheet1'为sheet对象的名字 ws1.title = '表1' #指定位置创建sheet,第1个插入 ws2 = wb.create_sheet('sheet2',0) ws2.title = '表0' wb.save(

openpyxl returning empty cell values for formula series

这一生的挚爱 提交于 2019-11-30 21:44:09
Vitals: python 3.4.3 | openpyxl 2.2.3 | Excel 2013 Like everyone knows you can use Excel's fill handle to quickly set up a number series down a column [1,2,3,4,5,6,etc] and this works with formula as well [=sum(B1,C1), =sum(B2,C2), =sum(B3,C3), etc]. However when I attempt to use openpyxl to nab formula added using the fill handle in Excel, those cells are reported to be empty. It didn't work with my original code so I created a simple script containing this: for row in ws.rows: for cell in row: print('my value is:',cell.value) And ran it against this tiny (aww) Excel file. I used the fill

Openpyxl missing 'jdcal'

北慕城南 提交于 2019-11-30 19:19:00
I tried to install the openpyxl module, but during the installation it showed some errors with JDcall . When I then try to import it, I get this error: Traceback (most recent call last): File "C:\Andrzej\workspace\sandbox\sandbox.py", line 7, in <module> import openpyxl File "C:\Python34\lib\site-packages\openpyxl-2.0.5-py3.4.egg\openpyxl\__init__.py", line 29, in <module> from openpyxl.workbook import Workbook File "C:\Python34\lib\site-packages\openpyxl-2.0.5-py3.4.egg\openpyxl\workbook\__init__.py", line 25, in <module> from .workbook import * File "C:\Python34\lib\site-packages\openpyxl-2

借助openpyxl处理excel

被刻印的时光 ゝ 提交于 2019-11-30 19:14:47
一次处理excel中,原计划是借助excel中自带的工具进行处理,然而看到需要处理的列要达到30+,后来放弃了,用Python处理或许是一个不错的选择。 需求: 表格中每一列数据都是一个随机值,但是已知该列对应的标准区间,eg:20<x<40是正常区间,超出这一区间就是非正常区间,需要将落在正常区间的数据标记为0,落在非正常区间的数据标记为1。另外还有一种情况,eg:x<50是正常区间,超过50就是非法区间。 我的数据字典放在了sheet2中,首先去读取sheet2中的标准: def readSheet2(ExcelFullName): wb = load_workbook(ExcelFullName) sheets = wb.sheetnames print (sheets) mysheet = sheets[1] #获取sheet2的信息 ws = wb[mysheet] for i in range(2,29): mydata[ws.cell(row=i, column=1).value] = ws.cell(row=i, column=3).value print (mydata) 读出的标准放在了字典中: {'AST': '15-40', 'ALT': '9-50', 'GGT': '10-60', 'ALP': '45-125', 'ALB': '40-55', 'TB

How can openpyxl write list data in sheet?

主宰稳场 提交于 2019-11-30 16:27:17
recently I am using openpyxl to deal with some scientific data, when I was writting the list data to a excel file, I have a problem. datalist[row][col] wb=openpyxl.Workbook(optimized_write=Ture) ws_write = wb.create_sheet(0) for i in range(row): ws_write.append([datalist[i][0]]) wb.save(filename='data.xlsx') when it is done, seems like that it only can write the list into the first col of the xlsx file, how can I assign the same col of my datalist to the same col of xlsx file in the same sheet? Is there any reference to control the output? Thanks alot When using ws.append(row) you must insert

How can openpyxl write list data in sheet?

百般思念 提交于 2019-11-30 16:14:49
问题 recently I am using openpyxl to deal with some scientific data, when I was writting the list data to a excel file, I have a problem. datalist[row][col] wb=openpyxl.Workbook(optimized_write=Ture) ws_write = wb.create_sheet(0) for i in range(row): ws_write.append([datalist[i][0]]) wb.save(filename='data.xlsx') when it is done, seems like that it only can write the list into the first col of the xlsx file, how can I assign the same col of my datalist to the same col of xlsx file in the same

How can I edit Excel Workbooks using XLRD or openpyxl while preserving charts?

我与影子孤独终老i 提交于 2019-11-30 15:57:46
问题 I have a workbook that has some sheets in it. One of the sheets has charts in it. I need to use xlrd or openpyxl to edit another sheet, but, whenever I save the workbook, the charts are gone. Any workaround to this? Is there another python package that preserves charts and formatting? 回答1: This is currently not possible with either but I hope to have it in openpyxl 2.x. Patches / pull requests always welcome! ;-) 来源: https://stackoverflow.com/questions/19323049/how-can-i-edit-excel-workbooks

How can I edit Excel Workbooks using XLRD or openpyxl while preserving charts?

大城市里の小女人 提交于 2019-11-30 15:39:19
I have a workbook that has some sheets in it. One of the sheets has charts in it. I need to use xlrd or openpyxl to edit another sheet, but, whenever I save the workbook, the charts are gone. Any workaround to this? Is there another python package that preserves charts and formatting? This is currently not possible with either but I hope to have it in openpyxl 2.x. Patches / pull requests always welcome! ;-) 来源: https://stackoverflow.com/questions/19323049/how-can-i-edit-excel-workbooks-using-xlrd-or-openpyxl-while-preserving-charts

Pandas Excel Writer using Openpyxl with existing workbook

这一生的挚爱 提交于 2019-11-30 15:27:15
I have code from a while ago that I am re-using for a new task. The task is to write a new DataFrame into a new sheet, into an existing excel file. But there is one part of the code that I do not understand, but it just makes the code "work". working: from openpyxl import load_workbook import pandas as pd file = r'YOUR_PATH_TO_EXCEL_HERE' df1 = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]}) book = load_workbook(file) writer = pd.ExcelWriter(file, engine='openpyxl') writer.book = book # <---------------------------- piece i do not understand df1.to_excel(writer, sheet_name='New', index