openpyxl

How to swap columns using openpyxl

天大地大妈咪最大 提交于 2021-01-28 06:27:55
问题 I've .xlsx file. Rows are good, values are just fine. But i need to change the columns order by list of new columns positions, e.g: old = [1, 2, 3, 4] new = [2, 1, 4, 3] Docs are checked - there is no straightforward options for this problem. I've tried to iterate over columns, so: old = {cell.column: cell.value for cell in ws[1]}.keys() # [1, 2, 3, 4] new = [2, 1, 4, 3] for i, col in enumerate(ws.iter_cols(max_col=old[-1]), 1): if old[i-1] != new[i-1]: for one in ws[get_column_letter(i)]:

OpenPyXl: Mark row as heading

怎甘沉沦 提交于 2021-01-27 23:33:57
问题 This snippet works fine: from openpyxl import Workbook data = [ ['Year', 'Amount'], ['2016', '1000'], ['2017', '1300'], ['2018', '1500'], ] wb = Workbook() for row in data: wb.active.append(row) wb.save('test.xlsx') Now I would like to make the first row (Year, Amount) a heading. How to do this with openpyxl? 回答1: You can mark first row as Header by changing font color, freezing First row and making first row as print_title_rows Adding aRGB hex values color to font font = Font(color="FF0000")

Identify external workbook links using openpyxl

我怕爱的太早我们不能终老 提交于 2021-01-27 11:21:28
问题 I am trying to identify all cells that contain external workbook references, using openpyxl in Python 3.4. But I am failing. My first try consisted of: def find_external_value(cell): # identifies an external link in a given cell if '.xls' in cell.value: has_external_reference = True return has_external_value However, when I print the cell values that have external values to the console, it yields this: =[1]Sheet1!$B$4 =[2]Sheet1!$B$4 So, openpyxl obviously does not parse formulas containing

Excel wont recognize date using openpyxl

冷暖自知 提交于 2021-01-27 06:41:35
问题 I have a piece of code that converts a unix date to standard U.S. date format. It displays properly in excel, however excel doesn't recognize it as a date. if str(startdate).isdigit(): startdate = int(startdate) date = datetime.datetime.fromtimestamp(int(startdate)).strftime('%m/%d/%Y') ws4.cell(row=cell.row,column=2).value = date ws4.cell(row=cell.row,column=2).number_format = 'MM DD YYYY' Any idea how to get excel to see this as a date rather than text? 回答1: My mistake was assuming the line

Excel wont recognize date using openpyxl

北慕城南 提交于 2021-01-27 06:40:18
问题 I have a piece of code that converts a unix date to standard U.S. date format. It displays properly in excel, however excel doesn't recognize it as a date. if str(startdate).isdigit(): startdate = int(startdate) date = datetime.datetime.fromtimestamp(int(startdate)).strftime('%m/%d/%Y') ws4.cell(row=cell.row,column=2).value = date ws4.cell(row=cell.row,column=2).number_format = 'MM DD YYYY' Any idea how to get excel to see this as a date rather than text? 回答1: My mistake was assuming the line

Writing pandas DataFrame to Excel with different formats for different columns

落爺英雄遲暮 提交于 2021-01-20 16:54:51
问题 I am trying to write a pandas DataFrame to an .xlsx file where different numerical columns would have different formats. For example, some would show only two decimal places, some would show none, some would be formatted as percents with a "%" symbol, etc. I noticed that DataFrame.to_html() has a formatters parameter that allows one to do just that, mapping different formats to different columns. However, there is no similar parameter on the DataFrame.to_excel() method. The most we have is a

Writing pandas DataFrame to Excel with different formats for different columns

。_饼干妹妹 提交于 2021-01-20 16:54:44
问题 I am trying to write a pandas DataFrame to an .xlsx file where different numerical columns would have different formats. For example, some would show only two decimal places, some would show none, some would be formatted as percents with a "%" symbol, etc. I noticed that DataFrame.to_html() has a formatters parameter that allows one to do just that, mapping different formats to different columns. However, there is no similar parameter on the DataFrame.to_excel() method. The most we have is a

【问题解决方案】ImportError: No module named 'openpyxl'/‘xlrd’

孤街醉人 提交于 2021-01-15 06:44:36
【问题解决方案】ImportError: No module named 'openpyxl'/‘xlrd’ 参考文章: (1)【问题解决方案】ImportError: No module named 'openpyxl'/‘xlrd’ (2)https://www.cnblogs.com/anliux/p/10460285.html 备忘一下。 来源: oschina 链接: https://my.oschina.net/u/3797416/blog/4899771

Python_报错:PermissionError: [Errno 13] Permission denied: 'XXXXX.xlsx'

谁说我不能喝 提交于 2020-12-25 02:53:15
Python_报错: D:\Python36\python.exe D:/test_excel/excel_001.py Traceback (most recent call last): File "D:/test_excel/excel_001.py", line 14, in <module> wb.save("e:\\sample.xlsx") File "D:\Python36\lib\site-packages\openpyxl\workbook\workbook.py", line 367, in save save_workbook(self, filename) File "D:\Python36\lib\site-packages\openpyxl\writer\excel.py", line 282, in save_workbook archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True) File "D:\Python36\lib\zipfile.py", line 1090, in __init__ self.fp = io.open(file, filemode) PermissionError: [Errno 13] Permission denied: 'e:\\sample

用Python操作excel文档

◇◆丶佛笑我妖孽 提交于 2020-12-18 07:43:01
使用Python第三方库 这一节我们学习如何使用Python去操作Excel文档。如果大家有人不知道Excel的话,那么建议先学一学office办公基础。这里想要操作Excel,必须安装一个Python第三方库。 有人可能会疑问,第三方库是什么?其实第三方库就是非Python官方提供的库,也就是民间好心人开发出来的开源库,供大家免费使用。那这里肯定又有人会疑问,库是什么呢?我们前面已经知道了Python模块,其实库就是一个或多个相关连的Python模块合在一起,这样说很容易理解吧。通常Python中的所谓库,其实就是一个文件夹,这个文件夹中放了几个Python模块,而一个Python源代码文件就是一个模块。 安装 第三方库和Python的标准库不同,它需要我们手动去安装,不安装是没法使用的。这里简要说一下如何安装Python第三方库。通常一些书籍或网络博客中,推荐使用 pip 命令去自动下载安装第三方库,但是前提是需要我们将 pip 配置到环境变量中。这样去使用存在一些问题,当我们电脑装了多个Python版本时,极容易造成 pip 环境混乱。我在这里介绍一个小技巧,不需要去配置 pip 到环境变量中,且不会造成环境混乱。当我们需要使用 pip 命令时,在它前面加上 python -m 去使用,如下 1 python -m pip install 第三方库名称 好了