[英]How to format excel cells using read_excel and to_excel?
I currently have an excel file named 'excel example.xlsx'. 我目前有一个名为“ excel example.xlsx”的excel文件。 I have 2 sheets in this file. 我在这个文件中有2张纸。
The first sheet is titled 'Home' and the second sheet is titled 'Sheet2'. 第一张纸的标题为“ Home”,第二张纸的标题为“ Sheet2”。
Sheet 1: The first sheet, 'Home', has the sentence 'This is a test' in the first row starting in cell A1 and ending in column D1. 表格1:第一张表格“首页”在第一行中的句子“ This is a test”从单元格A1到列D1结束。
Here is an example of the Home sheet: 这是主页的示例:
Cell: A1
"This" Cell: B1
"is" Cell: C1
"a" Cell: D1
"test" Cell: A1
“此” Cell: B1
“是” Cell: C1
“ a” Cell: D1
“测试”
Sheet 2: 表格2:
In the second sheet, 'Sheet2', I would like the following data printed: 在第二张工作表“ Sheet2”中,我希望打印以下数据:
Cell: A1
"col1" Cell: B1
"col2" Cell: A2
"21" Cell: B1
"23" Cell: A3
"22" Cell: B3
"24" Cell: A1
“ col1” Cell: B1
“ col2” Cell: A2
“ 21” Cell: B1
“ 23” Cell: A3
“ 22” Cell: B3
“ 24”
Here is my code so far: 到目前为止,这是我的代码:
import pandas as pd
from pandas import read_excel
my_sheet_name = 'Home'
df1 = read_excel('excel example.xlsx', sheet_name = 'Home')
x = {'col1': [21, 22], 'col2': [23, 24]}
df2 = pd.DataFrame(data=x)
writer = pd.ExcelWriter('excel example.xlsx')
df1.to_excel(writer, 'Home')
df2.to_excel(writer, 'Sheet2')
writer.save()
This code works fine but there are some format issues. 这段代码可以正常工作,但是存在一些格式问题。
Issue 1: 问题1:
The 'Home' sheet printed the sentence 'This is a test' starting in cell B1, not A1 like I wanted. “主页”表从单元格B1开始打印句子“这是一个测试”,而不是我想要的A1。 Cell A1 is blank. 单元格A1为空白。
Issue 2: 问题2:
The sentence 'This is a test' has borders. 句子“ This is a test”带有边框。 I would like the borders removed if possible. 我希望尽可能删除边界。
Issue 3: 问题3:
The sentence 'This is a test' has been bolded. “这是测试”一词已加粗。 I would like the bold format removed if possible. 我希望尽可能删除粗体格式。
I am new to coding and will appreciate any help I can get. 我是编码的新手,将不胜感激。 Thanks! 谢谢!
From the documentation I modified the arguments passed in to_excel
to include index=False
to resolve Issue 1 that you had because pandas uses the first column for an index and interprets the first row of the Excel file as a header (hence the bolding & borders when re-written to an Excel file). 从文档中,我修改了to_excel
传递的参数以包括index=False
来解决您遇到的问题1,因为pandas使用第一列作为索引并将Excel文件的第一行解释为标题(因此,粗体和边框重新写入Excel文件)。 To resolve issue 2 & 3 I read the first sheet with header=None
addition so that the column headers would not be bolded and re-did the data loaded into df2 so as not to have headers as well 为了解决问题2和3,我阅读了第一张表header=None
表格,以使列标题不会被加粗,然后将加载到df2中的数据重新设置为不包含标题
The final code that worked for me was: 对我有用的最终代码是:
import pandas as pd
from pandas import read_excel
my_sheet_name = 'Home'
df1 = read_excel('excel example.xlsx', sheet_name = 'Home', header=None)
x = {'0': ['col1', 21, 22], '1': ['col2', 23, 24]}
df2 = pd.DataFrame(data=x)
writer = pd.ExcelWriter('excel example.xlsx')
df1.to_excel(writer, 'Home', index=False)
df2.to_excel(writer, 'Sheet2', index=False)
writer.save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.