openpyxl

Apply borders to all cells in a range with openpyxl

ⅰ亾dé卋堺 提交于 2019-11-29 21:57:53
问题 I have a script that takes a pandas dataframe and chops it up into several hundred chunks and saves each chunk as a separate excel file. Each chunk will have the same number of columns but the number of rows varies. I've figured out how to apply all the other necessary formatting to these files with openpyxl, but I haven't yet determined the fastest way to apply borders. Also, I think I'm just not applying borders correctly, because the code below (which I suspect shouldn't need to loop over

创建workbook及相关操作

余生颓废 提交于 2019-11-29 21:47:17
通过openpyxl模块创建workbook时,无需本地事先创建好excel,它会直接创建一个新的excel文件 创建workbook时,会至少包含一个worksheet 使用示例: 导入openpyxl模块的Workbook类 from openpyxl import Workbook 创建workbook,并获取workbook中第一个sheet wb = Workbook() #创建一个workbook ws = wb.active #获取当前workbook的第一个worksheet,默认的索引值是0,它是可以改变的 创建worksheet ws1 = wb.create_sheet() #在当前workbook的结尾处追加一个新的worksheet,名称是自动赋值的,如Sheet1,Sheet2,.... ws2 = wb.create_sheet(0) #在当前workbook的指定索引处追加一个新的worksheet,名称是自动赋值的,如Sheet1,Sheet2,.... ws1.title = "New Title1" #指定sheet名称为New Title1 ws2.title = "New Title2" #指定sheet名称为New Title2 ws3 = wb.create_sheet("MyNewSheet1")

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

馋奶兔 提交于 2019-11-29 21:38:35
问题 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

使用openpyxl 进行excel文件读写

放肆的年华 提交于 2019-11-29 21:18:40
文章目录 1、配置 2、安装模块: 3、读写: 注意点1: 注意点2: 3、demo代码: 4、读取一个Excel表格的所有工作表下的所有行 5、合并一个Excel表格下所有工作表(或者合并多个Excel表格也可以参考这个方法) 6、读取一个Excel表格下所有工作表的所有列 7、指定列合并Excel表格下所有的工作表(或者合并多个Excel) 参考: 1、配置 window10系统 python3.6 2、安装模块: pip install openpyxl 3、读写: 注意点1: 写入日期时,要转换成字符串写入,不然打开日期的单元格会显示成乱码 注意点2: 如果保存为的excel表格存在,并且你电脑打开了,会报错: PermissionError: [Errno 13] Permission denied: 'sample.xlsx' 3、demo代码: 参考官方修改的: https://pypi.org/project/openpyxl/ import datetime from openpyxl import Workbook wb = Workbook() # grab the active worksheet ws = wb.active # Data can be assigned directly to cells ws['A1'] = 42 # Rows can

openpyxl - adjust column width size

家住魔仙堡 提交于 2019-11-29 20:45:21
I have following script which is converting a CSV file to an XLSX file, but my column size is very narrow. Each time I have to drag them with mouse to read data. Does anybody know how to set column width in openpyxl ? Here is the code I am using. #!/usr/bin/python2.6 import csv from openpyxl import Workbook from openpyxl.cell import get_column_letter f = open('users_info_cvs.txt', "rU") csv.register_dialect('colons', delimiter=':') reader = csv.reader(f, dialect='colons') wb = Workbook() dest_filename = r"account_info.xlsx" ws = wb.worksheets[0] ws.title = "Users Account Information" for row

python3.7 安装pip3

纵然是瞬间 提交于 2019-11-29 19:35:32
环境:windows7、python3.7 应用:使用pip3命令安装openpyxl 步骤:1、进入python安装目录,我的是在C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Scripts 2、检查是否有pip.exe或pip3.exe,很显然,我这个目录下没有 3、安装pip (1)打开cmd界面,cd到C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Scripts,执行easy_install.exe pip。执行之后报如下错误: 猜想应该是版本不匹配,于是又执行了easy_install-3.7.exe pip,可以安装成功。 此时检查该目录下多了pip的东西。 4、检验是否安装成功 由于没有添加path路径,所以还是在当前目录下执行pip3。如下,说明安装成功。 5、安装openpyxl,当前目录下执行pip3 install openpyxl 来源: https://blog.csdn.net/dedell/article/details/100900933

How can I skip first several lines of the Excel sheet?

痴心易碎 提交于 2019-11-29 17:02:54
Using openpyxl I tried to read from the fifth line for some files. The files' first four lines are the header. Then the main content has a different format from the header. And I tried the method: import openpyxl file_name="xxx.xlsx" wb = openpyxl.load_workbook(filename=file_name, use_iterators = True) first_sheet = workbook.get_sheet_names()[0] ws = workbook.get_sheet_by_name(first_sheet) for index, row in enumerate(ws.iter_rows()): if start < index < stop: for c in row: print c.value It will always have the error: IndexError: list index out of range If I delete the first four lines, the data

copy cell style openpyxl

安稳与你 提交于 2019-11-29 16:58:39
问题 I am trying to copy a sheet, default_sheet , into a new sheet new_sheet in the same workbook. I did managed to create a new sheet and to copy the values from default sheet. How can I also copy the style of each cell into the new_sheet cells? new_sheet = workbook.create_sheet() new_sheet.title = sheetName default_sheet = workbook.get_sheet_by_name('default') new_sheet = workbook.get_sheet_by_name(sheetName) for row in default_sheet.rows: col_idx = float(default_sheet.get_highest_column())

openpyxl module does not have attribute '__version__' when imported by pandas

天涯浪子 提交于 2019-11-29 15:10:11
My traceback from running pandas takes me to: site-packages\pandas\io\excel.py line 58, in get_writer AttributeError: 'module' object has no attribute '__version__' I found this link to a git issue in the PyInstaller repo https://github.com/pyinstaller/pyinstaller/issues/1890 and found my openpyxl version, manually added it into the get_writer method like so: def get_writer(engine_name): if engine_name == 'openpyxl': try: import openpyxl #remove when conda update available openpyxl.__version__ = '2.3.2' # with version-less openpyxl engine # make sure we make the intelligent choice for the user

Openpyxl does not close Excel workbook in read only mode

痞子三分冷 提交于 2019-11-29 14:05:35
I want to be able to read an Excel file in Python, keep the Python script running doing something else after the reading is finished, and be able to edit the Excel file in another process in the meantime. I'm using python 2.7 and openpyxl. Currently it looks like: from openpyxl import load_workbook def get_excel_data(): OESwb = load_workbook(filename = OESconfigFile, data_only=True, read_only=True) ws = OESwb.get_sheet_by_name('MC01') aValue = ws['A1'].value return aValue val = get_excel_data() After I run the function, the Excel file is still locked for access from other processes (it gives